“`markdown
NumPy repeat() 详细介绍与实用案例
NumPy 是 Python 中用于科学计算的核心库,提供了高性能的多维数组对象以及处理这些数组的工具。在数据处理和操作中,我们经常需要重复数组中的元素或整个数组。NumPy 提供了 repeat() 函数,这是一个强大且灵活的工具,用于实现这一目的。
本文将详细介绍 NumPy repeat() 函数的用法、参数及其在不同场景下的实用案例。
什么是 np.repeat()?
numpy.repeat(a, repeats, axis=None) 函数用于根据指定的次数重复数组 a 中的元素。它可以沿着指定的轴重复元素,也可以将数组扁平化后重复。
语法
python
numpy.repeat(a, repeats, axis=None)
参数详解
a:array_like- 输入数组。可以是 NumPy 数组、列表、元组等可以转换为数组的对象。
repeats:int或array_likeofints- 指定重复的次数。
- 如果
repeats是一个整数,则a中的每个元素(或沿着指定轴的每个切片)都将重复该整数次。 - 如果
repeats是一个数组,它必须与a的长度在指定的axis上匹配,或者如果axis=None,则它必须与扁平化后的a的总元素数匹配。这意味着repeats[i]指定了a中第i个元素(或切片)的重复次数。
axis:int, 可选- 指定重复元素的轴。
- 如果
axis为None(默认值),输入数组a会首先被扁平化(flattened),然后所有元素按照扁平化后的顺序重复。 - 如果
axis是一个整数,例如0表示行,1表示列,那么重复操作将沿着该轴进行。
返回值
out:ndarray- 包含重复元素的数组,形状会根据
repeats和axis的值进行调整。
- 包含重复元素的数组,形状会根据
实用案例
让我们通过一系列示例来理解 np.repeat() 的不同用法。
1. 重复一维数组的元素
这是最简单的用法,repeat 将作用于数组的每个元素。
“`python
import numpy as np
arr_1d = np.array([1, 2, 3])
每个元素重复 2 次
result_int_repeats = np.repeat(arr_1d, 2)
print(“一维数组,每个元素重复 2 次:\n”, result_int_repeats)
输出: [1 1 2 2 3 3]
使用不同次数重复每个元素
repeats 数组的长度必须与 arr_1d 的长度相同
result_array_repeats = np.repeat(arr_1d, [1, 3, 2])
print(“\n一维数组,不同次数重复每个元素:\n”, result_array_repeats)
输出: [1 2 2 2 3 3]
“`
2. 重复二维数组的元素(axis=None)
当 axis=None 时,数组会被扁平化,然后按照扁平化后的顺序重复元素。
“`python
arr_2d = np.array([[1, 2], [3, 4]])
扁平化后,每个元素重复 3 次
result_flattened = np.repeat(arr_2d, 3, axis=None)
print(“\n二维数组,axis=None,每个元素重复 3 次:\n”, result_flattened)
输出: [1 1 1 2 2 2 3 3 3 4 4 4]
“`
3. 重复二维数组的行(axis=0)
当 axis=0 时,repeat 会沿着垂直方向(行)重复元素。
“`python
每个行重复 2 次
result_rows_int = np.repeat(arr_2d, 2, axis=0)
print(“\n二维数组,axis=0,每行重复 2 次:\n”, result_rows_int)
输出:
[[1 2]
[1 2]
[3 4]
[3 4]]
使用不同次数重复每行
repeats 数组的长度必须与 arr_2d 的行数相同
result_rows_array = np.repeat(arr_2d, [3, 1], axis=0)
print(“\n二维数组,axis=0,不同次数重复每行:\n”, result_rows_array)
输出:
[[1 2]
[1 2]
[1 2]
[3 4]]
“`
4. 重复二维数组的列(axis=1)
当 axis=1 时,repeat 会沿着水平方向(列)重复元素。
“`python
每个列重复 2 次
result_cols_int = np.repeat(arr_2d, 2, axis=1)
print(“\n二维数组,axis=1,每列重复 2 次:\n”, result_cols_int)
输出:
[[1 1 2 2]
[3 3 4 4]]
使用不同次数重复每列
repeats 数组的长度必须与 arr_2d 的列数相同
result_cols_array = np.repeat(arr_2d, [1, 3], axis=1)
print(“\n二维数组,axis=1,不同次数重复每列:\n”, result_cols_array)
输出:
[[1 2 2 2]
[3 4 4 4]]
“`
5. 三维数组的重复
对于更高维度的数组,axis 参数同样适用。
“`python
arr_3d = np.arange(1, 9).reshape(2, 2, 2)
print(“\n原始三维数组:\n”, arr_3d)
输出:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
沿着 axis=0 (第一个维度) 重复
result_3d_axis0 = np.repeat(arr_3d, 2, axis=0)
print(“\n三维数组,axis=0,重复 2 次:\n”, result_3d_axis0)
输出:
[[[1 2]
[3 4]]
[[1 2]
[3 4]]
[[5 6]
[7 8]]
[[5 6]
[7 8]]]
沿着 axis=2 (第三个维度) 重复
result_3d_axis2 = np.repeat(arr_3d, 3, axis=2)
print(“\n三维数组,axis=2,重复 3 次:\n”, result_3d_axis2)
输出:
[[[1 1 1 2 2 2]
[3 3 3 4 4 4]]
[[5 5 5 6 6 6]
[7 7 7 8 8 8]]]
“`
实际应用场景
1. 数据增强(Data Augmentation)
在机器学习和深度学习中,特别是图像处理任务,为了增加训练数据的多样性,我们可能需要对数据进行复制。虽然 repeat 不直接进行图像变换,但它可以用于扩展批次维度或特征维度。
例如,如果你有一个单张灰度图像,需要将其重复三次以模拟 RGB 图像的通道:
“`python
假设这是一个 10×10 的灰度图像
gray_image = np.random.randint(0, 256, size=(10, 10))
print(“\n原始灰度图像形状:”, gray_image.shape)
添加一个通道维度,然后沿着新维度重复 3 次
rgb_image = np.repeat(gray_image[:, :, np.newaxis], 3, axis=2)
print(“模拟 RGB 图像形状:”, rgb_image.shape)
输出: 模拟 RGB 图像形状: (10, 10, 3)
“`
2. 创建图案或网格
repeat() 可以用来创建重复的模式,这在生成测试数据或可视化时非常有用。
“`python
创建一个重复的行向量
pattern_row = np.array([0, 1])
repeated_pattern = np.repeat(pattern_row, 5)
print(“\n重复的行模式:\n”, repeated_pattern)
输出: [0 0 0 0 0 1 1 1 1 1]
创建一个棋盘格图案
small_block = np.array([[0, 1], [1, 0]])
checkerboard = np.repeat(np.repeat(small_block, 2, axis=0), 2, axis=1)
print(“\n棋盘格图案:\n”, checkerboard)
输出:
[[0 0 1 1]
[0 0 1 1]
[1 1 0 0]
[1 1 0 0]]
“`
3. 特征工程
在某些数据预处理任务中,你可能需要根据现有特征创建重复的新特征。
例如,你有一个表示类别的 ID 列表,并希望每个 ID 在某些操作中被视为多份。
“`python
category_ids = np.array([101, 102])
expanded_ids = np.repeat(category_ids, [2, 3]) # 101重复2次,102重复3次
print(“\n扩展的类别 ID:\n”, expanded_ids)
输出: [101 101 102 102 102]
“`
np.repeat() 与 np.tile() 的区别
np.repeat() 和 np.tile() 都用于重复数组,但它们的行为方式不同:
np.repeat(): 重复数组的 元素 或 切片。它会增加数组的长度(如果axis=None)或在指定轴上复制切片,从而改变数组的形状。np.tile(): 将整个数组作为 块 进行重复。它沿着指定的维度将数组平铺多次,创建更大的数组。
简单来说,repeat 更像是“放大”现有元素或切片,而 tile 更像是“铺设”整个模板。
“`python
arr = np.array([1, 2])
repeat
print(“np.repeat([1, 2], 3):\n”, np.repeat(arr, 3))
输出: [1 1 1 2 2 2]
tile
print(“np.tile([1, 2], 3):\n”, np.tile(arr, 3))
输出: [1 2 1 2 1 2]
arr_2d_tile = np.array([[1, 2], [3, 4]])
print(“\nnp.tile(arr_2d_tile, (2, 1)):\n”, np.tile(arr_2d_tile, (2, 1)))
(2, 1) 表示在行方向重复 2 次,列方向重复 1 次
输出:
[[1 2]
[3 4]
[1 2]
[3 4]]
“`
总结
NumPy 的 repeat() 函数是数据操作中一个非常有用的工具,它能够灵活地重复数组中的元素或切片。无论是简单地扩展一维数组,还是在多维数组中沿着特定轴进行复杂的重复操作,甚至结合不同重复次数的需求,repeat() 都能很好地胜任。理解其 axis 参数和 repeats 参数的用法是高效利用此函数的关键。在数据增强、模式生成和特征工程等场景中,np.repeat() 都能发挥重要的作用。
“`