Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
922 views
in Technique[技术] by (71.8m points)

python - Numpy.dot() dimensions not aligned

I'm having trouble giving the right input to the scipy.signal.dlsim method.

The method requires the 4 state space matrices:

A = np.array([
    [0.9056, -0.1908, 0.0348, 0.0880],
    [0.0973, 0.8728, 0.4091, -0.0027],
    [0.0068, -0.1694, 0.9729, -0.6131],
    [-0.0264, 0.0014, 0.1094, 0.6551]
    ])

B = np.array([
    [0, -0.0003, -0.0330, -0.0042, -0.0037],
    [0, -0.0005, 0.0513, -0.0869, -0.1812],
    [0, 0.0003, -0.0732, 1.1768, -1.1799],
    [0, -0.0002, -0.0008, 0.2821, -0.4797]
    ])

C = np.array([-0.01394, -0.0941, 0.0564, 0.0435])

D = np.array([0, 0.0004, -0.0055, 0.3326, 0.5383])

and an input vector which I build in the following way:

inputs = np.array([
    data['input1'].values(),
    data['input2'].values(),
    data['input3'].values(),
    data['input4'].values(),
    data['input5'].values()
])

This creates an inputs matrix with (5x752) dimensions (I have 752 data points). So I take the transpose of the inputs matrix to preprocess my data:

inputs = np.transpose(inputs)

The inputs matrix now has the (752x5) dimensions I presume are necessary for the simulation algorithm of scipy.

When I execute the method, I get the following error:

    110     # Simulate the system
    111     for i in range(0, out_samples - 1):
--> 112         xout[i+1,:] = np.dot(a, xout[i,:]) + np.dot(b, u_dt[i,:])
    113         yout[i,:] = np.dot(c, xout[i,:]) + np.dot(d, u_dt[i,:])
    114 

ValueError: shapes (4,5) and (1,5) not aligned: 5 (dim 1) != 1 (dim 0)

I understand scipy is unable to make this multiplication but I do not know in which format I should give my inputs array to the method. If I would not transpose the matrix the dimensions would be even worse (1x752).

Am I missing something here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The numpy.dot() method works separately for a matrix and an array. I converted the array somewhere to a matrix to be able to easily read the dimensions which caused this error. If the vector is interpreted as a matrix, it is seen by Numpy as a row vector. This gives the dimensions error: (4x5) x (1x5).

When numpy sees the vector as an array, numpy.dot() automatically does the right multiplication because the vector is seen as a column vector and the np.dot() can be calculated correctly: (4x5) x (5x1)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...