numpy 的基本使用1
生活随笔
收集整理的這篇文章主要介紹了
numpy 的基本使用1
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
numpy 的基本使用1
最近開始認真學習機器學習啦,之前只是大概了解了一下,記錄一下自己的學習過程,后面也方便隨時復習一下。
NumPy是一個由多維數組對象和用于處理數組的例程集合組成的庫。可以執行以下操作:數組的算數和邏輯運算;傅立葉變換和用于圖形操作的例程;與線性代數有關的操作,NumPy擁有線性代數和隨機數生成的內置函數。NumPy 通常與 SciPy(Scientific Python)和 Matplotlib(繪圖庫)一起使用。
官網:http://www.numpy.org
安裝Numpy:
pip install numpy 或 python -m pip install numpy #學會文檔查詢 np.random.normal? help(np.random.normal) #兩種方式均可 #引入numpy庫: import numpy #查看numpy版本: numpy.__version__ '1.19.5' #重命名庫,方便使用: import numpy as np #通過list創建數組(數組元素類型一致): nparray = np.array([i for i in range(10)]) nparray array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) nparray.dtype dtype('int32') #創建array方法: nparray = np.array([i for i in range(10)]) nparray array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) array = np.zeros(10) array array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) #類型查看: array.dtype dtype('float64') #指定類型: np.zeros(10,dtype = int) array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) ar = np.zeros(3) ar array([0., 0., 0.]) ar.dtype dtype('float64') #創建二維數組: np.zeros((2,3)) array([[0., 0., 0.],[0., 0., 0.]]) #全1數組或二維數組: np.ones(10) array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) np.ones((2,3)) array([[1., 1., 1.],[1., 1., 1.]]) #指定值: np.full(shape=(2,3),fill_value=6) array([[6,6,6],[6,6,6]])#arange #python中創建range(起始值默認0,截止,步長默認1) 步長不能為浮點數: [i for i in range(0,20,2)] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] #Numpy創建 步長可以為浮點數: np.arange(0,20,2.2) array([ 0. , 2.2, 4.4, 6.6, 8.8, 11. , 13.2, 15.4, 17.6, 19.8])#linspace #包括起始點 等間距截取個數: np.linspace(0,20,10) array([ 0. , 2.22222222, 4.44444444, 6.66666667, 8.88888889,11.11111111, 13.33333333, 15.55555556, 17.77777778, 20. ])#random #生成0到10(不包括10)之間的隨機數 np.random.randint(0,10) 9 #指定隨機數個數,建議個數前加上名稱size=以便區分 np.random.randint(0,10,10) array([0, 3, 6, 1, 0, 9, 0, 7, 1, 4]) np.random.randint(0,10,size=(2,3)) array([[3, 0, 5],[6, 5, 3]]) #指定隨機種子: np.random.seed(666) np.random.randint(0,10,10) array([2, 6, 9, 4, 3, 1, 0, 8, 7, 5]) #創建浮點隨機數: np.random.random() 0.19289200304058374 #指定個數,size=可不寫,建議加上以便區分: np.random.random(size = 10) array([0.70084475, 0.29322811, 0.77447945, 0.00510884, 0.11285765,0.11095367, 0.24766823, 0.0232363 , 0.72732115, 0.34003494]) np.random.random((2,3)) array([[0.19750316, 0.90917959, 0.97834699],[0.53280254, 0.25913185, 0.58381262]]) #創建符合正態分布隨機浮點數,均值為0 方差為1 np.random.normal() 0.7294645190059565 指定均值為10和方差100 第三個參數為大小 np.random.normal(10,100,(2,3)) array([[ 124.13367716, 55.24366191, -213.49687669],[ 137.04776598, -62.92882099, 145.9252547 ]])#屬性相關: x = np.arange(10) x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) y = np.arange(6).reshape((3,2)) y array([[0, 1],[2, 3],[4, 5]]) #查看維度: x.ndim 1 y.ndim 2 #一行10個元素: x.shape (10,) #表示3行2列: y.shape (3, 2) #元素個數的統計: x.size 10 y.size 6#array數據訪問: x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) x[0] 0 x[-1] 9 y array([[0, 1],[2, 3],[4, 5]]) y[0][0] 0 y[1,1] 3 #切片: x[0:5] array([0, 1, 2, 3, 4]) x[5:] array([5, 6, 7, 8, 9]) #取列: y[:,0] array([0, 2, 4]) y[:,0].ndim 1 #注意:修改子矩陣中元素會改變原矩陣(區別于python) y array([[0, 1],[2, 3],[4, 5]]) subY = y[:1,:1] subY array([[0]]) subY = y[:1,:2] subY array([[0, 1]]) subY[0,0]=1 subY array([[1, 1]]) y array([[1, 1],[2, 3],[4, 5]]) #創建與原矩陣不相關的矩陣: subY = y[:1,:2].copy() subY array([[1, 1]]) subY[0,0]=2 y array([[1, 1],[2, 3],[4, 5]]) #reshape x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) A = x.reshape(2,5) A array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]]) B = x.reshape(1,10) B array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) #注意:B為二維數組 維度為2: B.ndim 2 A.ndim 2 B.shape (1, 10) #改變為10行,每行有多少讓其自動生成(傳入-1) x.reshape(10,-1) array([[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]) x.reshape(2,-1) array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]]) #錯誤:10不能被3整除 x.reshape(3,-1) Traceback (most recent call last):File "<input>", line 1, in <module> ValueError: cannot reshape array of size 10 into shape (3,newaxis)跟著大佬梳理的流程走下來的,在這里注明一下出處:
https://github.com/Exrick/Machine-Learning
注:大佬的更直觀詳細
總結
以上是生活随笔為你收集整理的numpy 的基本使用1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博物馆守卫问题(世界名画展览馆)
- 下一篇: numpy的基本使用2