天府事儿 發表於 2026-1-6 10:39:31

NumPy 形状操纵的实现示例

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">改变数组的形状</a></li><li><a href="#_label1">将不同数组堆叠在一起 np.vstack: 行堆叠, np.hstack: 列堆叠</a></li><li><a href="#_label2">将一个数组拆分成几个较小的数组</a></li></ul></div><p class="maodian"><a name="_label0"></a></p><h2>改变数组的形状</h2>
<p>一个数组的形状是由每个轴的元素数量决定的:</p>
<div class="jb51code"><pre class="brush:py;">&gt;&gt;&gt; a = np.floor(10*np.random.random((3,4)))
&gt;&gt;&gt; a
array([[ 2.,8.,0.,6.],
       [ 4.,5.,1.,1.],
       [ 8.,9.,3.,6.]])
&gt;&gt;&gt; a.shape
(3, 4)
</pre></div>
<p>可以使用各种命令更改数组的形状。请注意,<strong>以下三个命令都返回一个修改后的数组,但不会更改原始数组</strong>:</p>
<div class="jb51code"><pre class="brush:py;">&gt;&gt;&gt; a.ravel()# returns the array, flattened
array([ 2.,8.,0.,6.,4.,5.,1.,1.,8.,9.,3.,6.])
&gt;&gt;&gt; a.reshape(6,2)# returns the array with a modified shape
array([[ 2.,8.],
       [ 0.,6.],
       [ 4.,5.],
       [ 1.,1.],
       [ 8.,9.],
       [ 3.,6.]])
&gt;&gt;&gt; a.T# returns the array, transposed
array([[ 2.,4.,8.],
       [ 8.,5.,9.],
       [ 0.,1.,3.],
       [ 6.,1.,6.]])
&gt;&gt;&gt; a.T.shape
(4, 3)
&gt;&gt;&gt; a.shape
(3, 4)
</pre></div>
<p>由 <strong>ravel() 产生的数组中元素的顺序通常是&ldquo;C风格&rdquo;</strong>,也就是说,最右边的索引&ldquo;变化最快&rdquo;,因此 之后的元素是 。如果将数组重新整形为其他形状,则该数组将被视为&ldquo;C风格&rdquo;。NumPy通常创建按此顺序存储的数组,因此 ravel() 通常不需要复制其参数,但如果数组是通过获取另一个数组的切片或使用不常见的选项创建的,则可能需要复制它。还可以使用可选参数指示函数 ravel() 和 reshape(),以使用FORTRAN样式的数组,其中最左边的索引变化最快。</p>
<p>该&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.reshape.html" rel="external nofollow"rel="external nofollow"   target="_blank" title="reshape">reshape</a>&nbsp;函数返回带有修改形状的参数,而该&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.ndarray.resize.html" rel="external nofollow"   target="_blank" title="ndarray.resize ">ndarray.resize&nbsp;</a>方法会修改数组本身:</p>
<div class="jb51code"><pre class="brush:py;">&gt;&gt;&gt; a
array([[ 2.,8.,0.,6.],
       [ 4.,5.,1.,1.],
       [ 8.,9.,3.,6.]])
&gt;&gt;&gt; a.resize((2,6))
&gt;&gt;&gt; a
array([[ 2.,8.,0.,6.,4.,5.],
       [ 1.,1.,8.,9.,3.,6.]])
</pre></div>
<p>如果在 reshape 操作中将 size 指定为-1,则会自动计算其他的 size 大小:</p>
<div class="jb51code"><pre class="brush:py;">&gt;&gt;&gt; a.reshape(3,-1)
array([[ 2.,8.,0.,6.],
       [ 4.,5.,1.,1.],
       [ 8.,9.,3.,6.]])
</pre></div>
<blockquote><p>另见</p>
<p><a href="https://numpy.org/devdocs/reference/generated/numpy.ndarray.shape.html" rel="external nofollow"   target="_blank" title="ndarray.shape">ndarray.shape</a>,&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.reshape.html" rel="external nofollow"rel="external nofollow"   title="reshape">reshape</a>,&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.resize.html" rel="external nofollow"   title="resize">resize</a>,&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.ravel.html" rel="external nofollow"   target="_blank" title="ravel">ravel</a></p></blockquote>
<p class="maodian"><a name="_label1"></a></p><h2>将不同数组堆叠在一起 np.vstack: 行堆叠, np.hstack: 列堆叠</h2>
<p>几个数组可以沿不同的轴堆叠在一起,例如:</p>
<div class="jb51code"><pre class="brush:py;">&gt;&gt;&gt; a = np.floor(10*np.random.random((2,2)))
&gt;&gt;&gt; a
array([[ 8.,8.],
       [ 0.,0.]])
&gt;&gt;&gt; b = np.floor(10*np.random.random((2,2)))
&gt;&gt;&gt; b
array([[ 1.,8.],
       [ 0.,4.]])
&gt;&gt;&gt; np.vstack((a,b))
array([[ 8.,8.],
       [ 0.,0.],
       [ 1.,8.],
       [ 0.,4.]])
&gt;&gt;&gt; np.hstack((a,b))
array([[ 8.,8.,1.,8.],
       [ 0.,0.,0.,4.]])
</pre></div>
<p>该函数将<a href="https://numpy.org/devdocs/reference/generated/numpy.column_stack.html" rel="external nofollow"rel="external nofollow"   target="_blank" title=" column_stack">&nbsp;column_stack</a>&nbsp;1D数组作为列堆叠到2D数组中。它仅相当于&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.hstack.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"   target="_blank" title="hstack">hstack</a>&nbsp;2D数组:</p>
<div class="jb51code"><pre class="brush:py;">&gt;&gt;&gt; from numpy import newaxis
&gt;&gt;&gt; np.column_stack((a,b))   # with 2D arrays
array([[ 8.,8.,1.,8.],
       [ 0.,0.,0.,4.]])
&gt;&gt;&gt; a = np.array()
&gt;&gt;&gt; b = np.array()
&gt;&gt;&gt; np.column_stack((a,b))   # returns a 2D array
array([[ 4., 3.],
       [ 2., 8.]])
&gt;&gt;&gt; np.hstack((a,b))         # the result is different
array([ 4., 2., 3., 8.])
&gt;&gt;&gt; a[:,newaxis]               # this allows to have a 2D columns vector
array([[ 4.],
       [ 2.]])
&gt;&gt;&gt; np.column_stack((a[:,newaxis],b[:,newaxis]))
array([[ 4.,3.],
       [ 2.,8.]])
&gt;&gt;&gt; np.hstack((a[:,newaxis],b[:,newaxis]))   # the result is the same
array([[ 4.,3.],
       [ 2.,8.]])
</pre></div>
<p>另一方面,该函数<a href="https://numpy.org/devdocs/reference/generated/numpy.ma.row_stack.html" rel="external nofollow"   title=" ma.row_stack ">&nbsp;ma.row_stack&nbsp;</a>等效<a href="https://numpy.org/devdocs/reference/generated/numpy.vstack.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"   title=" vstack">&nbsp;vstack</a>&nbsp;于任何输入数组。通常,对于具有两个以上维度的数组,&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.hstack.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"   title="hstack">hstack</a>&nbsp;沿其第二轴<a href="https://numpy.org/devdocs/reference/generated/numpy.vstack.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"   title=" vstack ">&nbsp;vstack&nbsp;</a>堆叠,沿其第一轴堆叠,并<a href="https://numpy.org/devdocs/reference/generated/numpy.concatenate.html" rel="external nofollow"rel="external nofollow"   title="concatenate">concatenate</a>&nbsp;允许可选参数给出连接应发生的轴的编号。</p>
<p><strong>注意</strong></p>
<p>在复杂的情况下,<a href="https://numpy.org/devdocs/reference/generated/numpy.r_.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"   target="_blank" title="r_">r_</a>和c&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.c_.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"   title="c_">c_</a>于通过沿一个轴堆叠数字来创建数组很有用。它们允许使用范围操作符(&ldquo;:&rdquo;)。</p>
<div class="jb51code"><pre class="brush:py;">&gt;&gt;&gt; np.r_
array()
</pre></div>
<p>与数组一起用作参数时,&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.r_.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"   title="r_">r_</a>&nbsp;和&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.c_.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"   title="c_">c_</a>&nbsp;在默认行为上类似于&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.vstack.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"   title="vstack">vstack</a>&nbsp;和&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.hstack.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"   title="hstack">hstack</a>&nbsp;,但允许使用可选参数给出要连接的轴的编号。</p>
<blockquote><p>另见</p>
<p><a href="https://numpy.org/devdocs/reference/generated/numpy.hstack.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"   title="hstack">hstack</a>,&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.vstack.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"   title="vstack">vstack</a>,&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.column_stack.html" rel="external nofollow"rel="external nofollow"   title="column_stack">column_stack</a>,&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.concatenate.html" rel="external nofollow"rel="external nofollow"   title="concatenate">concatenate</a>,&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.c_.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"   title="c_">c_</a>,&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.r_.html" rel="external nofollow"rel="external nofollow"rel="external nofollow"   title="r_">r_</a></p></blockquote>
<p class="maodian"><a name="_label2"></a></p><h2>将一个数组拆分成几个较小的数组</h2>
<p>使用<a href="https://numpy.org/devdocs/reference/generated/numpy.hsplit.html" rel="external nofollow"   title=" hsplit">&nbsp;hsplit</a>,可以沿数组的水平轴拆分数组,方法是指定要返回的形状相等的数组的数量,或者指定应该在其之后进行分割的列:</p>
<div class="jb51code"><pre class="brush:py;">&gt;&gt;&gt; a = np.floor(10*np.random.random((2,12)))
&gt;&gt;&gt; a
array([[ 9.,5.,6.,3.,6.,8.,0.,7.,9.,7.,2.,7.],
       [ 1.,4.,9.,2.,2.,1.,0.,6.,2.,2.,4.,0.]])
&gt;&gt;&gt; np.hsplit(a,3)   # Split a into 3
,
       [ 1.,4.,9.,2.]]), array([[ 6.,8.,0.,7.],
       [ 2.,1.,0.,6.]]), array([[ 9.,7.,2.,7.],
       [ 2.,2.,4.,0.]])]
&gt;&gt;&gt; np.hsplit(a,(3,4))   # Split a after the third and the fourth column
,
       [ 1.,4.,9.]]), array([[ 3.],
       [ 2.]]), array([[ 6.,8.,0.,7.,9.,7.,2.,7.],
       [ 2.,1.,0.,6.,2.,2.,4.,0.]])]
</pre></div>
<p><a href="https://numpy.org/devdocs/reference/generated/numpy.vsplit.html" rel="external nofollow"   title="vsplit">vsplit</a>&nbsp;沿垂直轴分割,并&nbsp;<a href="https://numpy.org/devdocs/reference/generated/numpy.array_split.html" rel="external nofollow"   title="array_split">array_split</a>&nbsp;允许指定要分割的轴。</p>
頁: [1]
查看完整版本: NumPy 形状操纵的实现示例