【发布时间】:2015-09-18 16:45:53
【问题描述】:
我很难理解在 numpy 中切片后如何确定结果数组的形状。例如,我正在使用以下简单代码:
import numpy as np
array=np.arange(27).reshape(3,3,3)
slice1 = array[:,1:2,1]
slice2= array[:,1,1]
print "Content in slice1 is ", slice1
print "Shape of slice1 is ", slice1.shape
print "Content in slice2 is ",slice2
print "Shape of Slice2 is", slice2.shape
这个输出是:
Content in slice1 is
[[ 4]
[13]
[22]]
Shape of slice1 is (3, 1)
Content in slice2 is [ 4 13 22]
Shape of Slice2 is (3,)
在这两种情况下,内容都是相同的(应该如此)。但它们的形状不同。那么,最终的形状是如何由 numpy 确定的呢?
【问题讨论】:
-
基本上:切片不会减少维数,但是用整数索引每次都会减少1维数。
-
有一个
shape属性。查看x.__array_interface__以查看它和其他属性。