我想制作一个包含5行和4列的矩阵。问题是当我运行它时出现错误“Traceback(最近的呼叫最后): 文件“C:\ Users \ yasini \ Desktop \ 30.py”,第21行,in ['ART |Ф',0.71],['N |Ф',0.29],['V |Ф',0.0001],['P |Ф',0]]。。重塑(5,4) ValueError:新数组的总大小必须保持不变“。
我该如何解决?
import numpy as np
A = np.array([['ART|ART',0], ['N|ART',1], ['V|ART',0], ['P|ART',0],
['ART|N',0.0001], ['N|N',0.13], ['V|N',0.43], ['P|N',0.44],
['ART|V',0.65], ['N|V',0.35], ['V|V',0.0001], ['P|V',0.0001],
['ART|P',0.74], ['N|P',0.26], ['V|P',0.0001], ['P|P',0.0001],
['ART|Ф',0.71], ['N|Ф',0.29], ['V|Ф',0.0001], ['P|Ф',0]]).reshape(5,4)
print (A)
答案 0 :(得分:0)
忽略reshape
步骤并打印A.shape
。 A
中有多少项?如果没有4 * 5 = 20,那么重塑为(5,4)
将产生此错误。当一个步骤给你一个错误时,请确保前一个产生了你想要的结果。
我的猜测是形状是(20,2),而dtype是字符串(或者可能是对象)。它不是你期望的20对('str',int)
元组。
为什么要在一个数组中混合名称和数字?