python numpy模块 universal functon reduce() 函数用法

网友投稿 653 2022-10-31

python numpy模块 universal functon reduce() 函数用法

python numpy模块 universal functon reduce() 函数用法

这里简单地介绍一下​​numpy​​​模块中地​​reduce()​​​函数的用法. 代码如下:

# -*- coding: utf-8 -*-import numpy as npclass 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()的坐标轴问题​​。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Vue Material是根据Material Design规范构建的轻量级框架
下一篇:数组(array)、列表(list)的动态初始化
相关文章

 发表评论

暂时没有评论,来抢沙发吧~