函數 | 描述 |
np.array([x,y,z],dtype=int) | 從Python列表和元組中創建數組 |
np.arange(x,y,i) | 創建一個由x到y,以i為步長的數組 |
np.linspace(x,y,n) | 創建一個由x到y,等分成n個元素的數組 |
np.indices((m,n)) | 創建一個m行n列的矩陣 |
np.random.rand(m,n) | 創建一個m行n列的隨機數組 |
np.ones((m,n),dtype) | 創建一個m行n列全1的數組,dtype是數據類型 |
np.empty((m,n),dtype) | 創建一個m行n列全0的數組,dtype是數據類型 |
import numpy as np a1 = np.array([1,2,3,4,5,6]) a2 = np.arange(1,10,3) a3 = np.linspace(1,10,3) a4 = np.indices((3,4)) a5 = np.random.rand(3,4) a6 = np.ones((3,4),int) a7 = np.empty((3,4),int) print(a1) print("===========================================================") print(a2) print("===========================================================") print(a3) print("===========================================================") print(a4) print("===========================================================") print(a5) print("===========================================================") print(a6) print("===========================================================") print(a7) ================================================================================= [1 2 3 4 5 6] =========================================================== [1 4 7] =========================================================== [ 1. 5.5 10. ] =========================================================== [[[0 0 0 0] [1 1 1 1] [2 2 2 2]] [[0 1 2 3] [0 1 2 3] [0 1 2 3]]] =========================================================== [[0.00948155 0.7145306 0.50490391 0.69827703] [0.18164292 0.78440752 0.75091258 0.31184394] [0.17199081 0.3789 0.69886588 0.0476422 ]] =========================================================== [[1 1 1 1] [1 1 1 1] [1 1 1 1]] =========================================================== [[0 0 0 0] [0 0 0 0] [0 0 0 0]]
在建立一個簡單的數組后,可以查看數組的屬性
屬性 | 描述 |
ndarray.ndim | 數組軸的個數,也被稱為秩 |
ndarray.shape | 數組在每個維度上大小的整數元組 |
ndarray.size | 數組元素的總個數 |
ndarray.dtype | 數組元素的數據類型,dtype類型可以用于創建數組 |
ndarray.itemsize | 數組中每個元素的字節大小 |
ndarray.data | 包含實際數組元素的緩沖區地址 |
ndarray.flat | 數組元素的迭代器 |
import numpy as np a6 = np.ones((3,4),int) print(a6) print("=========================================") print(a6.ndim) print("=========================================") print(a6.shape) print("=========================================") print(a6.size) print("=========================================") print(a6.dtype) print("=========================================") print(a6.itemsize) print("=========================================") print(a6.data) print("=========================================") print(a6.flat) ================================================================================= [[1 1 1 1] [1 1 1 1] [1 1 1 1]] ========================================= 2 ========================================= (3, 4) ========================================= 12 ========================================= int32 ========================================= 4 ========================================= memory at 0x0000020D79545908> ========================================= numpy.flatiter object at 0x0000020D103B1180>
數組在numpy中被當做對象,可以采用 a >. b >()方式調用一些方法。
ndarray類的形態操作方法
方法 | 描述 |
ndarray.reshape(n,m) | 不改變數組ndarray,返回一個維度為(n,m)的數組 |
ndarray.resize(new_shape) | 與reshape()作用相同,直接修改數組ndarray |
ndarray.swapaxes(ax1,ax2) | 將數組n個維度中任意兩個維度進行調換 |
ndarray.flatten() | 對數組進行降維,返回一個折疊后的一維數組 |
ndarray.ravel() | 作用同np.flatten(),但返回的是一個視圖 |
ndarray類的索引和切片方法
方法 | 描述 |
x[i] | 索引第i個元素 |
x[-i] | 從后向前索引第i個元素 |
x[n:m] | 默認步長為1,從前向后索引,不包含m |
x[-m:-n] | 默認步長為1,從前向后索引,結束位置為n |
x[n: m :i] | 指定i步長的由n到m的索引 |
除了ndarray類型方法外,numpy庫提供了一匹運算函數
函數 | 描述 |
np.add(x1,x2[,y]) | y = x1 + x2 |
np.subtract(x1,x2[,y]) | y = x1 -x2 |
np.multiply(x1,x2[,y]) | y = x1 * x2 |
np.divide(x1,x2[,y]) | y = x1 /x2 |
np floor_divide(x1,x2[,y]) | y = x1 // x2 |
np.negative(x[,y]) | y = -x |
np.power(x1,x2[,y]) | y = x1 ** x2 |
np.remainder(x1,x2[,y]) | y = x1 % x2 |
numpy庫的比較運算函數
函數 | 符號描述 |
np.equal(x1,x2[,y]) | y = x1 == x2 |
np.not_equal(x1,x2[,y]) | y = x1 != x2 |
np.less(x1,x2,[,y]) | y = x1 x2 |
np.less_equal(x1,x2,[,y]) | y = x1 = x2 |
np.greater(x1,x2,[,y]) | y = x1 > x2 |
np.greater_equal(x1,x2,[,y]) | y >= x1 >= x2 |
np.where(condition[x,y]) | 根據條件判斷是輸出x還是y |
numpy庫的其他運算函數
函數 | 描述 |
np.abs(x) | 計算濟源元素的整形、浮點、或復數的絕對值 |
np.sqrt(x) | 計算每個元素的平方根 |
np.squre(x) | 計算每個元素的平方 |
np.sign(x) | 計算每個元素的符號1(+),0,-1(-) |
np.ceil(x) | 計算大于或等于每個元素的最小值 |
np.floor(x) | 計算小于或等于每個元素的最大值 |
np.rint(x[,out]) | 圓整,取每個元素為最近的整數,保留數據類型 |
np.exp(x[,out]) | 計算每個元素的指數值 |
np.log(x),np.log10(x),np.log2(x) | 計算自然對數(e),基于10,,2的對數,log(1+x) |
到此這篇關于Python基礎之numpy庫的使用的文章就介紹到這了,更多相關Python numpy庫的使用內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!