NumPy的hstack函数详细教程
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">1. 函数基本语法</a></li><li><a href="#_label1">2. 一维数组的堆叠</a></li><li><a href="#_label2">3. 二维数组的堆叠</a></li><li><a href="#_label3">4. 三维数组的堆叠</a></li><li><a href="#_label4">5. 注意事项</a></li><li><a href="#_label5">7. 与其他堆叠函数比较</a></li><li><a href="#_label6">8. 实际应用示例</a></li></ul></div><p>`np.hstack()`是NumPy中用于<strong>水平(按列)堆叠数组的函数(</strong><span>这意味着它将数组在第二个轴(即列方向)上堆叠,但是要求除第二个轴外其他轴的大小必须相同</span><strong>)</strong>。下面通过详细的解释和示例来学习这个函数。</p><p class="maodian"><a name="_label0"></a></p><h2>1. 函数基本语法</h2>
<div class="jb51code"><pre class="brush:py;">numpy.hstack(tup)
</pre></div>
<p><strong>参数:</strong><br />- `tup`:包含要堆叠数组的序列(通常是元组或列表),所有数组必须具有相同的形状(除了第二个轴,即列方向)</p>
<p><strong>返回值:</strong><br />- 堆叠后的数组</p>
<p class="maodian"><a name="_label1"></a></p><h2>2. 一维数组的堆叠</h2>
<p>一维数组的水平堆叠会创建一个更长的一维数组:</p>
<div class="jb51code"><pre class="brush:py;">import numpy as np
# 一维数组示例
a = np.array()
b = np.array()
print("a:", a)
print("b:", b)
print("a.shape:", a.shape)
print("b.shape:", b.shape)
result = np.hstack((a, b))
print("hstack result:", result)
print("result.shape:", result.shape)</pre></div>
<p><strong>输出:</strong></p>
<blockquote><p>a: <br />b: <br />a.shape: (3,)<br />b.shape: (3,)<br />hstack result: <br />result.shape: (6,)</p></blockquote>
<p class="maodian"><a name="_label2"></a></p><h2>3. 二维数组的堆叠</h2>
<p><strong>二维数组的水平堆叠会增加列数:</strong></p>
<div class="jb51code"><pre class="brush:py;"># 二维数组示例
a = np.array([, ])
b = np.array([, ])
print("a:")
print(a)
print("b:")
print(b)
print("a.shape:", a.shape)
print("b.shape:", b.shape)
result = np.hstack((a, b))
print("hstack result:")
print(result)
print("result.shape:", result.shape)</pre></div>
<p><strong>输出:</strong></p>
<blockquote><p>a:<br />[<br /> ]<br />b:<br />[<br /> ]<br />a.shape: (2, 2)<br />b.shape: (2, 2)<br />hstack result:<br />[<br /> ]<br />result.shape: (2, 4)</p></blockquote>
<p class="maodian"><a name="_label3"></a></p><h2>4. 三维数组的堆叠</h2>
<p>对于三维数组,`hstack`会在第二个维度(列)上堆叠:</p>
<div class="jb51code"><pre class="brush:py;"># 三维数组示例
a = np.random.randn(2, 3, 4)
b = np.random.randn(2, 2, 4)
print("a.shape:", a.shape)
print("b.shape:", b.shape)
result = np.hstack((a, b))
print("result.shape:", result.shape)</pre></div>
<p><strong>输出:</strong></p>
<blockquote><p>a.shape: (2, 3, 4)<br />b.shape: (2, 2, 4)<br />result.shape: (2, 5, 4)</p></blockquote>
<p class="maodian"><a name="_label4"></a></p><h2>5. 注意事项</h2>
<p>1. <strong>形状要求</strong>:所有输入数组在除了第二个轴以外的所有轴上必须具有相同的形状<br />2. <strong>错误示例</strong>:<br /> </p>
<div class="jb51code"><pre class="brush:py;"> # 这会报错,因为行数不同
a = np.array([, ])
b = np.array([])# 形状不匹配
# result = np.hstack((a, b))# ValueError</pre></div>
<p class="maodian"><a name="_label5"></a></p><h2>7. 与其他堆叠函数比较</h2>
<p><strong>- `vstack()`</strong>:垂直堆叠(按行)<br />-<strong> `dstack()`</strong>:深度堆叠(沿第三个轴)<br />-<strong> `concatenate()`</strong>:通用连接函数,可以指定轴```python<br /><strong>比较不同堆叠方法</strong></p>
<div class="jb51code"><pre class="brush:py;">a = np.array([, ])
b = np.array([, ])
print("hstack (水平):")
print(np.hstack((a, b)))
print("vstack (垂直):")
print(np.vstack((a, b)))
print("dstack (深度):")
print(np.dstack((a, b)))</pre></div>
<p><strong>输出</strong></p>
<blockquote><p>hstack (水平):<br />[<br /> ]<br />vstack (垂直):<br />[<br /> <br /> <br /> ]<br />dstack (深度):<br />[[<br /> ]</p>
<p> [<br /> ]]</p></blockquote>
<p class="maodian"><a name="_label6"></a></p><h2>8. 实际应用示例</h2>
<div class="jb51code"><pre class="brush:py;"># 合并特征矩阵
features1 = np.random.randn(100, 5)# 100个样本,5个特征
features2 = np.random.randn(100, 3)# 100个样本,3个特征
combined_features = np.hstack((features1, features2))
print("原始特征形状:", features1.shape, features2.shape)
print("合并后特征形状:", combined_features.shape)</pre></div>
<p>通过这个教程,你应该能够理解`hstack`函数的工作原理和适用场景。记住关键点是:**水平堆叠会增加数组的列数(第二个维度)**,并且所有输入数组在除了第二个维度外的其他维度上必须具有相同的形状。</p>
頁:
[1]