numpy 簡介
numpy的存在使得python擁有強大的矩陣運算能力,不亞於matlab。
官方文件(https://docs.scipy.org/doc/numpy-dev/user/quickstart.html)
各種用法介紹
首先是numpy中的資料型別,ndarray型別,跟標準函式庫中的array.array不一樣。
ndarray的一些屬性
ndarray.ndim
the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.
ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the rank, or number of dimensions, ndim.
ndarray.size
the total number of elements of the array. This is equal to the product of the elements of shape.
ndarray.dtype
#an object describing the type of the elements in the array. One can create or specify dtype's using standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some#exles. #ndarray.itemsize
the size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while
one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.ndarray.data
the buffer containing the actual elements of the array. Normally, the buffer containing the actual elements of the array. Normally, ##we won't need to use this attribute because we will access the elements in an array using indexing facilities.
>>> import numpy as np>>> a = np.array([2,3,4])>>> a
array([2, 3, 4])>>> a.dtype
dtype('int64')>>> b = np.array([1.2, 3.5, 5.1])>>> b.dtype
dtype('float64')
二維的數組## >>> b = np.array([(1.5,2,3), (4,5,6)])>>> b
array([[ 1.5, 2. , 3. ],
[ 4. , 5. , 6. ]])
建立時指定類型
>>> c = np.array( [ [1,2], [3,4] ], dtype=complex )>>> c array([[ 1.+0.j, 2.+0.j], [ 3.+0.j, 4.+0.j]])
建立一些特殊的矩陣
>>> np.zeros( (3,4) ) array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) >>> np.ones( (2,3,4), dtype=np.int16 ) # dtype can also be specified array([[[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]], [[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]]], dtype=int16) >>> np.empty( (2,3) ) # uninitialized, output may vary array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260], [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
建立一些有特定規律的矩陣
>>> np.arange( 10, 30, 5 ) array([10, 15, 20, 25]) >>> np.arange( 0, 2, 0.3 ) # it accepts float arguments array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]) >>> from numpy import pi >>> np.linspace( 0, 2, 9 ) # 9 numbers from 0 to 2 array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ]) >>> x = np.linspace( 0, 2*pi, 100 ) # useful to evaluate function at lots of points >>> f = np.sin(x)
一些基本的運算
加減乘除三角
函數matlab中有.* ,./等等但是在numpy中,如果使用+,-, ×,/優先執行的是各點之間的加減乘除法
如果兩個矩陣(方陣)可既以元素之間對於運算,又能執行矩陣運算會優先執行元素之間的運算
>>> import numpy as np>>> A = np.arange(10,20)>>> B = np.arange(20,30)>>> A + B array([30, 32, 34, 36, 38, 40, 42, 44, 46, 48])>>> A * B array([200, 231, 264, 299, 336, 375, 416, 459, 504, 551])>>> A / B array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])>>> B / A array([2, 1, 1, 1, 1, 1, 1, 1, 1, 1])
如果需要執行矩陣運算,一般就是矩陣的乘法運算
>>> A = np.array([1,1,1,1]) >>> B = np.array([2,2,2,2]) >>> A.reshape(2,2) array([[1, 1], [1, 1]]) >>> B.reshape(2,2) array([[2, 2], [2, 2]]) >>> A * B array([2, 2, 2, 2]) >>> np.dot(A,B) 8 >>> A.dot(B) 8
一些常用的
全域函數>>> a = np.arange(10)**3 >>> a array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) >>> a[2] 8 >>> a[2:5] array([ 8, 27, 64]) >>> a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000 >>> a array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729]) >>> a[ : :-1] # reversed a array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000]) >>> for i in a: ... print(i**(1/3.)) ... nan 1.0 nan 3.0 nan 5.0 6.0 7.0 8.0 9.0
矩陣的遍歷
>>> import numpy as np >>> b = np.arange(16).reshape(4, 4) >>> for row in b: ... print(row) ... [0 1 2 3] [4 5 6 7] [ 8 9 10 11] [12 13 14 15] >>> for node in b.flat: ... print(node) ... 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
矩陣的特殊運算
#改變矩陣形狀--reshape
>>> a = np.floor(10 * np.random.random((3,4))) >>> a array([[ 6., 5., 1., 5.], [ 5., 5., 8., 9.], [ 5., 5., 9., 7.]]) >>> a.ravel() array([ 6., 5., 1., 5., 5., 5., 8., 9., 5., 5., 9., 7.]) >>> a array([[ 6., 5., 1., 5.], [ 5., 5., 8., 9.], [ 5., 5., 9., 7.]])
resize和reshape的差異
resize會改變原來的矩陣,reshape並不會>>> a
array([[ 6., 5., 1., 5.],
[ 5., 5., 8., 9.],
[ 5., 5., 9., 7.]])
>>> a.reshape(2,-1)
array([[ 6., 5., 1., 5., 5., 5.],
[ 8., 9., 5., 5., 9., 7.]])
>>> a
array([[ 6., 5., 1., 5.],
[ 5., 5., 8., 9.],
[ 5., 5., 9., 7.]])
>>> a.resize(2,6)
>>> a
array([[ 6., 5., 1., 5., 5., 5.],
[ 8., 9., 5., 5., 9., 7.]])
矩陣的合併
>>> a = np.floor(10*np.random.random((2,2)))>>> a array([[ 8., 8.], [ 0., 0.]])>>> b = np.floor(10*np.random.random((2,2)))>>> b array([[ 1., 8.], [ 0., 4.]])>>> np.vstack((a,b)) array([[ 8., 8.], [ 0., 0.], [ 1., 8.], [ 0., 4.]])>>> np.hstack((a,b)) array([[ 8., 8., 1., 8.], [ 0., 0., 0., 4.]])
以上是常用numpy用法詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能