python


4、np.newaxis的作用

<p>np.newaxis的作用就是在这一位置增加一个一维,这一位置指的是np.newaxis所在的位置,比较抽象,需要配合例子理解。 x1 = np.array([1, 2, 3, 4, 5])</p> <h1>the shape of x1 is (5,)</h1> <pre><code>x1_new = x1[:, np.newaxis] # now, the shape of x1_new is (5, 1) # array([[1], # [2], # [3], # [4], # [5]]) x1_new = x1[np.newaxis,:] # now, the shape of x1_new is (1, 5) # array([[1, 2, 3, 4, 5]])</code></pre> <p>再来一个例子 In [124]: arr = np.arange(5*5).reshape(5,5)</p> <pre><code>In [125]: arr.shape Out[125]: (5, 5) array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) # promoting 2D array to a 5D array In [126]: arr_5D = arr[np.newaxis, ..., np.newaxis, np.newaxis] In [127]: arr_5D.shape Out[127]: (1, 5, 5, 1, 1) array([[[[[ 0]], [[ 1]], [[ 2]], [[ 3]], [[ 4]]], [[[ 5]], [[ 6]], [[ 7]], [[ 8]], [[ 9]]], [[[10]], [[11]], [[12]], [[13]], [[14]]], [[[15]], [[16]], [[17]], [[18]], [[19]]], [[[20]], [[21]], [[22]], [[23]], [[24]]]]])</code></pre> <p>作者:VanJordan 链接:<a href="https://www.jianshu.com/p/78e1e281f698">https://www.jianshu.com/p/78e1e281f698</a> 来源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。</p>

页面列表

ITEM_HTML