1. 使用 numpy.min() 函数,指定 axis 参数为所需列的索引:
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
min_col = np.min(arr[:, 1])  # 获取第二列的最小值
print(min_col)  # 输出 2
  1. 使用 numpy.ndarray.min() 方法,指定 axis 参数为所需列的索引:
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
min_col = arr[:, 1].min()  # 获取第二列的最小值
print(min_col)  # 输出 2
  1. 使用 numpy.argmin() 函数,指定 axis 参数为所需列的索引,获取最小值的索引,再使用 numpy.ndarray.__getitem__() 方法获取该列的最小值:
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
min_col_index = np.argmin(arr[:, 1])  # 获取第二列最小值的索引
min_col = arr[:, 1].__getitem__(min_col_index)  # 获取第二列的最小值
print(min_col)  # 输出 2
  1. 使用 numpy.partition() 函数,指定 kth 参数为 0,获取每列的最小值,再使用 numpy.ndarray.__getitem__() 方法获取所需列的最小值:
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
min_cols = np.partition(arr, kth=0, axis=0)[:1]  # 获取每列的最小值
min_col = min_cols.__getitem__((slice(None), 1))  # 获取第二列的最小值
print(min_col)  # 输出 [2]
NumPy 数组求某列最小值:多种方法详解

原文地址: https://www.cveoy.top/t/topic/mwgr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录