最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

NumPy的hstack函數(shù)詳細(xì)教程

 更新時間:2026年01月06日 10:56:59   作者:我愛派生  
np.hstack()是NumPy庫中用于水平堆疊數(shù)組的函數(shù),本文就來介紹一下NumPy的hstack函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

`np.hstack()`是NumPy中用于水平(按列)堆疊數(shù)組的函數(shù)(這意味著它將數(shù)組在第二個軸(即列方向)上堆疊,但是要求除第二個軸外其他軸的大小必須相同。下面通過詳細(xì)的解釋和示例來學(xué)習(xí)這個函數(shù)。

1. 函數(shù)基本語法

numpy.hstack(tup)

參數(shù):
- `tup`:包含要堆疊數(shù)組的序列(通常是元組或列表),所有數(shù)組必須具有相同的形狀(除了第二個軸,即列方向)

返回值:
- 堆疊后的數(shù)組

2. 一維數(shù)組的堆疊

一維數(shù)組的水平堆疊會創(chuàng)建一個更長的一維數(shù)組:

import numpy as np

# 一維數(shù)組示例
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

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)

輸出:

a: [1 2 3]
b: [4 5 6]
a.shape: (3,)
b.shape: (3,)
hstack result: [1 2 3 4 5 6]
result.shape: (6,)

3. 二維數(shù)組的堆疊

二維數(shù)組的水平堆疊會增加列數(shù):

# 二維數(shù)組示例
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

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)

輸出:

a:
[[1 2]
 [3 4]]
b:
[[5 6]
 [7 8]]
a.shape: (2, 2)
b.shape: (2, 2)
hstack result:
[[1 2 5 6]
 [3 4 7 8]]
result.shape: (2, 4)

4. 三維數(shù)組的堆疊

對于三維數(shù)組,`hstack`會在第二個維度(列)上堆疊:

# 三維數(shù)組示例
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)

輸出:

a.shape: (2, 3, 4)
b.shape: (2, 2, 4)
result.shape: (2, 5, 4)

5. 注意事項

1. 形狀要求:所有輸入數(shù)組在除了第二個軸以外的所有軸上必須具有相同的形狀
2. 錯誤示例
 

   # 這會報錯,因為行數(shù)不同
   a = np.array([[1, 2], [3, 4]])
   b = np.array([[5, 6, 7]])  # 形狀不匹配
   # result = np.hstack((a, b))  # ValueError

7. 與其他堆疊函數(shù)比較

- `vstack()`:垂直堆疊(按行)
- `dstack()`:深度堆疊(沿第三個軸)
- `concatenate()`:通用連接函數(shù),可以指定軸```python
比較不同堆疊方法

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

print("hstack (水平):")
print(np.hstack((a, b)))

print("vstack (垂直):")
print(np.vstack((a, b)))

print("dstack (深度):")
print(np.dstack((a, b)))

輸出

hstack (水平):
[[1 2 5 6]
 [3 4 7 8]]
vstack (垂直):
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
dstack (深度):
[[[1 5]
  [2 6]]

 [[3 7]
  [4 8]]]

8. 實際應(yīng)用示例

# 合并特征矩陣
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)

通過這個教程,你應(yīng)該能夠理解`hstack`函數(shù)的工作原理和適用場景。記住關(guān)鍵點是:**水平堆疊會增加數(shù)組的列數(shù)(第二個維度)**,并且所有輸入數(shù)組在除了第二個維度外的其他維度上必須具有相同的形狀。

到此這篇關(guān)于NumPy的hstack函數(shù)詳細(xì)教程的文章就介紹到這了,更多相關(guān)NumPy hstack函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

吐鲁番市| 宣化县| 宽甸| 阳西县| 金湖县| 翼城县| 华阴市| 正蓝旗| 泌阳县| 安西县| 万安县| 务川| 哈巴河县| 桃园县| 浦北县| 顺义区| 绥阳县| 郑州市| 中江县| 喀喇沁旗| 克东县| 韶关市| 娱乐| 浮梁县| 临汾市| 屏南县| 吉安市| 增城市| 景东| 彰化市| 宁河县| 青海省| 建湖县| 卢龙县| 汝城县| 道真| 通化市| 右玉县| 密山市| 濮阳市| 乌拉特前旗|