鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ NumPy數(shù)學(xué)算數(shù)函數(shù)
NumPy位操作
NumPy數(shù)學(xué)算數(shù)函數(shù)
NumPy高級索引
NumPy環(huán)境安裝配置
NumPy IO文件操作
NumPy字符串函數(shù)
NumPy切片和索引
NumPy統(tǒng)計函數(shù)
NumPy矩陣庫
NumPy數(shù)組創(chuàng)建例程
NumPy線性代數(shù)
NumPy Matplotlib庫
NumPy教程
NumPy排序、搜索和計數(shù)函數(shù)
NumPy字節(jié)交換
NumPy Ndarray對象
NumPy數(shù)組操作
NumPy使用 Matplotlib 繪制直方圖
NumPy數(shù)組屬性
NumPy廣播
NumPy來自現(xiàn)有數(shù)據(jù)的數(shù)組
NumPy副本和視圖
NumPy在數(shù)組上的迭代
NumPy來自數(shù)值范圍的數(shù)組
NumPy算數(shù)運算
NumPy數(shù)據(jù)類型

NumPy數(shù)學(xué)算數(shù)函數(shù)

NumPy - 算數(shù)函數(shù)

很容易理解的是,NumPy 包含大量的各種數(shù)學(xué)運算功能。 NumPy 提供標準的三角函數(shù),算術(shù)運算的函數(shù),復(fù)數(shù)處理函數(shù)等。

三角函數(shù)

NumPy 擁有標準的三角函數(shù),它為弧度制單位的給定角度返回三角函數(shù)比值。

示例

import numpy as np
a = np.array([0,30,45,60,90])  
print  '不同角度的正弦值:'  
# 通過乘 pi/180 轉(zhuǎn)化為弧度  
print np.sin(a*np.pi/180)  
print  '\n'  
print  '數(shù)組中角度的余弦值:'  
print np.cos(a*np.pi/180)  
print  '\n'  
print  '數(shù)組中角度的正切值:'  
print np.tan(a*np.pi/180)

輸出如下:

不同角度的正弦值:                                                   
[ 0.          0.5         0.70710678  0.8660254   1.        ]                 

數(shù)組中角度的余弦值:                                         
[  1.00000000e+00   8.66025404e-01   7.07106781e-01   5.00000000e-01          
   6.12323400e-17]                                                            

數(shù)組中角度的正切值:                                            
[  0.00000000e+00   5.77350269e-01   1.00000000e+00   1.73205081e+00          
   1.63312394e+16]

arcsin,arccos,和arctan函數(shù)返回給定角度的sincostan的反三角函數(shù)。 這些函數(shù)的結(jié)果可以通過numpy.degrees()函數(shù)通過將弧度制轉(zhuǎn)換為角度制來驗證。

示例

import numpy as np
a = np.array([0,30,45,60,90])  
print  '含有正弦值的數(shù)組:'
sin = np.sin(a*np.pi/180)  
print sin
print  '\n'  
print  '計算角度的反正弦,返回值以弧度為單位:'
inv = np.arcsin(sin)  
print inv
print  '\n'  
print  '通過轉(zhuǎn)化為角度制來檢查結(jié)果:'  
print np.degrees(inv)  
print  '\n'  
print  'arccos 和 arctan 函數(shù)行為類似:'
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 函數(shù):'
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)

輸出如下:

含有正弦值的數(shù)組:
[ 0.          0.5         0.70710678  0.8660254   1.        ]

計算角度的反正弦,返回值以弧度制為單位:
[ 0.          0.52359878  0.78539816  1.04719755  1.57079633]

通過轉(zhuǎn)化為角度制來檢查結(jié)果:
[  0.  30.  45.  60.  90.]

arccos 和 arctan 函數(shù)行為類似:
[  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 函數(shù):
[  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.]

舍入函數(shù)

numpy.around()

這個函數(shù)返回四舍五入到所需精度的值。 該函數(shù)接受以下參數(shù)。

numpy.around(a,decimals)

其中:

序號 參數(shù)及描述
1. a 輸入數(shù)組
2. decimals 要舍入的小數(shù)位數(shù)。 默認值為0。 如果為負,整數(shù)將四舍五入到小數(shù)點左側(cè)的位置

示例

import numpy as np
a = np.array([1.0,5.55,  123,  0.567,  25.532])  
print  '原數(shù)組:'  
print a
print  '\n'  
print  '舍入后:'  
print np.around(a)  
print np.around(a, decimals =  1)  
print np.around(a, decimals =  -1)

輸出如下:

原數(shù)組:                                                          
[   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. ]

numpy.floor()

此函數(shù)返回不大于輸入?yún)?shù)的最大整數(shù)。 即標量x 的下限是最大的整數(shù)i ,使得i <= x。 注意在Python中,向下取整總是從 0 舍入。

示例

import numpy as np
a = np.array([-1.7,  1.5,  -0.2,  0.6,  10])  
print  '提供的數(shù)組:'  
print a
print  '\n'  
print  '修改后的數(shù)組:'  
print np.floor(a)

輸出如下:

提供的數(shù)組:                                                            
[ -1.7   1.5  -0.2   0.6  10. ]

修改后的數(shù)組:                                                         
[ -2.   1.  -1.   0.  10.]

numpy.ceil()

ceil()函數(shù)返回輸入值的上限,即,標量x的上限是最小的整數(shù)i ,使得i> = x。

示例

import numpy as np
a = np.array([-1.7,  1.5,  -0.2,  0.6,  10])  
print  '提供的數(shù)組:'  
print a
print  '\n'  
print  '修改后的數(shù)組:'  
print np.ceil(a)

輸出如下:

提供的數(shù)組:
[ -1.7   1.5  -0.2   0.6  10. ]

修改后的數(shù)組:
[ -1.   2.  -0.   1.  10.]