这里简单地介绍一下numpy模块中地reduce()函数的用法. 代码如下: # -*- coding: utf-8 -*- import numpy as np class Debug : def __init__ ( self ): self . array1 = np . array ([ 1 , 2 , 3 , 4 ]
这里简单地介绍一下numpy模块中地reduce()函数的用法.
代码如下:
import numpy as np
class Debug:
def __init__(self):
self.array1 = np.array([1, 2, 3, 4])
self.array2 = np.array([5, 6, 7, 8])
self.array3 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
def mainProgram(self):
result = np.add(self.array1, self.array2)
print("The value of result is: ")
print(result)
result1 = np.add.reduce(self.array3, axis=0)
print("The value of result1 is: ")
print(result1)
result2 = np.add.reduce(self.array3, axis=1)
print("The value of result2 is: ")
print(result2)
if __name__ == '__main__':
main = Debug()
main.mainProgram()
"""
The value of result is:
[ 6 8 10 12]
The value of result1 is:
[ 6 8 10 12]
The value of result2 is:
[10 26]
"""
我们可以看到,当我们指定坐标轴为axis=0时,np.add.reduce()函数会将数组沿着y轴加起来,当指定坐标轴axis=1时,np.add.reduce()函数会将数组沿着x轴加起来。对于为什么是这样,可以参考np.repeat()的坐标轴问题。