Python模块(2)-Numpy 简易使用教程
Numpy模塊 簡易使用教程
- 1.數組創建
- 2.數組基本屬性-維度、尺寸、數據類型
- 3.數組訪問-索引、切片、迭代
- 4.數組的算術運算-加減乘除、轉置求逆、極大極小
- 5.通用函數-sin,cos,exp,sqrt
- np.dot與np.matmul的區別
- 6.數組的合并和分割
- 6.1 np.vstack(),np.hstack()
- 6.2 np.stack()
- 7.list與數組相互轉換
- 7.1 list() vs tolist()
- 8.np.random
- 8.1 randn()和rand()
- 8.2np.random.choice()
- 8.3 np.random.uniform()
- 9.常用方法
- x.astype(int)
- np.squeeze()
- np.std()
- np.prob()
- array.copy()
- np.linspace()
- np.vstack()
- np.c_ ()
- arr.ravel()
Numpy 是pythond科學計算的基礎模塊,常被用作多維數據容器,能夠完成基本的科學計算操作
1.數組創建
import numpy as np# 1.創建數組 arr1 = np.array([1, 2, 3]) arr2 = np.array([(1.3, 9, 2.0), (7, 6, 1)]) arr3 = np.zeros((2, 3)) arr4 = np.identity(3) # 三維度單位陣 arr5 = np.random.random(size=(2, 3))2.數組基本屬性-維度、尺寸、數據類型
# 2.數組屬性 print(arr2.shape) # 矩陣的形狀 (2, 3) print(arr2.ndim) # 矩陣的秩 2 print(arr2.size) # 矩陣所有元素個數 6 print(arr2.dtype.name) # 矩陣中元素的類型 float643.數組訪問-索引、切片、迭代
# 3.數組訪問 print(arr2[:1,:1]) # [[1.3]] for row in arr2:print(row) for element in arr2.flat:print(element)4.數組的算術運算-加減乘除、轉置求逆、極大極小
# 4.數組運算 arr9 = np.array([[2, 1], [1, 2]]) arr10 = np.array([[1, 2], [3, 4]]) # 逐元素的+,-,*,/,%,操作 print(arr9 - arr10) print(arr9**2) print(arr9 * 3) print(arr9 * arr10) # 等價于 np.multiply(arr9, arr10) print(np.dot(arr9, arr10)) # 矩陣叉積, 在二維度的情況下和矩陣乘積的結果一致 print(np.matmul(arr9, arr10)) # 矩陣乘積 # 矩陣轉置,求逆,求和,求極大,求極小 print(arr9.T) print(np.linalg.inv(arr9)) print(arr9.sum(), arr9.max(), arr9.min()) # 6 2 15.通用函數-sin,cos,exp,sqrt
# 5.通用函數sin,cos,都是針對整個數組逐元素操作 print(np.exp(arr9)) print(np.sin(arr9)) print(np.sqrt(arr9))np.dot與np.matmul的區別
在二維度的情況下和矩陣乘積的結果一致
print(np.dot(arr9, arr10)) # 矩陣叉積, print(np.matmul(arr9, arr10)) # 矩陣乘積參考博文:https://blog.csdn.net/acterminate/article/details/96151132
6.數組的合并和分割
# 6.數組的合并和分割 arr11 = np.vstack((arr9, arr10)) # 縱向合并,沿著第0維度合并 arr12 = np.hstack((arr9, arr10)) # 橫向合并,沿著第1維度合并 print(np.vsplit(arr12, 2)) # 縱向切割 print(np.hsplit(arr12, 2)) # 橫向切割6.1 np.vstack(),np.hstack()
np.vstack()沿著第0維度堆疊
np.hstack()沿著第1維度堆疊
只有兩個維度:
np.vstack(tuple)垂直方向堆疊成numpy.array
np.hstack(tuple)水平方向堆疊成numpy.array
注意:
tuple=(a1,a2,a3,…an)
a1,a2,a3,…an除了堆疊的那個維度,剩余的維度尺寸要求完全一致.
參考博文:https://blog.csdn.net/nanhuaibeian/article/details/100597342
6.2 np.stack()
stack() 函數是vstack(),與hstack()結合升級.
Parameters: arrays : sequence of array_like Each array must have the same shape. axis : int, optional The axis in the result array along which the input arrays are stacked. out : ndarray, optional If provided, the destination to place the result. The shape must be correct, matching that of what stack would have returned if no out argument were specified. Returns: stacked : ndarray The stacked array has one more dimension than the input arrays.np.stack(tuple, axis=0) 等價于 np.vstack(tuple)
np.stack(tuple, axis=1) 等價于 np.hstack(tuple)
參考博文:https://blog.csdn.net/u013019431/article/details/79768219
7.list與數組相互轉換
list–>np.array
a=[1,2,3]
b=np.array(a)
np.array->list
c=b.tolist()
7.1 list() vs tolist()
結論–tolist() 方法轉換的更徹底,list()方法只轉換了源數據的第一個維度。
如果np.array是一維,兩者沒有區別。但如果是二維結果是不同的。
參考博文:https://www.cnblogs.com/wxiaoli/p/9550382.html
8.np.random
于隨機/隨機數相關的一個模塊。
8.1 randn()和rand()
rand()-從標準正態分布中隨機抽樣
randn()-從標準正態分布中隨機抽樣,值處于[0,1]之間
8.2np.random.choice()
從xxx中隨機抽取元素
numpy.random.choice(a, size=None, replace=True, p=None)
a : 一維數組或整數
size : 生成樣本的大小
replace : bool類型 False表示樣本中不允許有重復值 True…
p : 給定數組中元素出現的概率
參考文檔:https://www.cnblogs.com/cavaliers20160620/p/8964784.html
8.3 np.random.uniform()
從[1,2]的均勻分布中隨機采樣64個數據,并且將尺寸由 (64,)轉換成(64,1)
np.random.uniform(1, 2, size=64)[:, np.newaxis]
9.常用方法
x.astype(int)
改變數組中的數據類型
x=np.array([1,2,2.5])
x.astype(int)
np.squeeze()
去除中維度為1 的維
In [20]: d=np.array([[[1],[1]],[[1],[1]]])
In [22]: d.shape
Out[22]: (2, 2, 1)
In [23]: e=np.squeeze(d)
In [25]: e.shape
Out[25]: (2, 2)
np.std()
計算所有元素的標準差
numpy.std(data)
計算每一列的標準差
numpy.std(data, 0)
計算每一行的標準差
numpy.std(data, 1)
np.prob()
返回給定維度上各個元素的乘積
>>> import numpy as np >>> a=np.array([[2,3],[4,5]]) >>> a array([[2, 3],[4, 5]])>>> np.prod(a) 120 >>> np.prod(a,axis=0) array([ 8, 15])>>> np.prod(a,axis=1) array([ 6, 20])array.copy()
直接名字賦值,兩個變量指向同一塊地址,其中任何一個數據操作都會影響另一個數組。用數組的.copy()方法,則會建立一個與原來完全獨立但是數字完全相同的一個數組。
import numpy as np ar1 = np.arange(10) print(ar1)ar2 = ar1 print(ar2 is ar1)ar1[2] = 9 print(ar1,ar2) #ar1和ar2 指向同一個值,所以ar1改變,ar2一起改變print('-------------------------------')ar3 = ar1.copy() print(ar3 is ar1) ar1[0] = 9 print(ar1,ar3) #coyp方法生成數組及其數據的完整拷貝j輸出結果:
[0 1 2 3 4 5 6 7 8 9]True[0 1 9 3 4 5 6 7 8 9] [0 1 9 3 4 5 6 7 8 9]-------------------------------False[9 1 9 3 4 5 6 7 8 9] [0 1 9 3 4 5 6 7 8 9]參考博文:https://blog.csdn.net/weixin_30935137/article/details/80822005
np.linspace()
輸出(-1,1)之間的等間距15個數
np.linspace(-1, 1, 15)
np.vstack()
寫在一行的for循環,每次將15個數作為一行堆疊64次,生成一個64*15的矩陣
np.vstack([np.linspace(-1, 1, 15) for _ in range(64)])
np.c_ ()
np.c_ 用于連接兩個矩陣
np.c 中的c 是 column(列)的縮寫,就是按列疊加兩個矩陣,就是把兩個矩陣左右組合,要求行數相等。
參考博文:https://blog.csdn.net/qq_33728095/article/details/102512600
arr.ravel()
將多維數組轉換成一維數組
>>> a = numpy.array([[1,2],[3,4]]) >>> a.ravel() array([1, 2, 3, 4]) >>>總結
以上是生活随笔為你收集整理的Python模块(2)-Numpy 简易使用教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NLP复习资料(2)-三~五章:形式语言
- 下一篇: 大数据学习(07)--MapReduce