Python 3x3 数组行平均数计算 - 使用 NumPy
您可以使用以下代码创建由 1 到 9 九个数字组成的 3x3 的二维数组,并计算每一行的平均数:
import numpy as np
# 创建 3*3 的二维数组
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 计算每一行的平均数
row_means = np.mean(arr, axis=1)
print(row_means)
运行结果为:
[2. 5. 8.]
其中,np.mean(arr, axis=1) 表示计算 arr 数组的每一行的平均数,axis=1 参数表示沿着行的方向进行计算。
原文地址: https://www.cveoy.top/t/topic/mNNv 著作权归作者所有。请勿转载和采集!