0

假设有一个数组

(1) x=np.array([[1,2],[1,2],[1,2]])

和第二个数组

(2) y=np.array([[1],[1,2],[1,2,3]])

该命令size(x)返回沿每个轴的所有元素的总数。在这种情况下6。但是,size(y)返回3. 这一定是因为 numpy 在这种情况下将 (2) 解释为沿一个轴的三个元素(三个子数组),尽管shape(y)返回(3, ). 我现在的问题是:如何让 numpy 将 (2) 解释为具有三个轴的数组,以便size(y)返回所有原子元素的总数,即6

4

1 回答 1

1

I don't think it's possible to get the number of elements from y without looping over the objects.

The problem is that the elements of y are not numbers, they are objects (lists). Numpy does not support lists of lists and therefore it stores it as a 1-dimensional array of objects. I don't think there are Numpy methods to get the total number of elements in y.

于 2012-09-04T10:56:32.257 回答