hstack 是 NumPy 中的一个函数,用于水平堆叠数组。它沿着第二个轴(列方向) 将多个数组连接在一起。
import numpy as np
# 水平堆叠示例
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.hstack((a, b))
print(result) # 输出: [1 2 3 4 5 6]
numpy.hstack(tup)
参数:
tup:包含要堆叠数组的序列(元组或列表)返回值:
import numpy as np
# 一维数组
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr3 = np.array([7, 8, 9])
# 水平堆叠
result = np.hstack((arr1, arr2, arr3))
print("一维数组堆叠结果:")
print(result) # [1 2 3 4 5 6 7 8 9]
print("形状:", result.shape) # (9,)
# 二维数组
arr1 = np.array([[1, 2], [3, 4]]) # 形状: (2, 2)
arr2 = np.array([[5, 6], [7, 8]]) # 形状: (2, 2)
result = np.hstack((arr1, arr2))
print("二维数组堆叠结果:")
print(result)
"""
[[1 2 5 6]
[3 4 7 8]]
"""
print("形状:", result.shape) # (2, 4)
# 必须保证第一个轴(行数)相同
arr1 = np.array([[1, 2], [3, 4]]) # 形状: (2, 2)
arr2 = np.array([[5], [6]]) # 形状: (2, 1)
result = np.hstack((arr1, arr2))
print("混合形状堆叠结果:")
print(result)
"""
[[1 2 5]
[3 4 6]]
"""
print("形状:", result.shape) # (2, 3)
# 正确的例子 - 行数相同
arr1 = np.ones((3, 2)) # 3行2列
arr2 = np.zeros((3, 4)) # 3行4列
result = np.hstack((arr1, arr2)) # 形状: (3, 6)
# 错误的例子 - 行数不同
arr1 = np.ones((2, 3)) # 2行3列
arr2 = np.zeros((3, 3)) # 3行3列
# result = np.hstack((arr1, arr2)) # ValueError: 所有数组的维度必须相同
arr1 = np.array([[1, 1], [1, 1]])
arr2 = np.array([[2, 2], [2, 2]])
arr3 = np.array([[3, 3], [3, 3]])
result = np.hstack((arr1, arr2, arr3))
print("多数组堆叠:")
print(result)
"""
[[1 1 2 2 3 3]
[1 1 2 2 3 3]]
"""
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
h_result = np.hstack((arr1, arr2)) # 水平堆叠
print("hstack结果:", h_result) # [1 2 3 4 5 6]
v_result = np.vstack((arr1, arr2)) # 垂直堆叠
print("vstack结果:")
print(v_result)
"""
[[1 2 3]
[4 5 6]]
"""
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
# 使用 hstack
h_result = np.hstack((arr1, arr2))
# 使用 concatenate
c_result = np.concatenate((arr1, arr2), axis=1)
print("hstack结果:")
print(h_result)
print("\nconcatenate结果:")
print(c_result)
print("\n两个结果相同吗?", np.array_equal(h_result, c_result)) # True
# 假设有两个不同的特征集
features1 = np.random.randn(100, 5) # 100个样本,5个特征
features2 = np.random.randn(100, 3) # 100个样本,3个特征
# 合并特征
combined_features = np.hstack((features1, features2))
print(f"原始特征1形状: {features1.shape}")
print(f"原始特征2形状: {features2.shape}")
print(f"合并后形状: {combined_features.shape}") # (100, 8)
# 模拟RGB图像的三个通道
height, width = 256, 256
red_channel = np.random.randint(0, 256, (height, width), dtype=np.uint8)
green_channel = np.random.randint(0, 256, (height, width), dtype=np.uint8)
blue_channel = np.random.randint(0, 256, (height, width), dtype=np.uint8)
# 将三个单通道图像合并成多通道图像
rgb_image = np.hstack((red_channel.reshape(-1, 1),
green_channel.reshape(-1, 1),
blue_channel.reshape(-1, 1)))
rgb_image = rgb_image.reshape(height, width, 3)
print(f"红通道形状: {red_channel.shape}") # (256, 256)
print(f"RGB图像形状: {rgb_image.shape}") # (256, 256, 3)
# 原始数据
X = np.array([[1, 2], [3, 4], [5, 6]]) # 特征
y = np.array([[0], [1], [0]]) # 标签
# 添加偏置项(全1列)
ones = np.ones((X.shape[0], 1))
X_with_bias = np.hstack((ones, X))
print("添加偏置项后的特征矩阵:")
print(X_with_bias)
"""
[[1. 1. 2.]
[1. 3. 4.]
[1. 5. 6.]]
"""
# 错误示例
try:
arr1 = np.array([[1, 2], [3, 4]]) # 形状: (2, 2)
arr2 = np.array([[5, 6, 7]]) # 形状: (1, 3)
result = np.hstack((arr1, arr2))
except ValueError as e:
print(f"错误: {e}")
print("解决方法: 确保所有数组的行数相同")
# 错误示例
try:
arr1 = np.array([1, 2, 3]) # 一维,形状: (3,)
arr2 = np.array([[4, 5, 6]]) # 二维,形状: (1, 3)
result = np.hstack((arr1, arr2))
print(result.shape)
except Exception as e:
print(f"可能会出现问题: {e}")
print("建议: 统一数组维度")
import time
# 测试 hstack 性能
size = 10000
# 创建大数组
arr1 = np.random.randn(size, size)
arr2 = np.random.randn(size, size)
# 测试 hstack
start = time.time()
result = np.hstack((arr1, arr2))
hstack_time = time.time() - start
# 测试 concatenate
start = time.time()
result = np.concatenate((arr1, arr2), axis=1)
concat_time = time.time() - start
print(f"hstack 耗时: {hstack_time:.4f} 秒")
print(f"concatenate 耗时: {concat_time:.4f} 秒")
print("注意: hstack 内部调用 concatenate,性能基本相同")
np.concatenate(..., axis=1)
适用场景:合并特征、图像通道、数据预处理等
注意事项:确保维度匹配,注意内存使用
# 练习 1: 基本操作
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
# 尝试水平堆叠这两个数组
# 练习 2: 一维数组
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# 堆叠后会发生什么?
# 练习 3: 三个数组堆叠
arr1 = np.ones((3, 2))
arr2 = np.zeros((3, 2))
arr3 = np.full((3, 2), 5)
# 如何将它们水平堆叠?
# 答案
print("练习1结果:")
print(np.hstack((arr1, arr2)))
print("\n练习2结果:")
print(np.hstack((arr1, arr2)))
print("\n练习3结果:")
print(np.hstack((arr1, arr2, arr3)))