I'm use python keras to build a cnn model.
I follow cnn mnist example and modify to my code. This is the example I found
# Read MNIST data
(X_Train, y_Train), (X_Test, y_Test) = mnist.load_data()
# Translation of data
X_Train40 = X_Train.reshape(X_Train.shape[0], 28, 28, 1).astype('float32')
X_Test40 = X_Test.reshape(X_Test.shape[0], 28, 28, 1).astype('float32')
My data has 30222 rows and 6 columns of csv.
Which is 10074 data each data is 3 * 6 size for one block of information.
For example, the 1 ~ 3row of the matrix is one block of information.
Then I changed the format of my data.
X_Train40 = X_Train.reshape(10074, 3, 6, 1)
X_Test40 = X_Test.reshape(4319, 3, 6, 1)
Then this error occurs.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-133-4f23172d450a> in <module>()
----> 1 X_Train40 = X_Train.reshape(10074, 3, 6, 1)
2 X_Test40 = X_Test.reshape(4319, 3, 6, 1)
~\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py in __array_finalize__(self, obj)
269 return
270 elif (ndim > 2):
--> 271 raise ValueError("shape too large to be a matrix.")
272 else:
273 newshape = self.shape
ValueError: shape too large to be a matrix.
答案 0 :(得分:2)
只是猜测,但由于数据来自csv文件,因此它被转换为np.matrix
,其限制为2维。
内部numpy将尝试保持矩阵的尺寸,因此要重塑为更高的尺寸,您需要将其转换为ndarray
,如下所示:
X_Train = np.array(X_Train)
X_Test = np.array(X_Test)
X_Train40 = X_Train.reshape(10074, 3, 6, 1)
X_Test40 = X_Test.reshape(4319, 3, 6, 1)
答案 1 :(得分:1)
您知道...我今天遇到这个问题,找不到一个有帮助的答案。
您的问题可能已在此时解决,但是当python抱怨“形状太大而不能成为矩阵”时,要看的一件事是变量的类型,即 它是否是一个numpy .matrix数据类型或numpy.ndarray ?
如果是前者,则您有麻烦。
尝试避免使用numpy.matrix类型,特别是如果您要进行任何线性代数运算或将其堆叠(使用(d / v / h)堆叠等)并坚持使用numpy.ndarray