如何使用Numpy查找给定矩阵的行和列的总和?
在此问题中,我们将分别找到所有行和所有列的总和。我们将使用该sum()函数来获取总和。
算法
Step 1: Import numpy. Step 2: Create a numpy matrix of mxn dimension. Step 3: Obtain the sum of all the rows. Step 4: Obtain the sum of all the columns.
范例程式码
import numpy as np
a = np.matrix('10 20; 30 40')
print("Our matrix: \n", a)
sum_of_rows = np.sum(a, axis = 0)
print("\nSum of all the rows: ", sum_of_rows)
sum_of_cols = np.sum(a, axis = 1)
print("\nSum of all the columns: \n", sum_of_cols)输出结果Our matrix: [[10 20] [30 40]] Sum of all the rows: [[40 60]] Sum of all the columns: [[30] [70]]
解释
该函数采用一个称为“轴”的附加矩阵。轴取两个值。0或1。如果axis=0,它告诉函数仅考虑行。如果axis=1,则告诉函数仅考虑列。np.sum()sum()sum()
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下