[编程][笔记] Python中numpy的求和

实干、实践、积累、思考、创新。


Numpy中的Sum函数,可以实现按行、按列相加。

代码:

import numpy as np
# A=np.zeros((3,4))
A=np.array([[1,2,3],[4,5,6]])
print(A)
# 逐个元素求和
print('逐个元素求和')
print(np.sum(A))
# 按列叠加
print('按列叠加')
temp=np.sum(A,axis = 0)
print(temp)
print(temp.shape)
# 按行叠加
print('按行叠加')
temp=np.sum(A,axis = 1)
print(temp)
print(temp.shape)
# temp=[temp]
# print(temp)
# print(temp.shape)

输出:

[[1 2 3]
[4 5 6]]
逐个元素求和
21
按列叠加
[5 7 9]
(3,)
按行叠加
[ 6 15]
(2,)


You already voted!

  • 微信公众号 ( Wechat Subscription)

WeChat_QRCode

欢迎关注 “结构之旅” 微信公众号

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.