前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python科学计算库numpy中的add运算

Python科学计算库numpy中的add运算

作者头像
Python小屋屋主
发布2018-04-16 17:08:30
1.8K0
发布2018-04-16 17:08:30
举报
文章被收录于专栏:Python小屋Python小屋

闲言碎语不多讲,直接上代码。

>>> import numpy as np

>>> np.add.accumulate([1,2,3]) # 累加

array([1, 3, 6], dtype=int32)

>>> np.add.accumulate([1,2,3,4,5])

array([ 1, 3, 6, 10, 15], dtype=int32)

>>> np.add.reduce([1,2,3,4,5]) # 连加

15

>>> x = np.array([1,2,3,4])

>>> np.add.at(x, [0,2], 3) # 下标0和2的元素分别加3

>>> x

array([4, 2, 6, 4])

>>> np.add.outer([1,2,3], [4,5,6])

array([[5, 6, 7], # 1+4, 1+5, 1+6

[6, 7, 8], # 2+4, 2+5, 2+6

[7, 8, 9]]) # 3+4, 3+5, 3+6

>>> np.add.outer([1,2,3], [4,5,6,7])

array([[ 5, 6, 7, 8],

[ 6, 7, 8, 9],

[ 7, 8, 9, 10]])

>>> np.add.outer([1,2,3,4], [5,6,7])

array([[ 6, 7, 8],

[ 7, 8, 9],

[ 8, 9, 10],

[ 9, 10, 11]])

>>> np.add.reduceat(np.arange(8),[0,4, 1,5, 2,6, 3,7])

array([ 6, 4, 10, 5, 14, 6, 18, 7], dtype=int32)

>>> x = np.linspace(0, 15, 16).reshape(4,4)

>>> x

array([[ 0., 1., 2., 3.],

[ 4., 5., 6., 7.],

[ 8., 9., 10., 11.],

[ 12., 13., 14., 15.]])

>>> np.add.reduceat(x, [0, 3, 0])

array([[ 12., 15., 18., 21.], # row0+row1+row2

[ 12., 13., 14., 15.], # row3

[ 24., 28., 32., 36.]]) # row0+row1+row2+row3

>>> np.add.reduceat(x, [0, 3, 1])

array([[ 12., 15., 18., 21.], # row0+row1+row2

[ 12., 13., 14., 15.], # row3

[ 24., 27., 30., 33.]]) # row1+row2+row3

>>> np.add.reduceat(x, [0, 3, 2])

array([[ 12., 15., 18., 21.], # row0+row1+row2

[ 12., 13., 14., 15.], # row3

[ 20., 22., 24., 26.]]) # row2+row3

>>> np.add.reduceat(x, [0, 3, 3])

array([[ 12., 15., 18., 21.], # row0+row1+row2

[ 12., 13., 14., 15.], # row3

[ 12., 13., 14., 15.]]) # row3

>>> np.add.reduceat(x, [0, 3, 1, 0])

array([[ 12., 15., 18., 21.], # row0+row1+row2

[ 12., 13., 14., 15.], # row3

[ 4., 5., 6., 7.], # row1

[ 24., 28., 32., 36.]]) # row0+row1+row2+row3

>>> np.add.reduceat(x, [0, 3, 1, 1])

array([[ 12., 15., 18., 21.], # row0+row1+row2

[ 12., 13., 14., 15.], # row3

[ 4., 5., 6., 7.], # row1

[ 24., 27., 30., 33.]]) # row1+row2+row3

>>> np.add.reduceat(x, [0, 3, 1, 2])

array([[ 12., 15., 18., 21.], # row0+row1+row2

[ 12., 13., 14., 15.], # row3

[ 4., 5., 6., 7.], # row1

[ 20., 22., 24., 26.]]) # row2+row3

>>> np.add.reduceat(x, [0, 3, 1, 3])

array([[ 12., 15., 18., 21.], # row0+row1+row2

[ 12., 13., 14., 15.], # row3

[ 12., 14., 16., 18.], # row1

[ 12., 13., 14., 15.]]) # row3

>>> np.add.reduceat(x, [0, 3, 1, 3], axis=1) # 对列进行计算

array([[ 3., 3., 3., 3.],

[ 15., 7., 11., 7.],

[ 27., 11., 19., 11.],

[ 39., 15., 27., 15.]])

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-05-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python小屋 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档