90个NumPy案例

  • 有多个条件时替换 Numpy 数组中的元素
    • 将所有大于 30 的元素替换为 0
    • 将大于 30 小于 50 的所有元素替换为 0
    • 给所有大于 40 的元素加 5
    • 用 Nan 替换数组中大于 25 的所有元素
    • 将数组中大于 25 的所有元素替换为 1,否则为 0
  • 在 Python 中找到 Numpy 数组的维度
  • 两个条件过滤 NumPy 数组
    • Example 1
    • Example 2
    • Example 3
    • Example 4
    • Example 5
  • 对最后一列求和
    • 第一列总和
    • 第二列总和
    • 第一列和第二列的总和
    • 最后一列的总和
  • 满足条件,则替换 Numpy 元素
    • 将所有大于 30 的元素替换为 0
    • 将大于 30 小于 50 的所有元素替换为 0
    • 给所有大于 40 的元素加 5
    • 用 Nan 替换数组中大于 25 的所有元素
    • 将数组中大于 25 的所有元素替换为 1,否则为 0
  • 从 Nump y数组中随机选择两行
    • Example 1
    • Example 2
    • Example 3
  • 以给定的精度漂亮地打印一个 Numpy 数组
    • Example 1
    • Example 2
    • Example 3
    • Example 4
    • Example 5
  • 提取 Numpy 矩阵的前 n 列
    • 列范围1
    • 列范围2
    • 列范围3
    • 特定列
    • 特定行和列
  • 从 NumPy 数组中删除值
    • Example 1
    • Example 2
    • Example 3
  • 将满足条件的项目替换为 Numpy 数组中的另一个值
    • 将所有大于 30 的元素替换为 0
    • 将大于 30 小于 50 的所有元素替换为 0
    • 给所有大于 40 的元素加 5
    • 用 Nan 替换数组中大于 25 的所有元素
    • 将数组中大于 25 的所有元素替换为 1,否则为 0
  • 对 NumPy 数组中的所有元素求和
  • 创建 3D NumPy 零数组
  • 计算 NumPy 数组中每一行的总和
  • 打印没有科学记数法的 NumPy 数组
  • 获取numpy数组中所有NaN值的索引列表
  • 检查 NumPy 数组中的所有元素都是 NaN
  • 将列表添加到 Python 中的 NumPy 数组
  • 在 Numpy 中抑制科学记数法
  • 将具有 12 个元素的一维数组转换为 3 维数组
    • Example 1
    • Example 2
    • Example 3
    • Example 4
  • 检查 NumPy 数组是否为空
  • 在 Python 中重塑 3D 数组
    • Example 1
    • Example 2
    • Example 3
    • Example 4
  • 在 Python 中重复 NumPy 数组中的一列
  • 在 NumPy 数组中找到跨维度的平均值
  • 检查 NumPy 数组中的 NaN 元素
  • 格式化 NumPy 数组的打印方式
    • Example 1
    • Example 2
    • Example 3
    • Example 4
    • Example 5
  • 乘以Numpy数组的每个元素
    • Example 1
    • Example 2
    • Example 3
    • Example 4
  • 在 NumPy 中生成随机数
    • Example 1
    • Example 2
    • Example 3
  • Numpy 将具有 8 个元素的一维数组转换为 Python 中的二维数组
    • 4 行 2 列
    • 2 行 4 列
  • 在 Python 中使用 numpy.all()
  • 将一维数组转换为二维数组
    • 4 行 2 列
    • 2 行 4 列
    • Example 3
    • 通过添加新轴将一维数组转换为二维数组
    • Example 5
  • 计算 NumPy 数组中唯一值的频率
  • 在一列中找到平均值
  • 在 Numpy 数组的长度、维度、大小
    • Example 1
    • Example 2
  • 在 NumPy 数组中找到最大值的索引
  • 按降序对 NumPy 数组进行排序
    • 按降序对 Numpy 进行排序
    • 按降序对 2D Numpy 进行排序
    • 按降序对 Numpy 进行排序
  • Numpy 从二维数组中获取随机的一组行
    • Example 1
    • Example 2
    • Example 3
  • 将 Numpy 数组转换为 JSON
  • 检查 NumPy 数组中是否存在值
  • 创建一个 3D NumPy 数组
  • 在numpy中将字符串数组转换为浮点数数组
  • 从 Python 的 numpy 数组中随机选择
    • Example 1
    • Example 2
    • Example 3
  • 不截断地打印完整的 NumPy 数组
  • 将 Numpy 转换为列表
  • 将字符串数组转换为浮点数数组
  • 计算 NumPy 数组中每一列的总和
  • 使用 Python 中的值创建 3D NumPy 数组
  • 计算不同长度的 Numpy 数组的平均值
  • 从 Numpy 数组中删除 nan 值
    • Example 1
    • Example 2
  • 向 NumPy 数组添加一列
  • 在 Numpy Array 中打印浮点值时如何抑制科学记数法
  • Numpy 将 1d 数组重塑为 1 列的 2d 数组
  • 初始化 NumPy 数组
  • 创建重复一行
  • 将 NumPy 数组附加到 Python 中的空数组
  • 找到 Numpy 数组的平均值
    • 计算每列的平均值
    • 计算每一行的平均值
    • 仅第一列的平均值
    • 仅第二列的平均值
  • 检测 NumPy 数组是否包含至少一个非数字值
  • 在 Python 中附加 NumPy 数组
  • 使用 numpy.any()
  • 获得 NumPy 数组的转置
  • 获取和设置NumPy数组的数据类型
  • 获得NumPy数组的形状
  • 获得 1、2 或 3 维 NumPy 数组
  • 重塑 NumPy 数组
  • 调整 NumPy 数组的大小
  • 将 List 或 Tuple 转换为 NumPy 数组
  • 使用 arange 函数创建 NumPy 数组
  • 使用 linspace() 创建 NumPy 数组
  • NumPy 日志空间数组示例
  • 创建 Zeros NumPy 数组
  • NumPy One 数组示例
  • NumPy 完整数组示例
  • NumPy Eye 数组示例
  • NumPy 生成随机数数组
  • NumPy 标识和对角线数组示例
  • NumPy 索引示例
  • 多维数组中的 NumPy 索引
  • NumPy 单维切片示例
  • NumPy 数组中的多维切片
  • 翻转 NumPy 数组的轴顺序
  • NumPy 数组的连接和堆叠
  • NumPy 数组的算术运算
  • NumPy 数组上的标量算术运算
  • NumPy 初等数学函数
  • NumPy Element Wise 数学运算
  • NumPy 聚合和统计函数
  • Where 函数的 NumPy 示例
  • Select 函数的 NumPy 示例
  • 选择函数的 NumPy 示例
  • NumPy 逻辑操作,用于根据给定条件从数组中选择性地选取值
  • 标准集合操作的 NumPy 示例

    有多个条件时替换 Numpy 数组中的元素

    将所有大于 30 的元素替换为 0

    ```python import numpy as np

the_array = np.array([49, 7, 44, 27, 13, 35, 71])

an_array = np.where(the_array > 30, 0, the_array) print(an_array)

  1. <a name="dY5xC"></a>
  2. #### OutPut
  3. ```python
  4. [ 0 7 0 27 13 0 0]

将大于 30 小于 50 的所有元素替换为 0

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where((the_array > 30) & (the_array < 50), 0, the_array)
  4. print(an_array)

OutPut

  1. [ 0 7 0 27 13 0 71]

给所有大于 40 的元素加 5

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where(the_array > 40, the_array + 5, the_array)
  4. print(an_array)

OutPut

  1. 0 A
  2. 1 A
  3. 2 A
  4. 3 A
  5. dtype: object

用 Nan 替换数组中大于 25 的所有元素

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where(the_array > 25, np.NaN, the_array)
  4. print(an_array)

OutPut

  1. [nan 7. nan nan 13. nan nan]

将数组中大于 25 的所有元素替换为 1,否则为 0

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.asarray([0 if val < 25 else 1 for val in the_array])
  4. print(an_array)

OutPut

  1. [1 0 1 1 0 1 1]

在 Python 中找到 Numpy 数组的维度

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. print(arr.ndim)
  4. arr = np.array([[1, 1, 1, 0], [0, 5, 0, 1], [2, 1, 3, 10]])
  5. print(arr.ndim)
  6. arr = np.array([[[1, 1, 1, 0], [0, 5, 0, 1], [2, 1, 3, 10]]])
  7. print(arr.ndim)

OutPut

  1. 1
  2. 2
  3. 3

两个条件过滤 NumPy 数组

Example 1

  1. import numpy as np
  2. the_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
  3. filter_arr = np.logical_and(np.greater(the_array, 3), np.less(the_array, 8))
  4. print(the_array[filter_arr])

OutPut

  1. [4 5 6 7]

Example 2

  1. import numpy as np
  2. the_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
  3. filter_arr = np.logical_or(the_array < 3, the_array == 4)
  4. print(the_array[filter_arr])

OutPut

  1. [1 2 4]

Example 3

  1. import numpy as np
  2. the_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
  3. filter_arr = np.logical_not(the_array > 1, the_array < 5)
  4. print(the_array[filter_arr])

OutPut

  1. [1]

Example 4

  1. import numpy as np
  2. the_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
  3. filter_arr = np.logical_or(the_array == 8, the_array < 5)
  4. print(the_array[filter_arr])

OutPut

  1. [1 2 3 4 8]

Example 5

  1. import numpy as np
  2. the_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
  3. filter_arr = np.logical_and(the_array == 8, the_array < 5)
  4. print(the_array[filter_arr])

OutPut

  1. []

对最后一列求和

第一列总和

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(4, 3)
  4. print(newarr)
  5. column_sums = newarr[:, 0].sum()
  6. print(column_sums)

OutPut

  1. [[ 1 2 3]
  2. [ 4 5 6]
  3. [ 7 8 9]
  4. [10 11 12]]
  5. 22

第二列总和

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(4, 3)
  4. print(newarr)
  5. column_sums = newarr[:, 1].sum()
  6. print(column_sums)

OutPut

  1. [[ 1 2 3]
  2. [ 4 5 6]
  3. [ 7 8 9]
  4. [10 11 12]]
  5. 26

第一列和第二列的总和

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(4, 3)
  4. print(newarr)
  5. column_sums = newarr[:, 0:2].sum()
  6. print(column_sums)

OutPut

  1. [[ 1 2 3]
  2. [ 4 5 6]
  3. [ 7 8 9]
  4. [10 11 12]]
  5. 48[]

最后一列的总和

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(4, 3)
  4. print(newarr)
  5. column_sums = newarr[:, -1].sum()
  6. print(column_sums)

OutPut

  1. [[ 1 2 3]
  2. [ 4 5 6]
  3. [ 7 8 9]
  4. [10 11 12]]
  5. 30

满足条件,则替换 Numpy 元素

将所有大于 30 的元素替换为 0

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where(the_array > 30, 0, the_array)
  4. print(an_array)

OutPut

  1. [ 0 7 0 27 13 0 0]

将大于 30 小于 50 的所有元素替换为 0

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where((the_array > 30) & (the_array < 50), 0, the_array)
  4. print(an_array)

OutPut

  1. [ 0 7 0 27 13 0 71][]

给所有大于 40 的元素加 5

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where(the_array > 40, the_array + 5, the_array)
  4. print(an_array)

OutPut

  1. [54 7 49 27 13 35 76]

用 Nan 替换数组中大于 25 的所有元素

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where(the_array > 25, np.NaN, the_array)
  4. print(an_array)

OutPut

  1. [nan 7. nan nan 13. nan nan]

将数组中大于 25 的所有元素替换为 1,否则为 0

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.asarray([0 if val < 25 else 1 for val in the_array])
  4. print(an_array)

OutPut

  1. [1 0 1 1 0 1 1]

从 Nump y数组中随机选择两行

Example 1

  1. import numpy as np
  2. # create 2D array
  3. the_array = np.arange(50).reshape((5, 10))
  4. # row manipulation
  5. np.random.shuffle(the_array)
  6. # display random rows
  7. rows = the_array[:2, :]
  8. print(rows)

OutPut

  1. [[10 11 12 13 14 15 16 17 18 19]
  2. [ 0 1 2 3 4 5 6 7 8 9]]

Example 2

  1. import random
  2. import numpy as np
  3. # create 2D array
  4. the_array = np.arange(16).reshape((4, 4))
  5. # row manipulation
  6. rows_id = random.sample(range(0, the_array.shape[1] - 1), 2)
  7. # display random rows
  8. rows = the_array[rows_id, :]
  9. print(rows)

OutPut

  1. [[ 4 5 6 7]
  2. [ 8 9 10 11]]

Example 3

  1. import numpy as np
  2. # create 2D array
  3. the_array = np.arange(16).reshape((4, 4))
  4. number_of_rows = the_array.shape[0]
  5. random_indices = np.random.choice(number_of_rows,
  6. size=2,
  7. replace=False)
  8. # display random rows
  9. rows = the_array[random_indices, :]
  10. print(rows)

OutPut

  1. [[ 4 5 6 7]
  2. [ 8 9 10 11]]

以给定的精度漂亮地打印一个 Numpy 数组

Example 1

  1. import numpy as np
  2. x = np.array([[1.1, 0.9, 1e-6]] * 3)
  3. print(x)
  4. print(np.array_str(x, precision=1, suppress_small=True))

OutPut

  1. [[1.1e+00 9.0e-01 1.0e-06]
  2. [1.1e+00 9.0e-01 1.0e-06]
  3. [1.1e+00 9.0e-01 1.0e-06]]
  4. [[1.1 0.9 0. ]
  5. [1.1 0.9 0. ]
  6. [1.1 0.9 0. ]]

Example 2

  1. import numpy as np
  2. x = np.random.random(10)
  3. print(x)
  4. np.set_printoptions(precision=3)
  5. print(x)

OutPut

  1. [0.53828153 0.75848226 0.50046312 0.94723558 0.50415632 0.13899663
  2. 0.80301141 0.40887872 0.24837485 0.83008548]
  3. [0.538 0.758 0.5 0.947 0.504 0.139 0.803 0.409 0.248 0.83 ]

Example 3

  1. import numpy as np
  2. x = np.array([[1.1, 0.9, 1e-6]] * 3)
  3. print(x)
  4. np.set_printoptions(suppress=True)
  5. print(x)

OutPut

  1. [[1.1e+00 9.0e-01 1.0e-06]
  2. [1.1e+00 9.0e-01 1.0e-06]
  3. [1.1e+00 9.0e-01 1.0e-06]]
  4. [[1.1 0.9 0.000001]
  5. [1.1 0.9 0.000001]
  6. [1.1 0.9 0.000001]]

Example 4

  1. import numpy as np
  2. x = np.array([[1.1, 0.9, 1e-6]] * 3)
  3. print(x)
  4. np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
  5. print(x)

OutPut

  1. [[1.1e+00 9.0e-01 1.0e-06]
  2. [1.1e+00 9.0e-01 1.0e-06]
  3. [1.1e+00 9.0e-01 1.0e-06]]
  4. [[ 1.100 0.900 0.000]
  5. [ 1.100 0.900 0.000]
  6. [ 1.100 0.900 0.000]]

Example 5

  1. import numpy as np
  2. x = np.random.random((3, 3)) * 9
  3. print(np.array2string(x, formatter={'float_kind': '{0:.3f}'.format}))

OutPut

  1. [[3.479 1.490 5.674]
  2. [6.043 7.025 1.597]
  3. [0.261 8.530 2.298]]

提取 Numpy 矩阵的前 n 列

列范围1

  1. import numpy as np
  2. the_arr = np.array([[0, 1, 2, 3, 5, 6, 7, 8],
  3. [4, 5, 6, 7, 5, 3, 2, 5],
  4. [8, 9, 10, 11, 4, 5, 3, 5]])
  5. print(the_arr[:, 1:5])

OutPut

  1. [[ 1 2 3 5]
  2. [ 5 6 7 5]
  3. [ 9 10 11 4]]

列范围2

  1. import numpy as np
  2. the_arr = np.array([[0, 1, 2, 3, 5, 6, 7, 8],
  3. [4, 5, 6, 7, 5, 3, 2, 5],
  4. [8, 9, 10, 11, 4, 5, 3, 5]])
  5. print(the_arr[:, np.r_[0:1, 5]])

OutPut

  1. [[ 0 2 3 5]
  2. [ 4 6 7 5]
  3. [ 8 10 11 4]]

列范围3

  1. import numpy as np
  2. the_arr = np.array([[0, 1, 2, 3, 5, 6, 7, 8],
  3. [4, 5, 6, 7, 5, 3, 2, 5],
  4. [8, 9, 10, 11, 4, 5, 3, 5]])
  5. print(the_arr[:, np.r_[:1, 3, 7:8]])

OutPut

  1. [[ 0 3 8]
  2. [ 4 7 5]
  3. [ 8 11 5]]

特定列

  1. import numpy as np
  2. the_arr = np.array([[0, 1, 2, 3, 5, 6, 7, 8],
  3. [4, 5, 6, 7, 5, 3, 2, 5],
  4. [8, 9, 10, 11, 4, 5, 3, 5]])
  5. print(the_arr[:, 1])

OutPut

  1. [1 5 9]

特定行和列

  1. import numpy as np
  2. the_arr = np.array([[0, 1, 2, 3, 5, 6, 7, 8],
  3. [4, 5, 6, 7, 5, 3, 2, 5],
  4. [8, 9, 10, 11, 4, 5, 3, 5]])
  5. print(the_arr[0:2, 1:3])

OutPut

  1. [[1 2]
  2. [5 6]]

从 NumPy 数组中删除值

Example 1

  1. import numpy as np
  2. the_array = np.array([[1, 2], [3, 4]])
  3. print(the_array)
  4. the_array = np.delete(the_array, [1, 2])
  5. print(the_array)

OutPut

  1. [[1 2]
  2. [3 4]]
  3. [1 4]

Example 2

  1. import numpy as np
  2. the_array = np.array([1, 2, 3, 4])
  3. print(the_array)
  4. the_array = np.delete(the_array, np.where(the_array == 2))
  5. print(the_array)

OutPut

  1. [1 2 3 4]
  2. [1 3 4]

Example 3

  1. import numpy as np
  2. the_array = np.array([[1, 2], [3, 4]])
  3. print(the_array)
  4. the_array = np.delete(the_array, np.where(the_array == 3))
  5. print(the_array)

OutPut

  1. [[1 2]
  2. [3 4]]
  3. [3 4]

将满足条件的项目替换为 Numpy 数组中的另一个值

将所有大于 30 的元素替换为 0

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where(the_array > 30, 0, the_array)
  4. print(an_array)

OutPut

  1. [ 0 7 0 27 13 0 0]

将大于 30 小于 50 的所有元素替换为 0

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where((the_array > 30) & (the_array < 50), 0, the_array)
  4. print(an_array)

OutPut

  1. [ 0 7 0 27 13 0 71]

给所有大于 40 的元素加 5

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where(the_array > 40, the_array + 5, the_array)
  4. print(an_array)

OutPut

  1. [54 7 49 27 13 35 76]

用 Nan 替换数组中大于 25 的所有元素

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.where(the_array > 25, np.NaN, the_array)
  4. print(an_array)

OutPut

  1. [nan 7. nan nan 13. nan nan]

将数组中大于 25 的所有元素替换为 1,否则为 0

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. an_array = np.asarray([0 if val < 25 else 1 for val in the_array])
  4. print(an_array)

OutPut

  1. [1 0 1 1 0 1 1]

如何根据 Pandas 列中的值从 DataFrame 中选择或过滤行

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(4, 3)
  4. column_sums = newarr[:, :].sum()
  5. print(column_sums)

OutPut

  1. 78

创建 3D NumPy 零数组

  1. import numpy as np
  2. the_3d_array = np.zeros((2, 2, 2))
  3. print(the_3d_array)

OutPut

  1. [[[0. 0.]
  2. [0. 0.]]
  3. [[0. 0.]
  4. [0. 0.]]]

计算 NumPy 数组中每一行的总和

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(4, 3)
  4. print(newarr)
  5. column_sums = newarr.sum(axis=1)
  6. print(column_sums)

OutPut

  1. [[ 1 2 3]
  2. [ 4 5 6]
  3. [ 7 8 9]
  4. [10 11 12]]
  5. [ 6 15 24 33]

打印没有科学记数法的 NumPy 数组

  1. import numpy as np
  2. np.set_printoptions(suppress=True,
  3. formatter={'float_kind': '{:f}'.format})
  4. the_array = np.array([3.74, 5162, 13683628846.64, 12783387559.86, 1.81])
  5. print(the_array)

OutPut

  1. [3.740000 5162.000000 13683628846.639999 12783387559.860001 1.810000]

获取numpy数组中所有NaN值的索引列表

  1. import numpy as np
  2. the_array = np.array([np.nan, 2, 3, 4])
  3. array_has_nan = np.isnan(the_array)
  4. print(array_has_nan)

OutPut

  1. [ True False False False]

检查 NumPy 数组中的所有元素都是 NaN

  1. import numpy as np
  2. the_array = np.array([np.nan, 2, 3, 4])
  3. array_has_nan = np.isnan(the_array).all()
  4. print(array_has_nan)
  5. the_array = np.array([np.nan, np.nan, np.nan, np.nan])
  6. array_has_nan = np.isnan(the_array).all()
  7. print(array_has_nan)

OutPut

  1. False
  2. True

将列表添加到 Python 中的 NumPy 数组

  1. import numpy as np
  2. the_array = np.array([[1, 2], [3, 4]])
  3. columns_to_append = [5, 6]
  4. the_array = np.insert(the_array, 2, columns_to_append, axis=1)
  5. print(the_array)

OutPut

  1. [[1 2 5]
  2. [3 4 6]]

在 Numpy 中抑制科学记数法

  1. import numpy as np
  2. np.set_printoptions(suppress=True,
  3. formatter={'float_kind': '{:f}'.format})
  4. the_array = np.array([3.74, 5162, 13683628846.64, 12783387559.86, 1.81])
  5. print(the_array)

OutPut

  1. [3.740000 5162.000000 13683628846.639999 12783387559.860001 1.810000]

将具有 12 个元素的一维数组转换为 3 维数组

Example 1

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(2, 3, 2)
  4. print(newarr)

OutPut

  1. [[[ 1 2]
  2. [ 3 4]
  3. [ 5 6]]
  4. [[ 7 8]
  5. [ 9 10]
  6. [11 12]]]

Example 2

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(3, 2, 2)
  4. print(newarr)

OutPut

  1. [[[ 1 2]
  2. [ 3 4]]
  3. [[ 5 6]
  4. [ 7 8]]
  5. [[ 9 10]
  6. [11 12]]]

Example 3

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(3, 2, 2).transpose()
  4. print(newarr)

OutPut

  1. [[[ 1 5 9]
  2. [ 3 7 11]]
  3. [[ 2 6 10]
  4. [ 4 8 12]]][[[ 1 2]
  5. [ 3 4]]
  6. [[ 5 6]
  7. [ 7 8]]
  8. [[ 9 10]
  9. [11 12]]]

Example 4

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(-1, 2).T.reshape(-1, 3, 4)
  4. print(newarr)

OutPut

  1. [[[ 1 3 5 7]
  2. [ 9 11 2 4]
  3. [ 6 8 10 12]]]

检查 NumPy 数组是否为空

  1. import numpy as np
  2. the_array = np.array([])
  3. is_empty = the_array.size == 0
  4. print(is_empty)
  5. the_array = np.array([1, 2, 3])
  6. is_empty = the_array.size == 0
  7. print(is_empty)

OutPut

  1. True
  2. False

在 Python 中重塑 3D 数组

Example 1

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(2, 3, 2)
  4. print(newarr)

OutPut

  1. [[[ 1 2]
  2. [ 3 4]
  3. [ 5 6]]
  4. [[ 7 8]
  5. [ 9 10]
  6. [11 12]]]

Example 2

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(3, 2, 2)
  4. print(newarr)

OutPut

  1. [[[ 1 2]
  2. [ 3 4]]
  3. [[ 5 6]
  4. [ 7 8]]
  5. [[ 9 10]
  6. [11 12]]]

Example 3

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(3, 2, 2).transpose()
  4. print(newarr)

OutPut

  1. [[[ 1 5 9]
  2. [ 3 7 11]]
  3. [[ 2 6 10]
  4. [ 4 8 12]]]

Example 4

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(-1, 2).T.reshape(-1, 3, 4)
  4. print(newarr)

OutPut

  1. [[[ 1 3 5 7]
  2. [ 9 11 2 4]
  3. [ 6 8 10 12]]]

在 Python 中重复 NumPy 数组中的一列

  1. import numpy as np
  2. the_array = np.array([1, 2, 3])
  3. repeat = 3
  4. new_array = np.transpose([the_array] * repeat)
  5. print(new_array)

OutPut

  1. [[1 1 1]
  2. [2 2 2]
  3. [3 3 3]]

在 NumPy 数组中找到跨维度的平均值

  1. import numpy as np
  2. the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
  3. mean_array = the_array.mean(axis=0)
  4. print(mean_array)

OutPut

  1. [3. 4. 5. 6.]

检查 NumPy 数组中的 NaN 元素

  1. import numpy as np
  2. the_array = np.array([np.nan, 2, 3, 4])
  3. array_has_nan = np.isnan(the_array).any()
  4. print(array_has_nan)
  5. the_array = np.array([1, 2, 3, 4])
  6. array_has_nan = np.isnan(the_array).any()
  7. print(array_has_nan)

OutPut

  1. True
  2. False

格式化 NumPy 数组的打印方式

Example 1

  1. import numpy as np
  2. x = np.array([[1.1, 0.9, 1e-6]] * 3)
  3. print(x)
  4. print(np.array_str(x, precision=1, suppress_small=True))

OutPut

  1. [[1.1e+00 9.0e-01 1.0e-06]
  2. [1.1e+00 9.0e-01 1.0e-06]
  3. [1.1e+00 9.0e-01 1.0e-06]]
  4. [[1.1 0.9 0. ]
  5. [1.1 0.9 0. ]
  6. [1.1 0.9 0. ]]

Example 2

  1. import numpy as np
  2. x = np.random.random(10)
  3. print(x)
  4. np.set_printoptions(precision=3)
  5. print(x)

OutPut

  1. [0.53828153 0.75848226 0.50046312 0.94723558 0.50415632 0.13899663
  2. 0.80301141 0.40887872 0.24837485 0.83008548]
  3. [0.538 0.758 0.5 0.947 0.504 0.139 0.803 0.409 0.248 0.83 ]

Example 3

  1. import numpy as np
  2. x = np.array([[1.1, 0.9, 1e-6]] * 3)
  3. print(x)
  4. np.set_printoptions(suppress=True)
  5. print(x)

OutPut

  1. [[1.1e+00 9.0e-01 1.0e-06]
  2. [1.1e+00 9.0e-01 1.0e-06]
  3. [1.1e+00 9.0e-01 1.0e-06]]
  4. [[1.1 0.9 0.000001]
  5. [1.1 0.9 0.000001]
  6. [1.1 0.9 0.000001]]

Example 4

  1. import numpy as np
  2. x = np.array([[1.1, 0.9, 1e-6]] * 3)
  3. print(x)
  4. np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
  5. print(x)

OutPut

  1. [[1.1e+00 9.0e-01 1.0e-06]
  2. [1.1e+00 9.0e-01 1.0e-06]
  3. [1.1e+00 9.0e-01 1.0e-06]]
  4. [[ 1.100 0.900 0.000]
  5. [ 1.100 0.900 0.000]
  6. [ 1.100 0.900 0.000]]

Example 5

  1. import numpy as np
  2. x = np.random.random((3, 3)) * 9
  3. print(np.array2string(x, formatter={'float_kind': '{0:.3f}'.format}))

OutPut

  1. [[3.479 1.490 5.674]
  2. [6.043 7.025 1.597]
  3. [0.261 8.530 2.298]]

乘以Numpy数组的每个元素

Example 1

  1. import numpy as np
  2. the_array = np.array([[1, 2, 3], [1, 2, 3]])
  3. prod = np.prod(the_array)
  4. print(prod)

OutPut

  1. 36

Example 2

  1. import numpy as np
  2. the_array = np.array([[1, 2, 3], [1, 2, 3]])
  3. prod = np.prod(the_array, 0)
  4. print(prod)

OutPut

  1. [1 4 9]

Example 3

  1. import numpy as np
  2. the_array = np.array([[1, 2, 3], [1, 2, 3]])
  3. prod = np.prod(the_array, 1)
  4. print(prod)

OutPut

  1. [6, 6]

Example 4

  1. import numpy as np
  2. the_array = np.array([1, 2, 3])
  3. prod = np.prod(the_array)
  4. print(prod)

OutPut

  1. 6

在 NumPy 中生成随机数

Example 1

  1. import numpy as np
  2. # create 2D array
  3. the_array = np.arange(50).reshape((5, 10))
  4. # row manipulation
  5. np.random.shuffle(the_array)
  6. # display random rows
  7. rows = the_array[:2, :]
  8. print(rows)

OutPut

  1. [[10 11 12 13 14 15 16 17 18 19]
  2. [ 0 1 2 3 4 5 6 7 8 9]]

Example 2

  1. import random
  2. import numpy as np
  3. # create 2D array
  4. the_array = np.arange(16).reshape((4, 4))
  5. # row manipulation
  6. rows_id = random.sample(range(0, the_array.shape[1] - 1), 2)
  7. # display random rows
  8. rows = the_array[rows_id, :]
  9. print(rows)

OutPut

  1. [[ 4 5 6 7]
  2. [ 8 9 10 11]]

Example 3

  1. import numpy as np
  2. # create 2D array
  3. the_array = np.arange(16).reshape((4, 4))
  4. number_of_rows = the_array.shape[0]
  5. random_indices = np.random.choice(number_of_rows,
  6. size=2,
  7. replace=False)
  8. # display random rows
  9. rows = the_array[random_indices, :]
  10. print(rows)

OutPut

  1. [[ 4 5 6 7]
  2. [ 8 9 10 11]]

Numpy 将具有 8 个元素的一维数组转换为 Python 中的二维数组

4 行 2 列

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. newarr = arr.reshape(4, 2)
  4. print(newarr)

OutPut

  1. [[1 2]
  2. [3 4]
  3. [5 6]
  4. [7 8]]

2 行 4 列

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. newarr = arr.reshape(2, 4)
  4. print(newarr)

OutPut

  1. [[1 2 3 4]
  2. [5 6 7 8]]

在 Python 中使用 numpy.all()

  1. import numpy as np
  2. thelist = [[True, True], [True, True]]
  3. thebool = np.all(thelist)
  4. print(thebool)
  5. thelist = [[False, False], [False, False]]
  6. thebool = np.all(thelist)
  7. print(thebool)
  8. thelist = [[True, False], [True, False]]
  9. thebool = np.all(thelist)
  10. print(thebool)

OutPut

  1. True

将一维数组转换为二维数组

4 行 2 列

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. newarr = arr.reshape(4, 2)
  4. print(newarr)

OutPut

  1. [[1 2]
  2. [3 4]
  3. [5 6]
  4. [7 8]]

2 行 4 列

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. newarr = arr.reshape(2, 4)
  4. print(newarr)

OutPut

  1. [[1 2 3 4]
  2. [5 6 7 8]]

Example 3

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. newarr = np.reshape(arr, (-1, 2))
  4. print(newarr)

OutPut

  1. [[1 2]
  2. [3 4]
  3. [5 6]
  4. [7 8]]

通过添加新轴将一维数组转换为二维数组

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. newarr = np.reshape(arr, (1, arr.size))
  4. print(newarr)

OutPut

  1. [[1 2 3 4 5 6 7 8]]

一维数组转为指定元素长度的二维数组

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. newarr = np.reshape(arr, (-1, 4))
  4. print(newarr)

OutPut

  1. [[1 2 3 4]
  2. [5 6 7 8]]

计算 NumPy 数组中唯一值的频率

  1. import numpy as np
  2. the_array = np.array([9, 7, 4, 7, 3, 5, 9])
  3. frequencies = np.asarray((np.unique(the_array, return_counts=True))).T
  4. print(frequencies)

OutPut

  1. [[3 1]
  2. [4 1]
  3. [5 1]
  4. [7 2]
  5. [9 2]]

在一列中找到平均值

  1. import numpy as np
  2. the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
  3. mean_array = the_array.mean(axis=0)
  4. print(mean_array)

OutPut

  1. [3. 4. 5. 6.]

在 Numpy 数组的长度、维度、大小

Example 1

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. print(arr.ndim)
  4. print(arr.shape)
  5. arr = np.array([[1, 1, 1, 0], [0, 5, 0, 1], [2, 1, 3, 10]])
  6. print(arr.ndim)
  7. print(arr.shape)
  8. arr = np.array([[[1, 1, 1, 0], [0, 5, 0, 1], [2, 1, 3, 10]]])
  9. print(arr.ndim)
  10. print(arr.shape)

OutPut

  1. 1
  2. (12,)
  3. 2
  4. (3, 4)
  5. 3
  6. (1, 3, 4)

Example 2

  1. import numpy as np
  2. arr = np.array([[1, 1, 1, 0], [0, 5, 0, 1], [2, 1, 3, 10]])
  3. print(np.info(arr))

OutPut

  1. class: ndarray
  2. shape: (3, 4)
  3. strides: (16, 4)
  4. itemsize: 4
  5. aligned: True
  6. contiguous: True
  7. fortran: False
  8. data pointer: 0x25da9fd5710
  9. byteorder: little
  10. byteswap: False
  11. type: int32
  12. None

在 NumPy 数组中找到最大值的索引

  1. import numpy as np
  2. the_array = np.array([11, 22, 53, 14, 15])
  3. max_index_col = np.argmax(the_array, axis=0)
  4. print(max_index_col)

OutPut

  1. 2

按降序对 NumPy 数组进行排序

按降序对 Numpy 进行排序

  1. import numpy as np
  2. the_array = np.array([49, 7, 44, 27, 13, 35, 71])
  3. sort_array = np.sort(the_array)[::-1]
  4. print(sort_array)

OutPut

  1. [71 49 44 35 27 13 7]

按降序对 2D Numpy 进行排序

  1. import numpy as np
  2. the_array = np.array([[49, 7, 4], [27, 13, 35]])
  3. sort_array = np.sort(the_array)[::1]
  4. print(sort_array)

OutPut

  1. [[ 4 7 49]
  2. [13 27 35]]

按降序对 Numpy 进行排序

  1. import numpy as np
  2. the_array = np.array([[49, 7, 4], [27, 13, 35], [12, 3, 5]])
  3. a_idx = np.argsort(-the_array)
  4. sort_array = np.take_along_axis(the_array, a_idx, axis=1)
  5. print(sort_array)

OutPut

  1. [[49 7 4]
  2. [35 27 13]
  3. [12 5 3]]

Numpy 从二维数组中获取随机的一组行

Example 1

  1. import numpy as np
  2. # create 2D array
  3. the_array = np.arange(50).reshape((5, 10))
  4. # row manipulation
  5. np.random.shuffle(the_array)
  6. # display random rows
  7. rows = the_array[:2, :]
  8. print(rows)

OutPut

  1. [[10 11 12 13 14 15 16 17 18 19]
  2. [ 0 1 2 3 4 5 6 7 8 9]]

Example 2

  1. import random
  2. import numpy as np
  3. # create 2D array
  4. the_array = np.arange(16).reshape((4, 4))
  5. # row manipulation
  6. rows_id = random.sample(range(0, the_array.shape[1] - 1), 2)
  7. # display random rows
  8. rows = the_array[rows_id, :]
  9. print(rows)

OutPut

  1. [[ 4 5 6 7]
  2. [ 8 9 10 11]]

Example 3

  1. import numpy as np
  2. # create 2D array
  3. the_array = np.arange(16).reshape((4, 4))
  4. number_of_rows = the_array.shape[0]
  5. random_indices = np.random.choice(number_of_rows,
  6. size=2,
  7. replace=False)
  8. # display random rows
  9. rows = the_array[random_indices, :]
  10. print(rows)

OutPut

  1. [[ 4 5 6 7]
  2. [ 8 9 10 11]]

将 Numpy 数组转换为 JSON

  1. import numpy as np
  2. the_array = np.array([[49, 7, 44], [27, 13, 35], [27, 13, 35]])
  3. lists = the_array.tolist()
  4. print([{'x': x[0], 'y': x[1], 'z': x[2]} for i, x in enumerate(lists)])

OutPut

  1. [{'x': 49, 'y': 7, 'z': 44}, {'x': 27, 'y': 13, 'z': 35}, {'x': 27, 'y': 13, 'z': 35}]

检查 NumPy 数组中是否存在值

  1. import numpy as np
  2. the_array = np.array([[1, 2], [3, 4]])
  3. n = 3
  4. if n in the_array:
  5. print(True)
  6. else:
  7. print(False)

OutPut

  1. True
  2. False

创建一个 3D NumPy 数组

  1. import numpy as np
  2. the_3d_array = np.ones((2, 2, 2))
  3. print(the_3d_array)

OutPut

  1. [[[1. 1.]
  2. [1. 1.]]
  3. [[1. 1.]
  4. [1. 1.]]]

在numpy中将字符串数组转换为浮点数数组

  1. import numpy as np
  2. string_arr = np.array(['1.1', '2.2', '3.3'])
  3. float_arr = string_arr.astype(np.float64)
  4. print(float_arr)

OutPut

  1. [1.1 2.2 3.3]

从 Python 的 numpy 数组中随机选择

Example 1

  1. import numpy as np
  2. # create 2D array
  3. the_array = np.arange(50).reshape((5, 10))
  4. # row manipulation
  5. np.random.shuffle(the_array)
  6. # display random rows
  7. rows = the_array[:2, :]
  8. print(rows)

OutPut

  1. [[10 11 12 13 14 15 16 17 18 19]
  2. [ 0 1 2 3 4 5 6 7 8 9]]

Example 2

  1. import random
  2. import numpy as np
  3. # create 2D array
  4. the_array = np.arange(16).reshape((4, 4))
  5. # row manipulation
  6. rows_id = random.sample(range(0, the_array.shape[1] - 1), 2)
  7. # display random rows
  8. rows = the_array[rows_id, :]
  9. print(rows)

OutPut

  1. [[ 4 5 6 7]
  2. [ 8 9 10 11]]

Example 3

  1. import numpy as np
  2. # create 2D array
  3. the_array = np.arange(16).reshape((4, 4))
  4. number_of_rows = the_array.shape[0]
  5. random_indices = np.random.choice(number_of_rows,
  6. size=2,
  7. replace=False)
  8. # display random rows
  9. rows = the_array[random_indices, :]
  10. print(rows)

OutPut

  1. [[ 4 5 6 7]
  2. [ 8 9 10 11]]

不截断地打印完整的 NumPy 数组

  1. import numpy as np
  2. np.set_printoptions(threshold=np.inf)
  3. the_array = np.arange(100)
  4. print(the_array)

OutPut

  1. [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
  2. 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  3. 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  4. 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  5. 96 97 98 99]

将 Numpy 转换为列表

  1. import numpy as np
  2. the_array = np.array([[1, 2], [3, 4]])
  3. print(the_array.tolist())

OutPut

  1. [[1, 2], [3, 4]]

将字符串数组转换为浮点数数组

  1. import numpy as np
  2. string_arr = np.array(['1.1', '2.2', '3.3'])
  3. float_arr = string_arr.astype(np.float64)
  4. print(float_arr)

OutPut

  1. [1.1 2.2 3.3]

计算 NumPy 数组中每一列的总和

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  3. newarr = arr.reshape(4, 3)
  4. print(newarr)
  5. column_sums = newarr.sum(axis=0)
  6. print(column_sums)

OutPut

  1. [[ 1 2 3]
  2. [ 4 5 6]
  3. [ 7 8 9]
  4. [10 11 12]]
  5. [22 26 30]

使用 Python 中的值创建 3D NumPy 数组

  1. import numpy as np
  2. the_3d_array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
  3. print(the_3d_array)

OutPut

  1. [[[1 2]
  2. [3 4]]
  3. [[5 6]
  4. [7 8]]]

计算不同长度的 Numpy 数组的平均值

  1. import numpy as np
  2. x = np.array([[1, 2], [3, 4]])
  3. y = np.array([[1, 2, 3], [3, 4, 5]])
  4. z = np.array([[7], [8]])
  5. arr = np.ma.empty((2, 3, 3))
  6. arr.mask = True
  7. arr[:x.shape[0], :x.shape[1], 0] = x
  8. arr[:y.shape[0], :y.shape[1], 1] = y
  9. arr[:z.shape[0], :z.shape[1], 2] = z
  10. print(arr.mean(axis=2))

OutPut

  1. [[3.0 2.0 3.0]
  2. [4.666666666666667 4.0 5.0]]

从 Numpy 数组中删除 nan 值

Example 1

  1. import numpy as np
  2. x = np.array([np.nan, 2, 3, 4])
  3. x = x[~np.isnan(x)]
  4. print(x)

OutPut

  1. [2. 3. 4.]

Example 2

  1. import numpy as np
  2. x = np.array([
  3. [5, np.nan],
  4. [np.nan, 0],
  5. [1, 2],
  6. [3, 4]
  7. ])
  8. x = x[~np.isnan(x).any(axis=1)]
  9. print(x)

OutPut

  1. [[1. 2.]
  2. [3. 4.]]

向 NumPy 数组添加一列

  1. import numpy as np
  2. the_array = np.array([[1, 2], [3, 4]])
  3. columns_to_append = np.array([[5], [6]])
  4. the_array = np.append(the_array, columns_to_append, 1)
  5. print(the_array)

OutPut

  1. [[1 2 5]
  2. [3 4 6]]

在 Numpy Array 中打印浮点值时如何抑制科学记数法

  1. import numpy as np
  2. np.set_printoptions(suppress=True,
  3. formatter={'float_kind': '{:f}'.format})
  4. the_array = np.array([3.74, 5162, 13683628846.64, 12783387559.86, 1.81])
  5. print(the_array)

OutPut

  1. [3.740000 5162.000000 13683628846.639999 12783387559.860001 1.810000]

Numpy 将 1d 数组重塑为 1 列的 2d 数组

  1. import numpy as np
  2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. newarr = arr.reshape(arr.shape[0], -1)
  4. print(newarr)

OutPut

  1. [[1]
  2. [2]
  3. [3]
  4. [4]
  5. [5]
  6. [6]
  7. [7]
  8. [8]]

初始化 NumPy 数组

  1. import numpy as np
  2. thearray = np.array([[1, 2], [3, 4], [5, 6]])
  3. print(thearray)

OutPut

  1. [[1 2]
  2. [3 4]
  3. [5 6]]

创建重复一行

  1. import numpy as np
  2. the_array = np.array([1, 2, 3])
  3. repeat = 3
  4. new_array = np.tile(the_array, (repeat, 1))
  5. print(new_array)

OutPut

  1. [[1 2 3]
  2. [1 2 3]
  3. [1 2 3]]

将 NumPy 数组附加到 Python 中的空数组

  1. import numpy as np
  2. the_array = np.array([1, 2, 3, 4])
  3. empty_array = np.array([])
  4. new_array = np.append(empty_array, the_array)
  5. print(new_array)

OutPut

  1. [1. 2. 3. 4.]

找到 Numpy 数组的平均值

计算每列的平均值

  1. import numpy as np
  2. the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
  3. mean_array = the_array.mean(axis=0)
  4. print(mean_array)

OutPut

  1. [3. 4. 5. 6.]

计算每一行的平均值

  1. import numpy as np
  2. the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
  3. mean_array = the_array.mean(axis=1)
  4. print(mean_array)

OutPut

  1. [2.5 6.5]

仅第一列的平均值

  1. import numpy as np
  2. the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
  3. mean_array = the_array[:, 0].mean()
  4. print(mean_array)

OutPut

  1. 3.0

仅第二列的平均值

  1. import numpy as np
  2. the_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
  3. mean_array = the_array[:, 0].mean()
  4. print(mean_array)

OutPut

  1. 4.0

检测 NumPy 数组是否包含至少一个非数字值

  1. import numpy as np
  2. the_array = np.array([np.nan, 2, 3, 4])
  3. array_has_nan = np.isnan(the_array).any()
  4. print(array_has_nan)
  5. the_array = np.array([1, 2, 3, 4])
  6. array_has_nan = np.isnan(the_array).any()
  7. print(array_has_nan)

OutPut

  1. True
  2. False

在 Python 中附加 NumPy 数组

  1. import numpy as np
  2. the_array = np.array([[0, 1], [2, 3]])
  3. row_to_append = np.array([[4, 5]])
  4. the_array = np.append(the_array, row_to_append, 0)
  5. print(the_array)
  6. print('*' * 10)
  7. columns_to_append = np.array([[7], [8], [9]])
  8. the_array = np.append(the_array, columns_to_append, 1)
  9. print(the_array)

OutPut

  1. [[0 1]
  2. [2 3]
  3. [4 5]]
  4. **********
  5. [[0 1 7]
  6. [2 3 8]
  7. [4 5 9]]

使用 numpy.any()

  1. import numpy as np
  2. thearr = [[True, False], [True, True]]
  3. thebool = np.any(thearr)
  4. print(thebool)
  5. thearr = [[False, False], [False, False]]
  6. thebool = np.any(thearr)
  7. print(thebool)

OutPut

  1. True
  2. False

获得 NumPy 数组的转置

  1. import numpy as np
  2. the_array = np.array([[1, 2], [3, 4]])
  3. print(the_array)
  4. print(the_array.T)

OutPut

  1. [[1 2]
  2. [3 4]]
  3. [[1 3]
  4. [2 4]]

获取和设置NumPy数组的数据类型

  1. import numpy as np
  2. type1 = np.array([1, 2, 3, 4, 5, 6])
  3. type2 = np.array([1.5, 2.5, 0.5, 6])
  4. type3 = np.array(['a', 'b', 'c'])
  5. type4 = np.array(["Canada", "Australia"], dtype='U5')
  6. type5 = np.array([555, 666], dtype=float)
  7. print(type1.dtype)
  8. print(type2.dtype)
  9. print(type3.dtype)
  10. print(type4.dtype)
  11. print(type5.dtype)
  12. print(type4)

OutPut

  1. int32
  2. float64
  3. <U1
  4. <U5
  5. float64
  6. ['Canad' 'Austr']

获得NumPy数组的形状

  1. import numpy as np
  2. array1d = np.array([1, 2, 3, 4, 5, 6])
  3. array2d = np.array([[1, 2, 3], [4, 5, 6]])
  4. array3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
  5. print(array1d.shape)
  6. print(array2d.shape)
  7. print(array3d.shape)

OutPut

  1. (6,)
  2. (2, 3)
  3. (2, 2, 3)

获得 1、2 或 3 维 NumPy 数组

  1. import numpy as np
  2. array1d = np.array([1, 2, 3, 4, 5, 6])
  3. print(array1d.ndim) # 1
  4. array2d = np.array([[1, 2, 3], [4, 5, 6]])
  5. print(array2d.ndim) # 2
  6. array3d = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  7. array3d = array3d.reshape(2, 3, 2)
  8. print(array3d.ndim) # 3

OutPut

  1. 1
  2. 2
  3. 3

重塑 NumPy 数组

  1. import numpy as np
  2. thearray = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. thearray = thearray.reshape(2, 4)
  4. print(thearray)
  5. print("-" * 10)
  6. thearray = thearray.reshape(4, 2)
  7. print(thearray)
  8. print("-" * 10)
  9. thearray = thearray.reshape(8, 1)
  10. print(thearray)

OutPut

  1. [[1 2 3 4]
  2. [5 6 7 8]]
  3. ----------
  4. [[1 2]
  5. [3 4]
  6. [5 6]
  7. [7 8]]
  8. ----------
  9. [[1]
  10. [2]
  11. [3]
  12. [4]
  13. [5]
  14. [6]
  15. [7]
  16. [8]]

调整 NumPy 数组的大小

  1. import numpy as np
  2. thearray = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. thearray.resize(4)
  4. print(thearray)
  5. print("-" * 10)
  6. thearray = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  7. thearray.resize(2, 4)
  8. print(thearray)
  9. print("-" * 10)
  10. thearray = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  11. thearray.resize(3, 3)
  12. print(thearray)

OutPut

  1. [1 2 3 4]
  2. ----------
  3. [[1 2 3 4]
  4. [5 6 7 8]]
  5. ----------
  6. [[1 2 3]
  7. [4 5 6]
  8. [7 8 0]]

将 List 或 Tuple 转换为 NumPy 数组

  1. import numpy as np
  2. thelist = [1, 2, 3]
  3. print(type(thelist)) # <class 'list'>
  4. array1 = np.array(thelist)
  5. print(type(array1)) # <class 'numpy.ndarray'>
  6. thetuple = ((1, 2, 3))
  7. print(type(thetuple)) # <class 'tuple'>
  8. array2 = np.array(thetuple)
  9. print(type(array2)) # <class 'numpy.ndarray'>
  10. array3 = np.array([thetuple, thelist, array1])
  11. print(array3)

OutPut

  1. <class 'list'>
  2. <class 'numpy.ndarray'>
  3. <class 'tuple'>
  4. <class 'numpy.ndarray'>
  5. [[1 2 3]
  6. [1 2 3]
  7. [1 2 3]]

使用 arange 函数创建 NumPy 数组s

  1. import numpy as np
  2. array1d = np.arange(5) # 1 row and 5 columns
  3. print(array1d)
  4. array1d = np.arange(0, 12, 2) # 1 row and 6 columns
  5. print(array1d)
  6. array2d = np.arange(0, 12, 2).reshape(2, 3) # 2 rows 3 columns
  7. print(array2d)
  8. array3d = np.arange(9).reshape(3, 3) # 3 rows and columns
  9. print(array3d)

OutPut

  1. [0 1 2 3 4]
  2. [ 0 2 4 6 8 10]
  3. [[ 0 2 4]
  4. [ 6 8 10]]
  5. [[0 1 2]
  6. [3 4 5]
  7. [6 7 8]]

使用 linspace() 创建 NumPy 数组

  1. import numpy as np
  2. array1d = np.linspace(1, 12, 2)
  3. print(array1d)
  4. array1d = np.linspace(1, 12, 4)
  5. print(array1d)
  6. array2d = np.linspace(1, 12, 12).reshape(4, 3)
  7. print(array2d)

OutPut

  1. [ 1. 12.]
  2. [ 1. 4.66666667 8.33333333 12. ]
  3. [[ 1. 2. 3.]
  4. [ 4. 5. 6.]
  5. [ 7. 8. 9.]
  6. [10. 11. 12.]]

NumPy 日志空间数组示例

  1. import numpy as np
  2. thearray = np.logspace(5, 10, num=10, base=10000000.0, dtype=float)
  3. print(thearray)

OutPut

  1. [1.00000000e+35 7.74263683e+38 5.99484250e+42 4.64158883e+46
  2. 3.59381366e+50 2.78255940e+54 2.15443469e+58 1.66810054e+62
  3. 1.29154967e+66 1.00000000e+70]

创建 Zeros NumPy 数组

  1. import numpy as np
  2. array1d = np.zeros(3)
  3. print(array1d)
  4. array2d = np.zeros((2, 4))
  5. print(array2d)

OutPut

  1. [0. 0. 0.]
  2. [[0. 0. 0. 0.]
  3. [0. 0. 0. 0.]]

NumPy One 数组示例

  1. import numpy as np
  2. array1d = np.ones(3)
  3. print(array1d)
  4. array2d = np.ones((2, 4))
  5. print(array2d)

OutPut

  1. [1. 1. 1.]
  2. [[1. 1. 1. 1.]
  3. [1. 1. 1. 1.]]

NumPy 完整数组示例

  1. import numpy as np
  2. array1d = np.full((3), 2)
  3. print(array1d)
  4. array2d = np.full((2, 4), 3)
  5. print(array2d)

OutPut

  1. [2 2 2]
  2. [[3 3 3 3]
  3. [3 3 3 3]]

NumPy Eye 数组示例

  1. import numpy as np
  2. array1 = np.eye(3, dtype=int)
  3. print(array1)
  4. array2 = np.eye(5, k=2)
  5. print(array2)

OutPut

  1. [[1 0 0]
  2. [0 1 0]
  3. [0 0 1]]
  4. [[0. 0. 1. 0. 0.]
  5. [0. 0. 0. 1. 0.]
  6. [0. 0. 0. 0. 1.]
  7. [0. 0. 0. 0. 0.]
  8. [0. 0. 0. 0. 0.]]

NumPy 生成随机数数组

  1. import numpy as np
  2. print(np.random.rand(3, 2)) # Uniformly distributed values.
  3. print(np.random.randn(3, 2)) # Normally distributed values.
  4. # Uniformly distributed integers in a given range.
  5. print(np.random.randint(2, size=10))
  6. print(np.random.randint(5, size=(2, 4)))

OutPut

  1. [[0.68428242 0.62467648]
  2. [0.28595395 0.96066372]
  3. [0.63394485 0.94036659]]
  4. [[0.29458704 0.84015551]
  5. [0.42001253 0.89660667]
  6. [0.50442113 0.46681958]]
  7. [0 1 1 0 0 0 0 1 0 0]
  8. [[3 3 2 3]
  9. [2 1 2 0]]

NumPy 标识和对角线数组示例

  1. import numpy as np
  2. print(np.identity(3))
  3. print(np.diag(np.arange(0, 8, 2)))
  4. print(np.diag(np.diag(np.arange(9).reshape((3,3)))))

OutPut

  1. [[1. 0. 0.]
  2. [0. 1. 0.]
  3. [0. 0. 1.]]
  4. [[0 0 0 0]
  5. [0 2 0 0]
  6. [0 0 4 0]
  7. [0 0 0 6]]
  8. [[0 0 0]
  9. [0 4 0]
  10. [0 0 8]]

NumPy 索引示例

  1. import numpy as np
  2. array1d = np.array([1, 2, 3, 4, 5, 6])
  3. print(array1d[0]) # Get first value
  4. print(array1d[-1]) # Get last value
  5. print(array1d[3]) # Get 4th value from first
  6. print(array1d[-5]) # Get 5th value from last
  7. # Get multiple values
  8. print(array1d[[0, -1]])
  9. print("-" * 10)
  10. array2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  11. print(array2d)
  12. print("-" * 10)
  13. print(array2d[0, 0]) # Get first row first col
  14. print(array2d[0, 1]) # Get first row second col
  15. print(array2d[0, 2]) # Get first row third col
  16. print(array2d[0, 1]) # Get first row second col
  17. print(array2d[1, 1]) # Get second row second col
  18. print(array2d[2, 1]) # Get third row second col

OutPut

  1. 1
  2. 6
  3. 4
  4. 2
  5. [1 6]
  6. ----------
  7. [[1 2 3]
  8. [4 5 6]
  9. [7 8 9]]
  10. ----------
  11. 1
  12. 2
  13. 3
  14. 2
  15. 5
  16. 8

多维数组中的 NumPy 索引

  1. import numpy as np
  2. array3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
  3. print(array3d)
  4. print(array3d[0, 0, 0])
  5. print(array3d[0, 0, 1])
  6. print(array3d[0, 0, 2])
  7. print(array3d[0, 1, 0])
  8. print(array3d[0, 1, 1])
  9. print(array3d[0, 1, 2])
  10. print(array3d[1, 0, 0])
  11. print(array3d[1, 0, 1])
  12. print(array3d[1, 0, 2])
  13. print(array3d[1, 1, 0])
  14. print(array3d[1, 1, 1])
  15. print(array3d[1, 1, 2])

OutPut

  1. [[[ 1 2 3]
  2. [ 4 5 6]]
  3. [[ 7 8 9]
  4. [10 11 12]]]
  5. 1
  6. 2
  7. 3
  8. 4
  9. 5
  10. 6
  11. 7
  12. 8
  13. 9
  14. 10
  15. 11
  16. 12

NumPy 单维切片示例

  1. import numpy as np
  2. array1d = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  3. print(array1d[4:]) # From index 4 to last index
  4. print(array1d[:4]) # From index 0 to 4 index
  5. print(array1d[4:7]) # From index 4(included) up to index 7(excluded)
  6. print(array1d[:-1]) # Excluded last element
  7. print(array1d[:-2]) # Up to second last index(negative index)
  8. print(array1d[::-1]) # From last to first in reverse order(negative step)
  9. print(array1d[::-2]) # All odd numbers in reversed order
  10. print(array1d[-2::-2]) # All even numbers in reversed order
  11. print(array1d[::]) # All elements

OutPut

  1. [4 5 6 7 8 9]
  2. [0 1 2 3]
  3. [4 5 6]
  4. [0 1 2 3 4 5 6 7 8]
  5. [0 1 2 3 4 5 6 7]
  6. [9 8 7 6 5 4 3 2 1 0]
  7. [9 7 5 3 1]
  8. [8 6 4 2 0]
  9. [0 1 2 3 4 5 6 7 8 9]

NumPy 数组中的多维切片

  1. import numpy as np
  2. array2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  3. print("-" * 10)
  4. print(array2d[:, 0:2]) # 2nd and 3rd col
  5. print("-" * 10)
  6. print(array2d[1:3, 0:3]) # 2nd and 3rd row
  7. print("-" * 10)
  8. print(array2d[-1::-1, -1::-1]) # Reverse an array

OutPut

  1. ----------
  2. [[1 2]
  3. [4 5]
  4. [7 8]]
  5. ----------
  6. [[4 5 6]
  7. [7 8 9]]
  8. ----------
  9. [[9 8 7]
  10. [6 5 4]
  11. [3 2 1]]

翻转 NumPy 数组的轴顺序

  1. import numpy as np
  2. array2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  3. print(array2d)
  4. print("-" * 10)
  5. # Permute the dimensions of an array.
  6. arrayT = np.transpose(array2d)
  7. print(arrayT)
  8. print("-" * 10)
  9. # Flip array in the left/right direction.
  10. arrayFlr = np.fliplr(array2d)
  11. print(arrayFlr)
  12. print("-" * 10)
  13. # Flip array in the up/down direction.
  14. arrayFud = np.flipud(array2d)
  15. print(arrayFud)
  16. print("-" * 10)
  17. # Rotate an array by 90 degrees in the plane specified by axes.
  18. arrayRot90 = np.rot90(array2d)
  19. print(arrayRot90)

OutPut

  1. [[1 2 3]
  2. [4 5 6]
  3. [7 8 9]]
  4. ----------
  5. [[1 4 7]
  6. [2 5 8]
  7. [3 6 9]]
  8. ----------
  9. [[3 2 1]
  10. [6 5 4]
  11. [9 8 7]]
  12. ----------
  13. [[7 8 9]
  14. [4 5 6]
  15. [1 2 3]]
  16. ----------
  17. [[3 6 9]
  18. [2 5 8]
  19. [1 4 7]]

NumPy 数组的连接和堆叠

  1. import numpy as np
  2. array1 = np.array([[1, 2, 3], [4, 5, 6]])
  3. array2 = np.array([[7, 8, 9], [10, 11, 12]])
  4. # Stack arrays in sequence horizontally (column wise).
  5. arrayH = np.hstack((array1, array2))
  6. print(arrayH)
  7. print("-" * 10)
  8. # Stack arrays in sequence vertically (row wise).
  9. arrayV = np.vstack((array1, array2))
  10. print(arrayV)
  11. print("-" * 10)
  12. # Stack arrays in sequence depth wise (along third axis).
  13. arrayD = np.dstack((array1, array2))
  14. print(arrayD)
  15. print("-" * 10)
  16. # Appending arrays after each other, along a given axis.
  17. arrayC = np.concatenate((array1, array2))
  18. print(arrayC)
  19. print("-" * 10)
  20. # Append values to the end of an array.
  21. arrayA = np.append(array1, array2, axis=0)
  22. print(arrayA)
  23. print("-" * 10)
  24. arrayA = np.append(array1, array2, axis=1)
  25. print(arrayA)

OutPut

  1. [[ 1 2 3 7 8 9]
  2. [ 4 5 6 10 11 12]]
  3. ----------
  4. [[ 1 2 3]
  5. [ 4 5 6]
  6. [ 7 8 9]
  7. [10 11 12]]
  8. ----------
  9. [[[ 1 7]
  10. [ 2 8]
  11. [ 3 9]]
  12. [[ 4 10]
  13. [ 5 11]
  14. [ 6 12]]]
  15. ----------
  16. [[ 1 2 3]
  17. [ 4 5 6]
  18. [ 7 8 9]
  19. [10 11 12]]
  20. ----------
  21. [[ 1 2 3]
  22. [ 4 5 6]
  23. [ 7 8 9]
  24. [10 11 12]]
  25. ----------
  26. [[ 1 2 3 7 8 9]
  27. [ 4 5 6 10 11 12]]

NumPy 数组的算术运算

  1. import numpy as np
  2. array1 = np.array([[1, 2, 3], [4, 5, 6]])
  3. array2 = np.array([[7, 8, 9], [10, 11, 12]])
  4. print(array1 + array2)
  5. print("-" * 20)
  6. print(array1 - array2)
  7. print("-" * 20)
  8. print(array1 * array2)
  9. print("-" * 20)
  10. print(array2 / array1)
  11. print("-" * 40)
  12. print(array1 ** array2)
  13. print("-" * 40)

OutPut

  1. [[ 8 10 12]
  2. [14 16 18]]
  3. --------------------
  4. [[-6 -6 -6]
  5. [-6 -6 -6]]
  6. --------------------
  7. [[ 7 16 27]
  8. [40 55 72]]
  9. --------------------
  10. [[7. 4. 3. ]
  11. [2.5 2.2 2. ]]
  12. ----------------------------------------
  13. [[ 1 256 19683]
  14. [ 1048576 48828125 -2118184960]]
  15. ----------------------------------------

NumPy 数组上的标量算术运算

  1. import numpy as np
  2. array1 = np.array([[10, 20, 30], [40, 50, 60]])
  3. print(array1 + 2)
  4. print("-" * 20)
  5. print(array1 - 5)
  6. print("-" * 20)
  7. print(array1 * 2)
  8. print("-" * 20)
  9. print(array1 / 5)
  10. print("-" * 20)
  11. print(array1 ** 2)
  12. print("-" * 20)

OutPut

  1. [[12 22 32]
  2. [42 52 62]]
  3. --------------------
  4. [[ 5 15 25]
  5. [35 45 55]]
  6. --------------------
  7. [[ 20 40 60]
  8. [ 80 100 120]]
  9. --------------------
  10. [[ 2. 4. 6.]
  11. [ 8. 10. 12.]]
  12. --------------------
  13. [[ 100 400 900]
  14. [1600 2500 3600]]
  15. --------------------

NumPy 初等数学函数

  1. import numpy as np
  2. array1 = np.array([[10, 20, 30], [40, 50, 60]])
  3. print(np.sin(array1))
  4. print("-" * 40)
  5. print(np.cos(array1))
  6. print("-" * 40)
  7. print(np.tan(array1))
  8. print("-" * 40)
  9. print(np.sqrt(array1))
  10. print("-" * 40)
  11. print(np.exp(array1))
  12. print("-" * 40)
  13. print(np.log10(array1))
  14. print("-" * 40)

OutPut

  1. [[-0.54402111 0.91294525 -0.98803162]
  2. [ 0.74511316 -0.26237485 -0.30481062]]
  3. ----------------------------------------
  4. [[-0.83907153 0.40808206 0.15425145]
  5. [-0.66693806 0.96496603 -0.95241298]]
  6. ----------------------------------------
  7. [[ 0.64836083 2.23716094 -6.4053312 ]
  8. [-1.11721493 -0.27190061 0.32004039]]
  9. ----------------------------------------
  10. [[3.16227766 4.47213595 5.47722558]
  11. [6.32455532 7.07106781 7.74596669]]
  12. ----------------------------------------
  13. [[2.20264658e+04 4.85165195e+08 1.06864746e+13]
  14. [2.35385267e+17 5.18470553e+21 1.14200739e+26]]
  15. ----------------------------------------
  16. [[1. 1.30103 1.47712125]
  17. [1.60205999 1.69897 1.77815125]]
  18. ----------------------------------------

NumPy Element Wise 数学运算

  1. import numpy as np
  2. array1 = np.array([[10, 20, 30], [40, 50, 60]])
  3. array2 = np.array([[2, 3, 4], [4, 6, 8]])
  4. array3 = np.array([[-2, 3.5, -4], [4.05, -6, 8]])
  5. print(np.add(array1, array2))
  6. print("-" * 40)
  7. print(np.power(array1, array2))
  8. print("-" * 40)
  9. print(np.remainder((array2), 5))
  10. print("-" * 40)
  11. print(np.reciprocal(array3))
  12. print("-" * 40)
  13. print(np.sign(array3))
  14. print("-" * 40)
  15. print(np.ceil(array3))
  16. print("-" * 40)
  17. print(np.round(array3))
  18. print("-" * 40)

OutPut

  1. [[12 23 34]
  2. [44 56 68]]
  3. ----------------------------------------
  4. [[ 100 8000 810000]
  5. [ 2560000 -1554869184 -1686044672]]
  6. ----------------------------------------
  7. [[2 3 4]
  8. [4 1 3]]
  9. ----------------------------------------
  10. [[-0.5 0.28571429 -0.25 ]
  11. [ 0.24691358 -0.16666667 0.125 ]]
  12. ----------------------------------------
  13. [[-1. 1. -1.]
  14. [ 1. -1. 1.]]
  15. ----------------------------------------
  16. [[-2. 4. -4.]
  17. [ 5. -6. 8.]]
  18. ----------------------------------------
  19. [[-2. 4. -4.]
  20. [ 4. -6. 8.]]
  21. ----------------------------------------

NumPy 聚合和统计函数

  1. import numpy as np
  2. array1 = np.array([[10, 20, 30], [40, 50, 60]])
  3. print("Mean: ", np.mean(array1))
  4. print("Std: ", np.std(array1))
  5. print("Var: ", np.var(array1))
  6. print("Sum: ", np.sum(array1))
  7. print("Prod: ", np.prod(array1))

OutPut

  1. Mean: 35.0
  2. Std: 17.07825127659933
  3. Var: 291.6666666666667
  4. Sum: 210
  5. Prod: 720000000

Where 函数的 NumPy 示例

  1. import numpy as np
  2. before = np.array([[1, 2, 3], [4, 5, 6]])
  3. # If element is less than 4, mul by 2 else by 3
  4. after = np.where(before < 4, before * 2, before * 3)
  5. print(after)

OutPut

  1. [[ 2 4 6]
  2. [12 15 18]]

Select 函数的 NumPy 示例

  1. import numpy as np
  2. before = np.array([[1, 2, 3], [4, 5, 6]])
  3. # If element is less than 4, mul by 2 else by 3
  4. after = np.select([before < 4, before], [before * 2, before * 3])
  5. print(after)

OutPut

  1. [[ 2 4 6]
  2. [12 15 18]]

选择函数的 NumPy 示例

  1. import numpy as np
  2. before = np.array([[0, 1, 2], [2, 0, 1], [1, 2, 0]])
  3. choices = [5, 10, 15]
  4. after = np.choose(before, choices)
  5. print(after)
  6. print("-" * 10)
  7. before = np.array([[0, 0, 0], [2, 2, 2], [1, 1, 1]])
  8. choice1 = [5, 10, 15]
  9. choice2 = [8, 16, 24]
  10. choice3 = [9, 18, 27]
  11. after = np.choose(before, (choice1, choice2, choice3))
  12. print(after)

OutPut

  1. [[ 5 10 15]
  2. [15 5 10]
  3. [10 15 5]]
  4. ----------
  5. [[ 5 10 15]
  6. [ 9 18 27]
  7. [ 8 16 24]]

NumPy 逻辑操作,用于根据给定条件从数组中选择性地选取值

  1. import numpy as np
  2. thearray = np.array([[10, 20, 30], [14, 24, 36]])
  3. print(np.logical_or(thearray < 10, thearray > 15))
  4. print("-" * 30)
  5. print(np.logical_and(thearray < 10, thearray > 15))
  6. print("-" * 30)
  7. print(np.logical_not(thearray < 20))
  8. print("-" * 30)

OutPut

  1. [[False True True]
  2. [False True True]]
  3. ------------------------------
  4. [[False False False]
  5. [False False False]]
  6. ------------------------------
  7. [[False True True]
  8. [False True True]]
  9. ------------------------------

标准集合操作的 NumPy 示例

  1. import numpy as np
  2. array1 = np.array([[10, 20, 30], [14, 24, 36]])
  3. array2 = np.array([[20, 40, 50], [24, 34, 46]])
  4. # Find the union of two arrays.
  5. print(np.union1d(array1, array2))
  6. # Find the intersection of two arrays.
  7. print(np.intersect1d(array1, array2))
  8. # Find the set difference of two arrays.
  9. print(np.setdiff1d(array1, array2))

OutPut

  1. [10 14 20 24 30 34 36 40 46 50]
  2. [20 24]
  3. [10 14 30 36]