07 numpy 一元函数

=== 一元函数 ===

随机生成5x5的,从-10到10的整数数组

import numpy as np
data = np.random.randint(-10,10,[5,5])

=== abs fabs 求绝对值 fabs不能求复数的绝对值 ===

print(data)
np.fabs(data)

[[ 2 -10 -5 -10 3] [ -7 1 1 -7 4] [ -8 -8 3 -3 -9] [ -8 5 -7 2 9] [ -8 8 3 3 4]]
array([[ 2., 10., 5., 10., 3.], [ 7., 1., 1., 7., 4.], [ 8., 8., 3., 3., 9.], [ 8., 5., 7., 2., 9.], [ 8., 8., 3., 3., 4.]])

=== 开根号 sqrt ===

np.sqrt(data)

=== 平方 square ===

np.square(data)

= 计算e的x次方,x是data =

np.exp(data)

=以某数为底,求data的对数 =

np.log(data) # data的多少次方等于e
np.log10(data) # data的多少次方等于10
np.log2(data) # data的多少次方等于2

=以e为底,求data+1的对数=

np.log1p(data) # data+1的多少次方等于e ===

= 以5为底,求data的对数 =

np.log(data)/np.log(5)

根据换底公式


换底公式

==== 返回数据的正负号 ===

print(data)
np.sign(data)

[[ 2 -10 -5 -10 3] [ -7 1 1 -7 4] [ -8 -8 3 -3 -9] [ -8 5 -7 2 9] [ -8 8 3 3 4]]
array([[ 1, -1, -1, -1, 1], [-1, 1, 1, -1, 1], [-1, -1, 1, -1, -1], [-1, 1, -1, 1, 1], [-1, 1, 1, 1, 1]])

=== 进位操作 ===

float_x = np.array([1.34,2.64,3.44,1.33,2.33])
print(float_x)
#=== 四舍五入 rint ===
np.rint(float_x)

#=== 向下取整 floor ===
np.floor(float_x)

#=== 向上取整 ceil ===
np.ceil(float_x)
#=== 分别返回数组元素中的小数位和整数位,以两个独立数组形式 modf ===
np.modf(float_x)

(array([ 0.34, 0.64, 0.44, 0.33, 0.33]), array([ 1., 2., 3., 1., 2.]))

=== 向下取整 isnan 返回布尔类型数组 ===

数据开根号的时候会产生很多NaN的数据,NaN会对操作引起一些问题

np.isnan( np.sqrt(data) )

=== 判断元素是否有穷 ===

np.isfinite(data)

=== 判断元素是否无穷 ===

np.isinf(data)

=== 三角函数 sin ===

data=np.arange(-10,10,1)
print(data)
np.sin(data)

[-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9]
array([ 0.54402111, -0.41211849, -0.98935825, -0.6569866 , 0.2794155 , 0.95892427, 0.7568025 , -0.14112001, -0.90929743, -0.84147098, 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849])

numpy 一元函数 1

numpy 一元函数 2

你可能感兴趣的:(07 numpy 一元函数)