📜  计算二维 NumPy 数组中所有列的总和

📅  最后修改于: 2022-05-13 01:55:06.029000             🧑  作者: Mango

计算二维 NumPy 数组中所有列的总和

让我们看看如何计算 2D NumPy 数组中所有列的总和。
方法1:使用嵌套循环逐列访问数组元素,然后将它们的总和存储在一个变量中,然后打印它。
示例 1:

Python3
# importing required libraries
import numpy
 
# explicit function to compute column wise sum
def colsum(arr, n, m):
    for i in range(n):
        su = 0;
        for j in range(m):
            su += arr[j][i]
        print(su, end = " ")   
 
# creating the 2D Array
TwoDList = [[1, 2, 3], [4, 5, 6],
            [7, 8, 9], [10, 11, 12]]
TwoDArray = numpy.array(TwoDList)
 
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
 
# printing the sum of each column
print("\nColumn-wise Sum:")
colsum(TwoDArray, len(TwoDArray[0]), len(TwoDArray))


Python3
# importing required libraries
import numpy
 
# explicit function to compute column wise sum
def colsum(arr, n, m):
    for i in range(n):
        su = 0;
        for j in range(m):
            su += arr[j][i]
        print(su, end = " ")   
 
# creating the 2D Array
TwoDList = [[1.2, 2.3], [3.4, 4.5]]
TwoDArray = numpy.array(TwoDList)
 
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
 
# printing the sum of each column
print("\nColumn-wise Sum:")
colsum(TwoDArray, len(TwoDArray[0]), len(TwoDArray))


Python3
# importing required libraries
import numpy
 
# creating the 2D Array
TwoDList = [[1, 2, 3], [4, 5, 6],
            [7, 8, 9], [10, 11, 12]]
TwoDArray = numpy.array(TwoDList)
 
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
 
# printing the sum of each column
print("\nColumn-wise Sum:")
print(numpy.sum(TwoDArray, axis = 0))


Python3
# importing required libraries
import numpy
 
# creating the 2D Array
TwoDList =[[1.2, 2.3], [3.4, 4.5]]
TwoDArray = numpy.array(TwoDList)
 
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
 
# printing the sum of each column
print("\nColumn-wise Sum:")
print(*numpy.sum(TwoDArray, axis = 0))


输出 :

2D Array:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

Column-wise Sum:
22 26 30 

示例 2:

Python3

# importing required libraries
import numpy
 
# explicit function to compute column wise sum
def colsum(arr, n, m):
    for i in range(n):
        su = 0;
        for j in range(m):
            su += arr[j][i]
        print(su, end = " ")   
 
# creating the 2D Array
TwoDList = [[1.2, 2.3], [3.4, 4.5]]
TwoDArray = numpy.array(TwoDList)
 
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
 
# printing the sum of each column
print("\nColumn-wise Sum:")
colsum(TwoDArray, len(TwoDArray[0]), len(TwoDArray))

输出 :

2D Array:
[[1.2 2.3]
 [3.4 4.5]]

Column-wise Sum:
4.6 6.8 

方法2:使用NumPy中的sum()函数,numpy.sum(arr, axis, dtype, out)函数返回指定轴上数组元素的总和。要计算所有列的总和, sum()函数中的轴参数应为 0。
示例 1:

Python3

# importing required libraries
import numpy
 
# creating the 2D Array
TwoDList = [[1, 2, 3], [4, 5, 6],
            [7, 8, 9], [10, 11, 12]]
TwoDArray = numpy.array(TwoDList)
 
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
 
# printing the sum of each column
print("\nColumn-wise Sum:")
print(numpy.sum(TwoDArray, axis = 0))

输出 :

2D Array:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

Column-wise Sum:
22 26 30

示例 2:

Python3

# importing required libraries
import numpy
 
# creating the 2D Array
TwoDList =[[1.2, 2.3], [3.4, 4.5]]
TwoDArray = numpy.array(TwoDList)
 
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
 
# printing the sum of each column
print("\nColumn-wise Sum:")
print(*numpy.sum(TwoDArray, axis = 0))

输出 :

2D Array:
[[1.2 2.3]
 [3.4 4.5]]

Column-wise Sum:
4.6 6.8