Numpy - 从数组中切割2d行或列向量

时间:2015-09-24 14:39:02

标签: python arrays numpy slice reshape

我正在尝试找到一个巧妙的小技巧,用于从2d数组中切割行/列并获取(col_size x 1)(1 x row_size)的数组。

是否有比在每次切片后使用numpy.reshape()更简单的方法?

干杯, 斯蒂芬

3 个答案:

答案 0 :(得分:13)

您可以在一次操作中切片并插入新轴。例如,这里是一个2D数组:

>>> a = np.arange(1, 7).reshape(2, 3)
>>> a
array([[1, 2, 3],
       [4, 5, 6]])

要切出单个(返回形状(2, 1)的数组),请将None作为第三维切片:

>>> a[:, 1, None]
array([[2],
       [5]])

要切出单个(返回形状(1, 3)的数组),请将None作为第二维切片:

>>> a[0, None, :]
array([[1, 2, 3]])

答案 1 :(得分:6)

使索引成为切片,列表或数组

    X[[0],:]
    X[0:1,4]

reshape除了要求打字之外没有任何问题。它并不慢。 [None,:]是一个很好的简写。

列表索引的使用可能是最短的,但确实会产生副本(加号或减号?)并且速度较慢

对于(100,100)整数数组:

In [487]: timeit x[[50],:]
100000 loops, best of 3: 10.3 µs per loop  # slowest

In [488]: timeit x[50:51,:]
100000 loops, best of 3: 2.24 µs per loop   # slice indexing is fast

In [489]: timeit x[50,:].reshape(1,-1)
100000 loops, best of 3: 3.29 µs per loop  # minimal time penalty

In [490]: timeit x[50,:][None,:]
100000 loops, best of 3: 3.55 µs per loop

In [543]: timeit x[None,50,:]          # **best**
1000000 loops, best of 3: 1.76 µs per loop

复制的一个测试是将数据缓冲区指针与原始指针进行比较。

In [492]: x.__array_interface__['data']
Out[492]: (175920456, False)
In [493]: x[50,:].__array_interface__['data']
Out[493]: (175940456, False)
In [494]: x[[50],:].__array_interface__['data']
Out[494]: (175871672, False)    # different pointer
In [495]: x[50:51,:].__array_interface__['data']
Out[495]: (175940456, False)
In [496]: x[50,:][None,:].__array_interface__['data']
Out[496]: (175940456, False)

答案 2 :(得分:3)

这个简单明了的方法怎么样?

In [73]: arr = (np.arange(5, 25)).reshape(5, 4)

In [74]: arr
Out[74]: 
array([[ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16],
       [17, 18, 19, 20],
       [21, 22, 23, 24]])

# extract column 1 as a column vector
In [79]: col1 = arr[:, [0]]
In [80]: col1.shape
Out[80]: (5, 1)

In [81]: col1
Out[81]: 
array([[ 5],
       [ 9],
       [13],
       [17],
       [21]])


# extract row 1 as a row vector
In [82]: row1 = arr[[0], :]

In [83]: row1.shape
Out[83]: (1, 4)

In [84]: row1
Out[84]: array([[5, 6, 7, 8]])