实验中遇到的numpy的一些函数用法总结

1. numpy.matmul()

两个numpy数组的矩阵相乘
(1). 如果两个参数a , b a,ba,b都是2 22维的,做普通的矩阵相乘。

import numpy as np
a = [[1, 0], [0, 1]]
b = [[4, 1], [2, 2]]
np.matmul(a, b)

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

(2). 如果某一个参数是N ( N > 2 ) N(N>2)N(N>2)维的,该参数被理解为一些矩阵(参数的最后两个维数为矩阵维数)的stack,而且计算时会相应的广播

a = np.arange(224).reshape((2,2,4))
b = np.arange(224).reshape((2,4,2))
c = np.arange(124).reshape((1,4,2))
np.matmul(a,b)
np.matmul(a,c)

out:
[[[ 28 34]
[ 76 98]]
[[428 466]
[604 658]]]

[[[ 28 34]
[ 76 98]]
[[124 162]
[172 226]]]

2. numpy.round()

描述:
round() 方法返回浮点数x的四舍五入值。

语法:
以下是 round() 方法的语法:

round( x [, n] )

x – 数值表达式。
n – 数值表达式,表示从小数点位数。

返回浮点数x的四舍五入值。

实例:
以下展示了使用 round() 方法的实例:

#!/usr/bin/python

print "round(80.23456, 2) : ", round(80.23456, 2)
print "round(100.000056, 3) : ", round(100.000056, 3)
print "round(-100.000056, 3) : ", round(-100.000056, 3)

以上实例运行后输出结果为:

round(80.23456, 2) : 80.23
round(100.000056, 3) : 100.0
round(-100.000056, 3) : -100.0

3. numpy.around()

numpy.around(a, decimals=0, out=None)

参数解析:
a为输入列表或矩阵;
decimals为n对输入近似后保留小数点后n位,默认为0,若值为-n,则对小数点左边第n位近似;
out为可选参数,一般不用,用于保存近似返回结果。

示例:

np.around([0.37, 1.64])
array([ 0., 2.])
np.around([0.37, 1.64], decimals=1)
array([ 0.4, 1.6])
np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
array([ 0., 2., 2., 4., 4.])
np.around([1,2,3,11], decimals=1) # ndarray of ints is returned
array([ 1, 2, 3, 11])
np.around([1,2,3,11], decimals=-1)
array([ 0, 0, 0, 10])

当近似的小数为5时,则前一位近似为偶数。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值