日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python模块(2)-Numpy 简易使用教程

發布時間:2023/12/13 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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) # 矩陣中元素的類型 float64

3.數組訪問-索引、切片、迭代

# 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 1

5.通用函數-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除了堆疊的那個維度,剩余的維度尺寸要求完全一致.

>>> a1=numpy.random.randn(2,2) >>> a2=numpy.random.randn(3,2) >>> a=numpy.vstack((a1,a2)) >>> a array([[ 0.67667278, -0.3318424 ],[-0.2550355 , -0.74132559],[ 0.43534239, 1.46399303],[-0.86049107, 2.03871322],[-0.01824614, 0.46310639]])>>> b1=numpy.random.randn(2,3) >>> b2=numpy.random.randn(2,1) >>> b=numpy.hstack((b1,b2)) >>> b array([[-0.69216195, 0.43455353, -0.5628851 , 1.98854944],[ 1.73648473, 1.11249471, -0.8067703 , -0.53433626]]) >>> import numpy >>> a1=numpy.random.randn(2,2,2) >>> a2=numpy.random.randn(2,2,2) >>> a=numpy.vstack((a1,a2)) >>> a[0] array([[ 0.06585097, -0.80433501],[ 1.77412345, -0.5875084 ]]) >>> b=numpy.hstack((a1,a2)) >>> b.shape (2, 4, 2) >>> a.shape (4, 2, 2)

參考博文: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()

  • 直接用list()函數 # 轉換成 第0維的每一個元素(Numpy.array) 組成的list
  • 用array.tolist()函數 # 與原來的array的數據形式是一樣的,只不過每一維度都是一個list
  • 結論–tolist() 方法轉換的更徹底,list()方法只轉換了源數據的第一個維度。
    如果np.array是一維,兩者沒有區別。但如果是二維結果是不同的。

    >>> import numpy >>> a1=numpy.random.rand(3) >>>>> a1 array([0.26546725, 0.27424911, 0.18618962]) >>> list(a1) [0.26546725247934855, 0.27424910895802035, 0.18618962270208705] >>> a1.tolist() [0.26546725247934855, 0.27424910895802035, 0.18618962270208705]>>> a2 array([[0.25176667, 0.78656379, 0.17814966],[0.38749959, 0.195838 , 0.91929009]]) >>> list(a2) [array([0.25176667, 0.78656379, 0.17814966]), array([0.38749959, 0.195838 , 0.91929009])] >>> a2.tolist() [[0.25176666870818276, 0.7865637882478266, 0.17814966253473885], [0.3874995863899837, 0.19583799515418743, 0.9192900894591074]]>>>

    參考博文:https://www.cnblogs.com/wxiaoli/p/9550382.html

    8.np.random

    于隨機/隨機數相關的一個模塊。

    8.1 randn()和rand()

    rand()-從標準正態分布中隨機抽樣
    randn()-從標準正態分布中隨機抽樣,值處于[0,1]之間

    >import numpy >a=numpy.random.randn(2) >a array([2.14862612, 2.64967285]) >b=numpy.random.randn(2,4) >b array([[ 1.12026781, -0.35804222, 0.40199839, 0.8530957 ],[ 0.17434359, 0.77714432, -1.050777 , -1.4872941 ]]) >c=numpy.random.randn(2,4,3) >c array([[[-0.63199863, 0.3026388 , 1.52031827],[-0.02198394, 1.21513216, -0.0347614 ],[ 0.05571264, 0.38651474, -1.24363781],[-1.93182679, 1.1883758 , -1.90170175]],[[-0.65822958, 0.52109845, -0.49748048],[ 0.66535972, -0.118965 , 1.55862421],[ 0.58604542, 0.44303396, -1.27043267],[-0.26475081, 0.91481557, -0.7255539 ]]]) >a1=numpy.random.rand(2) >a1 array([0.29220946, 0.56908742])

    8.2np.random.choice()

    從xxx中隨機抽取元素

    numpy.random.choice(a, size=None, replace=True, p=None)

    a : 一維數組或整數
    size : 生成樣本的大小
    replace : bool類型 False表示樣本中不允許有重復值 True…
    p : 給定數組中元素出現的概率

    np.random.choice(5,3,p=[0,0,0,0,1]) array([4, 4, 4], dtype=int64)

    參考文檔: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 简易使用教程的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。