电竞比分网-中国电竞赛事及体育赛事平台

分享

Python數(shù)據(jù)分析之numpy常用命令整理

 copy_left 2019-10-26

numpy是高性能科學計算和數(shù)據(jù)分析的基礎包。部分功能如下:

ndarray, 具有矢量算術運算和復雜廣播能力的快速且節(jié)省空間的多維數(shù)組。

用于對整組數(shù)據(jù)進行快速運算的標準數(shù)學函數(shù)(無需編寫循環(huán))。

用于讀寫磁盤數(shù)據(jù)的工具以及用于操作內存映射文件的工具。

線性代數(shù)、隨機數(shù)生成以及傅里葉變換功能。

用于集成C、C++、Fortran等語言編寫的代碼的工具。

導入numpy庫:import numpy as np

一、numpy常用函數(shù)

1.數(shù)組生成函數(shù)

np.array(x):將x轉化為一個數(shù)組

np.array(x,dtype):將x轉化為一個類型為type的數(shù)組

Python數(shù)據(jù)分析之numpy常用命令整理

np.zeros(shape):生成shape維度大小的全0數(shù)組

Python數(shù)據(jù)分析之numpy常用命令整理

np.zeros_like(a):生成與a各維度大小一致的全0數(shù)組

np.ones(shape):生成shape維度大小的全1數(shù)組

np.ones_like(a):生成與a各維度大小一致的全1數(shù)組

Python數(shù)據(jù)分析之numpy常用命令整理

np.full(shape,val):生成shape維度大小的全val數(shù)組

Python數(shù)據(jù)分析之numpy常用命令整理

np.full_like(a,val):生成與a各維度大小一致的全val數(shù)組

np.empty(shape):生成shape維度大小的未初始化數(shù)組

np.empty_like(a):與np.zeros_like(a)作用類似

np.eye(n):生成n×n的單位矩陣

Python數(shù)據(jù)分析之numpy常用命令整理

np.identity(n):生成n×n的單位矩陣

np.arange(begin,end,step):生成一個從begin到end-step的步長為step的一維數(shù)組,其中begin(默認0),step(默認1)可省略

Python數(shù)據(jù)分析之numpy常用命令整理

np.linspace(start,stop,num):生成一個含num個元素的等差數(shù)列,start為第一個元素,stop為最后一個元素

Python數(shù)據(jù)分析之numpy常用命令整理

np.where(cond,a1,a2):根據(jù)條件cond,選取a1或者a2,返回一個新數(shù)組

Python數(shù)據(jù)分析之numpy常用命令整理

2.矩陣函數(shù):

np.diag(a):以一維數(shù)組的形式返回方陣a的對角線元素

Python數(shù)據(jù)分析之numpy常用命令整理

np.diag(x):將輸入數(shù)據(jù)x轉化為方陣(非對角線元素為0)

Python數(shù)據(jù)分析之numpy常用命令整理

np.dot(a,b):矩陣乘法

Python數(shù)據(jù)分析之numpy常用命令整理

np.trace(a):計算對角線元素的和

Python數(shù)據(jù)分析之numpy常用命令整理

3.排序函數(shù):

np.sort(a):排序,返回a中的元素,不影響原數(shù)組。

Python數(shù)據(jù)分析之numpy常用命令整理

np.argsort(a):升序排列,返回a的索引

Python數(shù)據(jù)分析之numpy常用命令整理

np.unique(a):排除重復元素之后,升序排列,返回a中的元素

Python數(shù)據(jù)分析之numpy常用命令整理

4.計算函數(shù)(元素級計算)

np.abs(a)、np.fabs(a):計算絕對值

Python數(shù)據(jù)分析之numpy常用命令整理

np.mean(a):計算均值

np.sqrt(a):計算平方根

np.square(a):計算平方

np.exp(a):計算e^x

np.log(a):計算自然對數(shù)如:log10 log2 log1p

np.sign(a):計算正負號

np.ceil(ndarray):向上取整

np.floor(ndarray):向下取整

np.rint(ndarray):四舍五入

Python數(shù)據(jù)分析之numpy常用命令整理

np.modf(ndarray):拆分整數(shù)和小數(shù)部分,返回兩個數(shù)組組成的元組

Python數(shù)據(jù)分析之numpy常用命令整理

np.cos/cosh/sin/sinh/tan/tanh(a):計算普通型和雙曲型三角函數(shù)

np.arccos/arccosh/arcsin/arcsinh/arctan/arctanh(a):計算反三角函數(shù)和雙曲型反三角函數(shù)

np.maximum(a,b)、np.fmax(a,b):計算最大值

np.minimun(a,b)、np.fmin(a,b):計算最小值

np.copysign(a,b):將b的正負號復制給a

np.logical_and(a,b):邏輯運算&,返回布爾數(shù)組

np.logical_or(a,b):邏輯運算|,返回布爾數(shù)組

np.logical_xor(a,b):邏輯運算^,返回布爾數(shù)組

Python數(shù)據(jù)分析之numpy常用命令整理

5.數(shù)組重復函數(shù)

np.tile(a,reps):a是數(shù)組,reps是個list,reps的元素表示對A的各個axis進行重復的次數(shù)。

np.repeat(a,repeats,axis=None):a是數(shù)組,repeats是各個元素重復的次數(shù)(repeats一般是個標量,稍復雜點是個list),在axis的方向上進行重復,若不指定axis,則返回一維數(shù)組。

Python數(shù)據(jù)分析之numpy常用命令整理

6.數(shù)組組合函數(shù)

水平組合:

np.hstack((a,b))、np.concatenate((a,b),axis=1)

垂直組合:

np.vstack((a,b))、np.concatenate((a,b),axis=0)

深度組合,沿著縱軸方向組合:np.dstack((a,b))

Python數(shù)據(jù)分析之numpy常用命令整理

7.文件讀寫

np.save(string,a):將a保存到string.npy文件中

np.savez(string,a1,a2, ...):將所有的數(shù)組壓縮保存到文件string.npy文件中

np.savetxt(sring,a,fmt,newline='\n'):將a寫入文件,格式為fmt

np.load(string):讀取文件string的文件內容并轉化為數(shù)組對象(或字典對象)

np.loadtxt(string,delimiter):讀取文件string的文件內容,以delimiter為分隔符轉化為數(shù)組

Python數(shù)據(jù)分析之numpy常用命令整理

二、numpy.ndarray的函數(shù)和屬性

1.ndarray屬性

.ndim:返回數(shù)組維數(shù)

Python數(shù)據(jù)分析之numpy常用命令整理

.shape:返回數(shù)組各維度大小的元組

Python數(shù)據(jù)分析之numpy常用命令整理

.dtype:說明數(shù)組元素數(shù)據(jù)類型的對象

.astype(dtype):轉換類型

Python數(shù)據(jù)分析之numpy常用命令整理

.T:簡單矩陣轉置

Python數(shù)據(jù)分析之numpy常用命令整理

2.維數(shù)轉換函數(shù)

.reshape((n,m,...)):將數(shù)組轉化為n*m*...的多維數(shù)組。

Python數(shù)據(jù)分析之numpy常用命令整理

.ravel()/.flatten():數(shù)組展平,將多維數(shù)組降為一維。

Python數(shù)據(jù)分析之numpy常用命令整理

3.計算函數(shù)(axis=0:對列進行操作,axis=1:對行進行操作)

.mean():計算均值

Python數(shù)據(jù)分析之numpy常用命令整理

.sum():求和

.cumsum():累加

.cumprod():累乘

.var():計算方差

.std():計算標準差

.max():求最大值

.min():求最小值

.argmax():最大值索引

.argmin():最小值索引

.any():是否至少一個True

.all():是否全部為True

.dot(b):計算矩陣內積

4.排序函數(shù)(axis=0:對列進行操作,axis=1:對行進行操作)

.sort():排序,返回源數(shù)據(jù)

.argsort():排序,返回數(shù)組索引

Python數(shù)據(jù)分析之numpy常用命令整理

Python數(shù)據(jù)分析之numpy常用命令整理

學習大數(shù)據(jù)技術,可以了解下加米谷大數(shù)據(jù),理論+實踐小班學習,已培養(yǎng)出許多專業(yè)的大數(shù)據(jù)人才!

5.數(shù)組元素選取

a[n]:選取第n+1個元素

a[n:m]:選取第n+1到第m個元素

a[:]:選取全部元素

a[n:]:選取第n+1到最后一個元素

a[:m]:選取第1到第m個元素

a[布爾數(shù)組]:選取為true的元素

Python數(shù)據(jù)分析之numpy常用命令整理

a[[x,y,m,n]]...:選取順序和序列為x、y、m、n的數(shù)組

Python數(shù)據(jù)分析之numpy常用命令整理

a[n,m]:選取第n+1行第m+1個元素

a[n][m]:選取第n+1行第m+1個元素

Python數(shù)據(jù)分析之numpy常用命令整理

a[n,m,...]:選取n+1行m+1列....的元素(三維及三維以上數(shù)組)

a[n][m]...:選取n+1行m+1列....的元素(三維及三維以上數(shù)組)

Python數(shù)據(jù)分析之numpy常用命令整理

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多