深度学习中用到的numpy命令数组运算部分命令汇总

numpy数组中数字运算 命令汇总

2.1 np.square()——对数组中每一个元素平方,返回一个新数组

格式:np.square(a)
import numpy as np

a = np.arange(1, 7).reshape(2, 3)
s = np.square(a)	#对a中的每一个元素平方,形成新的数组返回给s
print("a=\n", a, "\nb=\n", s)

结果:

a= s=
[[1 2 3]
[4 5 6]]
[[ 1 4 9]
[16 25 36]]

2.2 np.sqrt()——对数组中每一个元素开方,返回一个新数组

格式:np.sqrt(a)
import numpy as np

a = np.arange(1, 7).reshape(2, 3)
s = np.square(a)
print("a=\n", a, "\nb=\n", s)
ss = np.sqrt(s)	#对数组s中的每一个元素开方,形成新的数组返回给ss
print("ss=\n", ss)

结果:

a= b= ss=
[[1 2 3]
[4 5 6]]
[[ 1 4 9]
[16 25 36]]
[[1. 2. 3.]
[4. 5. 6.]]

2.3 np.round()——对数组中每一个元素按照位数要求四舍五入,并返回一个新数组

格式:np.round(a,decimals=0)
  1. a:需要处理的数组
  2. decimals值:需要保留的位数,默认值为0,即四舍五入到个位,取正整数时保留到小数点后,取负整数时,按照10的次方四舍五入
  3. 当整数部分以0结束时,round函数一律是向下取整
import numpy as np

a_temp = np.array([[50.500, 10.501],
                   [-10.500, - 72.072],
                   [100.50, - 100.501]])
j_round = np.round(a_temp)	#四舍五入取整,注意第一行两个数字的取整变化
print("j_round=\n", j_round)

h_round = np.round(a_temp, decimals=2)	#四舍五入小数点后2位
print("h_round=\n", h_round)

k_round = np.round(a_temp, decimals=-2)	#四舍五入小数点前2位
print("k_round=\n", k_round)

结果:

(decimals取默认值)j_round= (decimals=2)h_round= (decimals=-2)j_round=
[[ 50. 11.]
[ -10. -72.]
[ 100. -101.]]
[[ 50.5 10.5 ]
[ -10.5 -72.07]
[ 100.5 -100.5 ]]
[[ 100. 0.]
[ -0. -100.]
[ 100. -100.]]

2.4 np.exp()——对数组a中的每一个元素x取e的x次方,并返回一个新的数组

格式:np.exp(a)
  • a为需要计算的数组
import numpy as np

a = np.arange(0, 6).reshape(2, 3)
a_exp = np.exp(a)
print("a=\n", a, "\na_exp=\n", a_exp)
a= a_exp=
[[0 1 2]
[3 4 5]]
[[ 1. 2.71828183 7.3890561 ]
[ 20.08553692 54.59815003 148.4131591 ]]
a= 运算过程 a_exp=
[[0 1 2]
[3 4 5]]
[[e0 e1 e2 ]
[e3 e4 e5]]
[[ 1. 2.71828183 7.3890561 ]
[ 20.08553692 54.59815003 148.4131591 ]]

2.5 np.log()——对数组a中的每一个元素x取自然对数lnx,并返回一个新的数组

格式:np.log(a)

  • a为需要计算的数组
import numpy as np

a = np.arange(0, 6).reshape(2, 3)
a_exp = np.exp(a)

print("a=\n", a, "\na_exp=\n", a_exp)

l_log = np.log(a_exp)
print("l_log=\n", l_log)

结果:

a= a_exp= 运算过程 l_log=
[[0 1 2]
[3 4 5]]
[[ 1. 2.71828183 7.3890561 ]
[ 20.08553692 54.59815003 148.4131591 ]]
[[ln1 ln2.7 ln7.38]
[ln20.08 ln54.58 ln 148.41]]
[[0. 1. 2.]
[3. 4. 5.]]

2.6 np.sum()——对数组按要求求和,返回一个数组或一个数值

格式:np.sum(a,axis=None,dtype=None,out=None,keepdims=False)
  1. a:需要处理的数组;
  2. axis:对哪个维度的数据求和,默认情况下axis=None(不用写出来)表示对整个数组所有元素求和,axis=0时对每一列上的元素求和,axis=1时对每一行上的元素求和;
  3. Falsedtype:指定返回值的数据类型
  4. keepdims:keep dimensions的缩写,如果这个参数为True,被删去的维度在结果矩阵中就被设置为一,结果为数组类型,默认值为keepdims=False,可以不写,结果按照计算过程删去一个维度
#当axis=None时:
import numpy as np

a = np.arange(0, 6).reshape(2, 3)
print("a=\n", a)
a_sum_None = np.sum(a)	#keepdims为默认值时查看结果,数组a中所有元素相加结果输出
print("a_sum_None=", a_sum_None)
print("a_sum_None.shape =", a_sum_None.shape, "\na_sum_Nome.type =", type(a_sum_None))

a_sum_None_True = np.sum(a, keepdims=True)	#keepdims=True 时查看结果变化
print("a_sum_None_True=", a_sum_None_True)
print("a_sum_None_True.shape =", a_sum_None_True.shape, "\na_sum_Nome_True.type =", type(a_sum_None_True))

axis=None时的结果:

a= 运算过程 a_sum_None= a_sum_None_True
[[0 1 2]
[3 4 5]]
0+1+2+3+4+5 15 [[15]]
a.shape= (2, 3)
a.type= <class ‘numpy.ndarray’>
a_sum_None.shape = ()
a_sum_Nome.type = <class ‘numpy.int32’>
a_sum_None_True.shape = (1, 1)
a_sum_Nome_True.type = <class ‘numpy.ndarray’>

#当axis=0时:
import numpy as np

a = np.arange(0, 6).reshape(2, 3)
print
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值