Python Numpy.dot() 尺寸未对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28028991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:38:00  来源:igfitidea点击:

Numpy.dot() dimensions not aligned

pythonnumpymatrixscipy

提问by arnoutaertgeerts

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

我无法为该scipy.signal.dlsim方法提供正确的输入。

The method requires the 4 state space matrices:

该方法需要 4 个状态空间矩阵:

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:

这将创建一个具有(5x752)维度的输入矩阵(我有 752 个数据点)。所以我对输入矩阵进行转置来预处理我的数据:

inputs = np.transpose(inputs)

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

输入矩阵现在具有(752x5)我认为 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).

我知道 scipy 无法进行这种乘法运算,但我不知道应该以哪种格式将输入数组提供给该方法。如果我不转置矩阵,尺寸会更糟(1x752)。

Am I missing something here?

我在这里错过了什么吗?

采纳答案by arnoutaertgeerts

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).

numpy.dot()方法分别适用于矩阵和数组。我将某个地方的数组转换为矩阵,以便能够轻松读取导致此错误的维度。如果向量被解释为矩阵,则 Numpy 将其视为行向量。这给出了尺寸错误:(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)

当 numpy 将向量视为数组时,会numpy.dot()自动进行正确的乘法,因为向量被视为列向量并且np.dot()可以正确计算:(4x5) x (5x1)