python数据处理与分析入门-Numpy(3)

往期文章:

  1. numpy使用1
  2. numpy使用2

NumPy - 运算函数

很容易理解的是,NumPy 包含大量的各种数学运算功能。 NumPy 提供标准的三角函数,算术运算的函数,复数处理函数等。

三角函数
a = np.array([0,30,45,60,90])  
print( '含有正弦值的数组:')
sin = np.sin(a*np.pi/180)  
print(sin)
print( '\n'  )
print( '计算角度的反正弦,返回值以弧度为单位:')
inv = np.arcsin(sin)  
print(inv)
print( '\n'  )
print( '通过转化为角度制来检查结果:'  )
print(np.degrees(inv)  )
print( '\n'  )
print( 'arccos 和 arctan 函数行为类似:')
cos = np.cos(a*np.pi/180)  
print(cos)
print( '\n'  )
print( '反余弦:')
inv = np.arccos(cos)  
print(inv)
print( '\n'  )
print( '角度制单位:'  )
print(np.degrees(inv)  )
print( '\n'  )
print( 'tan 函数:')
tan = np.tan(a*np.pi/180)  
print(tan)
print( '\n'  )
print( '反正切:')
inv = np.arctan(tan)  
print(inv)
print( '\n'  )
print( '角度制单位:'  )
print(np.degrees(inv))
舍入函数

这个函数返回四舍五入到所需精度的值。 该函数接受以下参数。

numpy.around(a,decimals)

其中:
序号 参数及描述

  1. a 输入数组
  2. decimals 要舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置
a = np.array([1.0,5.55,  123,  0.567,  25.532])  
print( '原数组:'  )
print(a)
print( '\n'  )
print( '舍入后:'  )
print(np.around(a)  )
print(np.around(a, decimals =  2)  )
print(np.around(a, decimals =  -1))
# 输出
原数组:
[  1.      5.55  123.      0.567  25.532]


舍入后:
[  1.   6. 123.   1.  26.]
[  1.     5.55 123.     0.57  25.53]
[  0.  10. 120.   0.  30.]
算数运算

用于执行算术运算(如add(),subtract(),multiply()和divide())的输入数组必须具有相同的形状或符合数组广播规则。

a = np.arange(9, dtype = np.float_).reshape(3,3)  
print( '第一个数组:'  )
print(a )
print( '\n'  )
print( '第二个数组:' )
b = np.array([10,10,10])  
print(b )
print( '\n'  )
print( '两个数组相加:'  )
print(np.add(a,b)  )
print( '\n'  )
print( '两个数组相减:'  )
print(np.subtract(a,b)  )
print( '\n'  )
print( '两个数组相乘:'  )
print(np.multiply(a,b)  )
print( '\n'  )
print( '两个数组相除:'  )
print(np.divide(a,b))
a = np.array([10,100,1000])  
print( '我们的数组是;'  )
print(a )
print( '\n'  )
print( '调用 power 函数:'  )
print(np.power(a,2)  )
print( '\n'  )
print( '第二个数组:' )
b = np.array([1,2,3])  
print(b )
print( '\n'  )
print( '再次调用 power 函数:'  )
print(np.power(a,b))

统计函数

NumPy 有很多有用的统计函数,用于从数组中给定的元素中查找最小,最大,百分标准差和方差等。 函数说明如下:

a = np.array([[3,7,5],[8,4,3],[2,4,9]])  
print( '我们的数组是:'  )
print(a )
print( '\n'  )
print( '调用 amin() 函数:'  )
print(np.amin(a,1)  )
print( '\n'  )
print( '再次调用 amin() 函数:'  )
print(np.amin(a,0)  )
print( '\n'  )
print( '调用 amax() 函数:'  )
print(np.amax(a)  )
print( '\n'  )
print( '再次调用 amax() 函数:'  )
print(np.amax(a, axis =  0))
# 输出
我们的数组是:
[[3 7 5]
 [8 4 3]
 [2 4 9]]


调用 amin() 函数:
[3 3 2]


再次调用 amin() 函数:
[2 4 3]


调用 amax() 函数:
9


再次调用 amax() 函数:
[8 7 9]
# 百分位数是统计中使用的度量,表示小于这个值得观察值占某个百分比。
a = np.array([[30,40,70],[80,20,10],[50,90,60]])  
print( '我们的数组是:'  )
print(a )
print( '\n'  )
print( '调用 percentile() 函数:'  )
print(np.percentile(a,50)  )
print( '\n'  )
print( '沿轴 1 调用 percentile() 函数:'  )
print(np.percentile(a,50, axis =  1)  )
print( '\n'  )
print( '沿轴 0 调用 percentile() 函数:'  )
print(np.percentile(a,50, axis =  0))
# 输出
我们的数组是:
[[30 40 70]
 [80 20 10]
 [50 90 60]]


调用 percentile() 函数:
50.0


沿轴 1 调用 percentile() 函数:
[40. 20. 60.]


沿轴 0 调用 percentile() 函数:
[50. 40. 60.]
#中值定义为将数据样本的上半部分与下半部分分开的值。
a = np.array([[30,65,70],[80,95,10],[50,90,60]])  
print( '我们的数组是:'  )
print(a )
print( '\n'  )
print( '调用 median() 函数:'  )
print(np.median(a)  )
print( '\n'  )
print( '沿轴 0 调用 median() 函数:'  )
print(np.median(a, axis =  0)  )
print( '\n'  )
print( '沿轴 1 调用 median() 函数:'  )
print(np.median(a, axis =  1))
# 输出
我们的数组是:
[[30 65 70]
 [80 95 10]
 [50 90 60]]


调用 median() 函数:
65.0


沿轴 0 调用 median() 函数:
[50. 90. 60.]


沿轴 1 调用 median() 函数:
[65. 80. 60.]
# 算术平均值是沿轴的元素的总和除以元素的数量。
a = np.array([[1,2,3],[3,4,5],[4,5,6]])  
print( '我们的数组是:'  )
print(a )
print( '\n'  )
print( '调用 mean() 函数:'  )
print(np.mean(a)  )
print( '\n'  )
print( '沿轴 0 调用 mean() 函数:'  )
print(np.mean(a, axis =  0)  )
print( '\n'  )
print( '沿轴 1 调用 mean() 函数:'  )
print(np.mean(a, axis =  1))
# 输出
我们的数组是:
[[1 2 3]
 [3 4 5]
 [4 5 6]]


调用 mean() 函数:
3.6666666666666665


沿轴 0 调用 mean() 函数:
[2.66666667 3.66666667 4.66666667]


沿轴 1 调用 mean() 函数:
[2. 4. 5.]

标准差

标准差是与均值的偏差的平方的平均值的平方根。 标准差公式如下:

std = sqrt(mean((x - x.mean())**2))

如果数组是[1,2,3,4],则其平均值为2.5。 因此,差的平方是[2.25,0.25,0.25,2.25],并且其平均值的平方根除以4,即sqrt(5/4)是1.1180339887498949

print(np.std([1,2,3,4]))
# 1.118033988749895

方差

方差是偏差的平方的平均值,即
mean((x - x.mean())** 2)

print(np.var([1,2,3,4]))  # 1.25

更多内容请查看我的gittee仓库 : Python基础练习

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值