numpy中的快速的元素级数组函数
一元(unary)ufunc
对于数组中的每一个元素,都将元素代入函数,将得到的结果放回到原来的位置
>>> import numpy as np
>>> arr=np.arange(10)
>>> arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.sqrt(arr)# 开方
array([0. , 1. , 1.41421356, 1.73205081, 2. ,
2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])
>>> np.exp(arr)# e的n次方
array([1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01,
5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03,
2.98095799e+03, 8.10308393e+03])
>>>
二元(binary)ufunc
取x和y之中对应位置的最大值
>>> x=np.random.randn(8)
>>> y=np.random.randn(8)
>>> x
array([ 0.15753027, 1.24668807, -0.26609702, 1.00292598, 0.49230071,
-1.6626497 , -0.35986389, 0.28558569])
>>> y
array([-0.44082009, 2.26042214, 0.95233366, -1.01650424, -0.35827745,
-0.21205099, 0.06795023, -1.2609774 ])
>>> np.maximum(x,y)
array([ 0.15753027, 2.26042214, 0.95233366, 1.00292598, 0.49230071,
-0.21205099, 0.06795023, 0.28558569])
返回多个数组的ufunc
分别获取小数部分和整数部分
>>> arr=np.random.randn(7)*5
>>> arr
array([-15.75240096, 0.4995332 , -6.53116402, 4.76986453,
0.90669531, 2.74661109, -1.29104246])
>>> remainder,whole_part=np.modf(arr)
>>> remainder
array([-0.75240096, 0.4995332 , -0.53116402, 0.76986453, 0.90669531,
0.74661109, -0.29104246])
>>> whole_part
array([-15., 0., -6., 4., 0., 2., -1.])
一些ufuc函数
原文地址:https://www.cnblogs.com/mengxiaoleng/p/11619537.html
时间: 2024-10-10 17:17:32