在python numpy中,我如何理解数组在切片后变空的情况,如下所示,但形状仍然是多维的,也不是零。
    import numpy as np
    x = np.matrix([[1,2,3],
                   [4,5,6],
                   [7,8,9]])
    y = x[0:2,3:5]
    print(y)            # []
    print(y.shape)      # (2, 0)发布于 2019-07-24 00:05:42
在我的评论中,我将其归咎于np.matrix,但这实际上是一个切片问题:
In [57]: np.arange(1,10).reshape(3,3)[0:2, 3:5]                                                              
Out[57]: array([], shape=(2, 0), dtype=int64)
In [58]: np.matrix(np.arange(1,10).reshape(3,3))[0:2, 3:5]                                                   
Out[58]: matrix([], shape=(2, 0), dtype=int64)Python允许我们对‘范围’之外的列表进行切片,numpy也是如此。3:5在列维之外,因此生成的维度为0。
多维数组可以有0维。元素的总数是维度的乘积,在本例中为0。
创建这样的数组的其他方法:
In [61]: np.array([[],[]])                                                                                   
Out[61]: array([], shape=(2, 0), dtype=float64)
In [62]: np.zeros((2,0))                                                                                     
Out[62]: array([], shape=(2, 0), dtype=float64)https://stackoverflow.com/questions/57167456
复制相似问题