我有一个完全用numpy函数编写的函数,并接受两个输入值。该函数由一些矩阵运算组成,当我传递两个大数组时,它给了我ValueError:矩阵必须是二维的。使用循环和numpy.apply_along_axis可解决此问题,但这些方法会使代码运行缓慢。
以下是我编写的代码
app.set('views', './views') //here check if your index file is inside the views or the directory of your template folder
app.set('view engine', 'ejs')
它给我以下错误
ValueError:矩阵必须为二维
答案 0 :(得分:0)
回溯为:
...: fun(data[:,0],data[:,1])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-262-144f145bebe5> in <module>
10
11
---> 12 fun(data[:,0],data[:,1])
<ipython-input-262-144f145bebe5> in fun(x, y)
5 f1 = np.sin(x)*np.cos(y)
6 f2 = np.cos(x)*np.sin(y)
----> 7 eig1 = f1*np.mat([[f1],[f2]])
8 eig2 = f2*np.mat([[f2+f1],[f1]])
9 return np.sum(np.linalg.eig(eig1*eig2.T)[0])
/usr/local/lib/python3.6/dist-packages/numpy/matrixlib/defmatrix.py in asmatrix(data, dtype)
69
70 """
---> 71 return matrix(data, dtype=dtype, copy=False)
72
73
/usr/local/lib/python3.6/dist-packages/numpy/matrixlib/defmatrix.py in __new__(subtype, data, dtype, copy)
149 shape = arr.shape
150 if (ndim > 2):
--> 151 raise ValueError("matrix must be 2-dimensional")
152 elif ndim == 0:
153 shape = (1, 1)
ValueError: matrix must be 2-dimensional
所以问题出在np.mat
函数的输入上。那是什么?
In [263]: data.shape
Out[263]: (360000, 2)
sin / cos调用不会改变形状,所以:
In [264]: [[data[:,0]],[data[:,1]]]
Out[264]:
[[array([-0.95915424, 1.38013956, 1.26480082, ..., 1.34129623,
1.14664781, 0.90385798])],
[array([-0.77785621, 0.695089 , 1.17894725, ..., -0.2891861 ,
0.47051436, -0.22550854])]]
In [265]: np.array([[data[:,0]],[data[:,1]]])
Out[265]:
array([[[-0.95915424, 1.38013956, 1.26480082, ..., 1.34129623,
1.14664781, 0.90385798]],
[[-0.77785621, 0.695089 , 1.17894725, ..., -0.2891861 ,
0.47051436, -0.22550854]]])
In [266]: _.shape
Out[266]: (2, 1, 360000)
因此,您尝试为np.mat
提供3d数组。
np.mat
会起作用:
In [274]: np.mat([data[:,0],data[:,1]]).shape
Out[274]: (2, 360000)
但是f1*np.mat([f1,f2])
的矩阵乘积有问题:
In [275]: data[:,0]*np.mat([data[:,0],data[:,1]])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-275-555ec143af3f> in <module>
----> 1 data[:,0]*np.mat([data[:,0],data[:,1]])
/usr/local/lib/python3.6/dist-packages/numpy/matrixlib/defmatrix.py in __rmul__(self, other)
224
225 def __rmul__(self, other):
--> 226 return N.dot(other, self)
227
228 def __imul__(self, other):
ValueError: shapes (360000,) and (2,360000) not aligned: 360000 (dim 0) != 2 (dim 0)
通过制作np.mat
,*
现在是dot
的产品,而不是按元素的产品。
答案 1 :(得分:0)
我在使用XGBoost时遇到了此问题,并设法解决了以下问题:
代替:
import numpy as np
x_trainNPArray = np.array(x_train)
x_testNPArray = np.array(x_test)
y_trainNPArray = np.array(y_train)
y_testNPArray = np.array(y_test)
model.fit(x_trainNPArray,y_trainNPArray)
...
,我曾经用过:
x_trainNPArray = np.vstack(x_trainNPArray)
x_testNPArray = np.vstack(x_testNPArray)
y_trainNPArray = np.vstack(y_trainNPArray)
y_testNPArray = np.vstack(y_testNPArray )
model.fit(x_trainNPArray,y_trainNPArray)
...