<p>使索引成为切片、列表或数组</p>
<pre><code> X[[0],:]
X[0:1,4]
</code></pre>
<p>但是<code>reshape</code>没有什么问题,除了它需要输入。不慢。<code>[None,:]</code>是一个很好的速记。</p>
<p>使用列表索引可能最短,但它确实会产生一个副本(加号还是减号?)而且速度较慢</p>
<p>对于<code>(100,100)</code>整数数组:</p>
<pre><code>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
</code></pre>
<p>复制的一个测试是将数据缓冲区指针与原始指针进行比较。</p>
<pre><code>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)
</code></pre>