我有一个包含 4 个元素的矩阵,其中每个元素都是不同大小的矩阵。我正在尝试重塑矩阵,但它一直向我显示此错误: ValueError: 无法将输入数组从形状 (3,2) 广播到形状 (3,)
谁能解释一下这是什么以及如何解决它?
这是到目前为止的代码:
G_data = [-A,
-np.identity(n2),
A,
-np.identity(n2)]
G_temp = np.array(G_data, dtype=object)
G = object_array.reshape((2, 2))
print('Shape of G:', G.shape, '\n')
我可以通过以下方式生成您的错误消息:
In [508]: A=np.ones((3,2)); n2=3
In [509]: np.array([-A, np.identity(n2),-A,np.identity(n2)], object)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[509], line 1
----> 1 np.array([-A, np.identity(n2),-A,np.identity(n2)], object)
ValueError: could not broadcast input array from shape (3,2) into shape (3,)
请注意,这发生在
object
数组创建中,而不是在 reshape
中。
报告(并自己阅读)整个错误消息是个好主意。 忽略错误堆栈可能会误导我们 - 和您。