Python基礎(chǔ)之Numpy的基本用法詳解
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) # 一維數(shù)組b = np.array([[1, 2], [3, 4]]) #二維數(shù)組
1.2 序列數(shù)組numpy.arange(start, stop, step, dtype),start默認(rèn)0,step默認(rèn)1
c = np.arange(0, 10, 1, dtype=int) # =np.arange(10) [0 1 2 3 4 5 6 7 8 9]d = np.array([np.arange(1, 3), np.arange(4, 6)]) # 二維數(shù)組# 不過為了避免麻煩,通常序列二維數(shù)組都是通過reshape進(jìn)行重新組織dd = c.reshape(2, 5) # 將一維數(shù)組重新組合成2行5列1.3 隨機(jī)數(shù)組
numpy.random.random(size=None) 該方法返回[0.0, 1.0)范圍的隨機(jī)小數(shù)。numpy.random.randint() 該方法返回[low, high)范圍的隨機(jī)整數(shù)。該方法有三個參數(shù)low、high、size 三個參數(shù)。默認(rèn)high是None,如果只有l(wèi)ow,那范圍就是[0,low)。如果有high,范圍就是[low,high)numpy.random.randn(d0,d1,…,dn) 該方法返回一個或一組樣本,具有正態(tài)分布np.random.normal 指定期望和方差的正太分布
e = np.random.random(size=2) # 一維數(shù)組,元素兩個,[0.0,1.0]的隨機(jī)數(shù)f = np.random.random(size=(2, 3)) # 兩行三列數(shù)組,[0.0,1.0]的隨機(jī)數(shù)h = np.random.randint(10, size=3) # [0,10]范圍內(nèi)的一行三列隨機(jī)整數(shù)i = np.random.randint(5, 10, size=(2, 3)) # [5,10]范圍內(nèi)的2行3列隨機(jī)整數(shù)1.4 其他方式數(shù)組
numpy.zeros 創(chuàng)建指定大小的數(shù)組,數(shù)組元素以0 來填充numpy.ones 創(chuàng)建指定形狀的數(shù)組,數(shù)組元素以1 來填充numpy.empty 創(chuàng)建一個指定形狀(shape)、數(shù)據(jù)類型(dtype)且未初始化的數(shù)組,里面的元素的值是之前內(nèi)存的值np.linspace 創(chuàng)建一個一維數(shù)組,數(shù)組是一個等差數(shù)列構(gòu)成的numpy.logspace 創(chuàng)建一個于等比數(shù)
j = np.zeros((2, 5))k = np.ones((2, 5))l = np.linspace(1, 20, 10)二、數(shù)組屬性查看
ndarray.ndimdarray.shape 數(shù)組的維度和列,對于矩陣,n 行m 列ndarray.size 數(shù)組元素的總個數(shù),相當(dāng)于.shape 中n*m 的值ndarray.dtype ndarray 對象的元素類型ndarray.itemsize ndarray 對象中每個元素的大小,以字節(jié)為單位ndarray.flags ndarray 對象的內(nèi)存信息ndarray.real ndarray 元素的實部ndarray.imag ndarray 元素的虛部ndarray.data 包含實際數(shù)組元素的緩沖區(qū),由于一般通過數(shù)組的索引獲取元素,所以通常不需要使用這個屬性。
print(’ndim:數(shù)組的秩(維度)’.center(20, ’*’))print(’ndim:’, i.shape[1])三、數(shù)組索引
x = np.arange(1, 13) # 一維數(shù)組a = x.reshape(4, 3) # 二維數(shù)組print(‘x:’, x)print(‘a(chǎn):’, a)
3.1 一維數(shù)組的索引print(x[2:])print(x[3:8])3.2 二維數(shù)組的索引
print(a[0]) # 第一行print(a[2, 2]) # 第三行第4列print(a[:, 2])print(a[::2, 0]) # 所有奇數(shù)行第1列數(shù)據(jù)print(a[(2, 1), (1, 2)]) # 第3行第2列,第2行第3列 = np.array((a[2,1],a[1,2]))取出來后在重新生成新的數(shù)組print(a[-2]) # 獲取倒數(shù)第二行print(a[::-1]) # 行倒序print(a[::-1, ::-1]) # 行列倒序四、數(shù)組的方法4.1 改變數(shù)組維度
reshape將一維數(shù)組變成二維或者三維ravel將三維數(shù)組變成一維數(shù)組,flatten將二維數(shù)組變成一維數(shù)組
4.2 數(shù)組拼接使用numpy.hstack(a1,a2) 函數(shù)將兩個數(shù)組水平組合numpy.vstack(a1,a2) 函數(shù)可以將兩個或多個數(shù)組垂直組合起來形成一個數(shù)組使用numpy.concatenate((a1, a2, …), axis),控制axis參數(shù)的值也可以實現(xiàn)hstack和vstack的功能,axis=0等同于vstack、axis=1等同于hstack
4.3 數(shù)組分隔b = np.split(x, 4) # 將一個一維數(shù)組四等分, 用b[1]的方式獲取每個塊的數(shù)據(jù)# print(b[1])c = np.split(a, 2, axis=0) # 二維數(shù)組的垂直分隔,按行分隔成兩部分# print(c[0])d = np.split(a, [2], axis=1) # 二維數(shù)組的水平分隔,按列分隔成兩部分# print(d[0])4.4 算術(shù)運(yùn)算
加減乘除: add(),subtract(),multiply() 和divide()np.sum() 求和np.prod() 所有元素相乘np.mean() 平均值np.std() 標(biāo)準(zhǔn)差np.var() 方差np.median() 中數(shù)np.power() 冪運(yùn)算np.sqrt() 開方np.min() 最小值np.max() 最大值np.argmin() 最小值的下標(biāo)np.argmax() 最大值的下標(biāo)np.inf 無窮大np.exp(10) 以e 為底的指數(shù)np.log(10) 對數(shù)
到此這篇關(guān)于Python基礎(chǔ)之Numpy的基本用法詳解的文章就介紹到這了,更多相關(guān)Python Numpy用法內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. php使用正則驗證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)3. Jsp servlet驗證碼工具類分享4. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)5. Jsp+Servlet實現(xiàn)文件上傳下載 文件列表展示(二)6. XML在語音合成中的應(yīng)用7. jscript與vbscript 操作XML元素屬性的代碼8. asp.net core 認(rèn)證和授權(quán)實例詳解9. 基于PHP做個圖片防盜鏈10. HTML5實戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)
