问题描述
我要为这件事生气了。
我有 2 个列表
A = [ [[1,2,3],[1,3]],[[1,3]]]
B = [ [[1,3]]]
当我将 A 和 B 的形状称为 numpy 数组时,我得到了这个:
In [33]: np.asarray(A).shape
Out[33]: (2,3,3)
In [31]: np.asarray(B).shape
Out[31]: (2,)
如何以与 A
相同的方式塑造 B
,即 (2,)
?
我想我明白为什么会发生这种情况,但我不知道如何防止这种情况发生。 任何人有任何帮助/想法吗?
谢谢!
解决方法
您的 2 个列表:
In [232]: A
Out[232]: [[[1,2,3],[1,3]],[[1,3]]]
In [233]: B
Out[233]: [[[1,3]]]
现在,解释为什么 B
结果比 A
更好?
In [234]: np.array(A)
Out[234]:
array([[[1,3]]])
In [235]: np.array(B)
<ipython-input-235-c938532b77c1>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this,you must specify 'dtype=object' when creating the ndarray.
np.array(B)
Out[235]:
array([list([[1,3]]),list([[1,3]])],dtype=object)