简体   繁体   English

numpy数组的numpy数组具有一维形状

[英]Numpy array of numpy arrays has 1D shape

I have two numpy arrays of arrays (A and B). 我有两个数组(A和B)的numpy数组。 They look something like this when printed: 打印时它们看起来像这样:

A: A:

[array([0, 0, 0]) array([0, 0, 0]) array([1, 0, 0]) array([0, 0, 0])
 array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0])
 array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 1]) array([0, 0, 0])
 array([1, 0, 0]) array([0, 0, 1]) array([0, 0, 0]) array([0, 0, 0])
 array([0, 0, 0]) array([1, 0, 0]) array([0, 0, 1]) array([0, 0, 0])]

B: B:

[[  4.302135e-01   4.320091e-01   4.302135e-01   4.302135e-01
    1.172584e+08]
 [  4.097128e-01   4.097128e-01   4.077675e-01   4.077675e-01
    4.397120e+07]
 [  3.796353e-01   3.796353e-01   3.778396e-01   3.778396e-01
    2.643200e+07]
 [  3.871173e-01   3.890626e-01   3.871173e-01   3.871173e-01
    2.161040e+07]
 [  3.984899e-01   4.002856e-01   3.984899e-01   3.984899e-01
    1.836240e+07]
 [  4.227315e-01   4.246768e-01   4.227315e-01   4.227315e-01
    1.215760e+07]
 [  4.433817e-01   4.451774e-01   4.433817e-01   4.433817e-01
    9.340800e+06]
 [  4.620867e-01   4.638823e-01   4.620867e-01   4.620867e-01
    1.173760e+07]]

type(A) , type(A[0]) , type(B) , type(B[0]) are all <class 'numpy.ndarray'> . type(A)type(A[0])type(B)type(B[0])都是<class 'numpy.ndarray'>

However, A.shape is (20,) , while B.shape is (8, 5) . 但是, A.shape(20,) ,而B.shape(8, 5) B.shape (8, 5)

Question 1: Why is A.shape one-dimensional, and how do I make it two-dimensional like B.shape ? 问题1:为什么A.shape是一维的,我如何使其像B.shape一样是B.shape They're both arrays of arrays, right? 它们都是数组的数组,对不对?

Question 2, possibly related to Q1: Why does printing A show the calls of array() , while printing B doesn't, and why do the elements of the subarrays of B not have commas in-between them? 问题2,可能与问题1有关:为什么打印A不会显示对array()的调用,而打印B却不显示,为什么B的子array()的元素之间没有逗号?

Thanks in advance. 提前致谢。

A.dtype is O , object, B.dtype is float . A.dtypeO ,对象, B.dtypefloat

A is a 1d array that contains objects, which happen to be arrays. A是一维数组,其中包含对象,恰好是数组。 They could just as well be lists or None`. 它们也可以是列表或“无”。

B is a 2d array of floats. B是2d浮点数组。 Indexing one row of B gives a 1d array. 索引B一行给出一个1d数组。

So A[0] and B[0] can appear to produce the same thing, but the selection process is different. 因此, A[0]B[0]看起来可以产生相同的事物,但是选择过程不同。

Try np.concatenate(A) , or np.vstack(A) . 尝试np.concatenate(A)np.vstack(A) Both of these then treat A as a list of arrays, and join them either in 1 or 2d. 然后,它们都将A视为数组列表,并以1d或2d形式将它们连接。

Converting object arrays to regular comes up quite often. 将对象数组转换为常规数组经常会出现。

Converting a 3D List to a 3D NumPy array is a little more general that what you need, but gives a lot of useful information. 将3D列表转换为3D NumPy数组比您所需要的要通用一些,但是会提供很多有用的信息。

also

Convert a numpy array of lists to a numpy array 将列表的numpy数组转换为numpy数组

================== ==================

In [28]: A=np.empty((5,),object)
In [31]: A
Out[31]: array([None, None, None, None, None], dtype=object)
In [32]: for i in range(5):A[i]=np.zeros((3,),int)
In [33]: A
Out[33]: 
array([array([0, 0, 0]), array([0, 0, 0]), array([0, 0, 0]),
       array([0, 0, 0]), array([0, 0, 0])], dtype=object)
In [34]: print(A)
[array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0])
 array([0, 0, 0])]
In [35]: np.vstack(A)
Out[35]: 
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM