15、Numpy数学函数

匿名 (未验证) 提交于 2019-12-02 23:32:01

1、番外说明

大家好,我是小P,本系列是本人对Python模块Numpy的一些学习记录,总结于此一方面方便其它初学者学习,另一方面害怕自己遗忘,希望大家喜欢。此外,对“目标检测/模型压缩/语义分割”感兴趣的小伙伴,欢迎加入QQ群 813221712 讨论交流,进群请看群公告!(可以点击如下连接直接加入!)
点击链接加入群聊【Object Detection】:https://jq.qq.com/?_wv=1027&k=5kXCXF8

2、正题

参考链接:

https://www.runoob.com/numpy/numpy-mathematical-functions.html

NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等。

2.1 三角函数

NumPy 提供了标准的三角函数:sin()、cos()、tan()。

实例:Numpy三角函数使用

import numpy as np   a = np.array([0,30,45,60,90]) print ('不同角度的正弦值:') # 通过乘 pi/180 转化为弧度   print (np.sin(a*np.pi/180))  print ('数组中角度的余弦值:') print (np.cos(a*np.pi/180))  print ('数组中角度的正切值:') print (np.tan(a*np.pi/180)) 

输出结果为:

不同角度的正弦值: [0.         0.5        0.70710678 0.8660254  1.        ]  数组中角度的余弦值: [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01  6.12323400e-17]  数组中角度的正切值: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00  1.63312394e+16] 

arcsin,arccos,和 arctan 函数返回给定角度的 sin,cos 和 tan 的反三角函数。

这些函数的结果可以通过 numpy.degrees() 函数将弧度转换为角度。

实例:numpy反三角函数的使用

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

输出结果为:

含有正弦值的数组: [0.         0.5        0.70710678 0.8660254  1.        ]  计算角度的反正弦,返回值以弧度为单位: [0.         0.52359878 0.78539816 1.04719755 1.57079633]  通过转化为角度制来检查结果: [ 0. 30. 45. 60. 90.]  arccos 和 arctan 函数行为类似: [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01  6.12323400e-17]  反余弦: [0.         0.52359878 0.78539816 1.04719755 1.57079633]  角度制单位: [ 0. 30. 45. 60. 90.]  tan 函数: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00  1.63312394e+16]  反正切: [0.         0.52359878 0.78539816 1.04719755 1.57079633]  角度制单位: [ 0. 30. 45. 60. 90.] 舍入函数 

2.2 numpy.around() 函数

返回指定数字的四舍五入值,使用样例如下:

numpy.around(a,decimals) 

参数说明:

● a: 数组
● decimals: 舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置

实例:Numpy.around的使用

import numpy as np   a = np.array([1.0,5.55,  123,  0.567,  25.532])   print  ('原数组:') print (a)  print ('舍入后:') print (np.around(a)) print (np.around(a, decimals =  1)) print (np.around(a, decimals =  -1)) 

输出结果为:

原数组: [  1.      5.55  123.      0.567  25.532]  舍入后: [  1.   6. 123.   1.  26.] [  1.    5.6 123.    0.6  25.5] [  0.  10. 120.   0.  30.] 

2.3 numpy.floor()

numpy.floor() 返回数字的下舍整数。

实例:numpy.floor使用

import numpy as np   a = np.array([-1.7,  1.5,  -0.2,  0.6,  10]) print ('提供的数组:') print (a)  print ('修改后的数组:') print (np.floor(a)) 

输出结果为:

提供的数组: [-1.7  1.5 -0.2  0.6 10. ]  修改后的数组: [-2.  1. -1.  0. 10.] 

2.4 numpy.ceil()

numpy.ceil() 返回数字的上入整数。

实例:numpy.ceil使用

import numpy as np   a = np.array([-1.7,  1.5,  -0.2,  0.6,  10])   print  ('提供的数组:') print (a)  print ('修改后的数组:') print (np.ceil(a)) 

输出结果为:

提供的数组: [-1.7  1.5 -0.2  0.6 10. ]  修改后的数组: [-1.  2. -0.  1. 10.] 
文章来源: https://blog.csdn.net/phinoo/article/details/89737782
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!