基本屬性
在做一些數據分析的時候,我們通常會把數據存為矩陣的形式,然后python本身對於矩陣的操作是不夠的,因此出現了numpy這樣一個科學開發庫來進行python在次上面的不足。
Numpy's array 類被稱為ndarray。 這個對象常用而重要的屬性如下:
- ndarray.ndim: 輸出矩陣(數組)的維度
- ndarray.shape: 輸出矩陣的各維數大小,相當於matlab中的size()函數
- ndarray.size: 輸出矩陣(數組)元素的總個數,相當於各維數之積
- ndarray.dtype: 輸出矩陣元素的類型,如int16, int32, float64等
- ndarray.itemsize: 輸出矩陣中每個元素所占字節數
一個例子
>>> from numpy import * >>> a = arange(15).reshape(3, 5) >>> a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]) >>> a.shape (3, 5) >>> a.ndim 2 >>> a.dtype.name 'int32' >>> a.itemsize 4 >>> a.size 15 >>> type(a) numpy.ndarray >>> b = array([6, 7, 8]) >>> b array([6, 7, 8]) >>> type(b) numpy.ndarray
矩陣創建
python中有多種方式來創建矩陣,第一種是通過Python中的列表直接創建,第二種是通過numpy中的array函數,第三種是利用一些特殊的函數如zeros
, ones
, empty
等來創建一些特殊的矩陣。
>>> from numpy import * >>> a = array( [2,3,4] ) >>> a array([2, 3, 4]) >>> a.dtype dtype('int32') >>> b = array([1.2, 3.5, 5.1]) >>> b.dtype dtype('float64') ----------------------------------------------- >>> a = array(1,2,3,4) # WRONG >>> a = array([1,2,3,4]) # RIGHT ----------------------------------------------- >>> b = array( [ (1.5,2,3), (4,5,6) ] ) >>> b array([[ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]]) ---------------------------------------------- >>> c = array( [ [1,2], [3,4] ], dtype=complex ) >>> c array([[ 1.+0.j, 2.+0.j], [ 3.+0.j, 4.+0.j]]) ----------------------------------------------- >>> zeros( (3,4) ) array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) >>> ones( (2,3,4), dtype=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) >>> empty( (2,3) ) array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260], [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
如果想產生一些連續有規則的序列,可以使用numpy中的arange函數,類似於python中的range,功能相當於matlab中的冒號表達式,此外linespace函數產生等分間隔的數。arange函數的形式是arange(start, end, step)
, 表示從start開始,每隔step取一個數,到end(但不包括end)結束;linespace函數的形式是linespace(start, end, divide)
,表示將區間[start,end)等分為divide這么多分,並取這些等分的點。
>>> arange( 10, 30, 5 ) array([10, 15, 20, 25]) >>> arange( 0, 2, 0.3 ) # it accepts float arguments array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]) --------------------------------------------------------------- >>> 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 = linspace( 0, 2*pi, 100 ) # useful to evaluate function at lots of points >>> f = sin(x)
numpy中常見的其他產生矩陣的函數件這里。
常用操作
常見的+,-,*,/,**
不再贅述,這里強調一下矩陣的點乘和矩陣的懲罰。
>>> A = array( [[1,1], ... [0,1]] ) >>> B = array( [[2,0], ... [3,4]] ) >>> A*B # elementwise product array([[2, 0], [0, 4]]) >>> dot(A,B) # matrix product array([[5, 4], [3, 4]])
一些操作符如+=
和*=
只是跟新原數組而不是創造一個新數組,因此如果你在原數組上+=
一個新的不同類型的矩陣,得到的矩陣的類型和原矩陣相同。
當不同類型矩陣相操作時,得到的結果傾向於更准確的結果,如整型和浮點型相加,得到結果為浮點型。
Many unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the ndarray class.
numpy中提供了一些常用的操作,比如矩陣的求和,求矩陣的最大最小值等,如果矩陣有多維,可以通過指定維數來得到某一位的基本操作(先列后行)。
>>> b = arange(12).reshape(3,4) >>> b array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> >>> b.sum(axis=0) # sum of each column array([12, 15, 18, 21]) >>> >>> b.min(axis=1) # min of each row array([0, 4, 8]) >>> >>> b.cumsum(axis=1) # cumulative sum along each row array([[ 0, 1, 3, 6], [ 4, 9, 15, 22], [ 8, 17, 27, 38]])
常用函數
numpy提供了許多常用的函數,如求平方根,指數等等。
>>> B = arange(3) >>> B array([0, 1, 2]) >>> exp(B) array([ 1. , 2.71828183, 7.3890561 ]) >>> sqrt(B) array([ 0. , 1. , 1.41421356]) >>> C = array([2., -1., 4.]) >>> add(B, C) array([ 2., 0., 6.])
更多的函數介紹請點擊這里
索引(Indexing), 分片(Slicing), 和迭代(Iterating)
One-dimensional
>>> a = 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
Multidimensional
>>> def f(x,y): ... return 10*x+y ... >>> b = fromfunction(f,(5,4),dtype=int) >>> b array([[ 0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33], [40, 41, 42, 43]]) >>> b[2,3] 23 >>> b[0:5, 1] # each row in the second column of b array([ 1, 11, 21, 31, 41]) >>> b[ : ,1] # equivalent to the previous example array([ 1, 11, 21, 31, 41]) >>> b[1:3, : ] # each column in the second and third row of b array([[10, 11, 12, 13], [20, 21, 22, 23]])
Iterating
>>> for row in b: ... print row ... [0 1 2 3] [10 11 12 13] [20 21 22 23] [30 31 32 33] [40 41 42 43] --------------------------------------------------------- >>> for element in b.flat: ... print element, ... 0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33 40 41 42 43
Shape Manipulation
改變數組的形狀
矩陣各個維數有不同的大小,我們可以通過一些命令如ravel()
,resize()
,resize()
等來改變矩陣的大小,下面是一些例子:
>>> a = floor(10*random.random((3,4))) >>> a array([[ 7., 5., 9., 3.], [ 7., 2., 7., 8.], [ 6., 8., 3., 2.]]) >>> a.shape (3, 4) >>> a.ravel() # flatten the array array([ 7., 5., 9., 3., 7., 2., 7., 8., 6., 8., 3., 2.]) >>> a.shape = (6, 2) >>> a.transpose() array([[ 7., 9., 7., 7., 6., 3.], [ 5., 3., 2., 8., 8., 2.]]) >>> a array([[ 7., 5.], [ 9., 3.], [ 7., 2.], [ 7., 8.], [ 6., 8.], [ 3., 2.]]) >>> a.resize((2,6)) >>> a array([[ 7., 5., 9., 3., 7., 2.], [ 7., 8., 6., 8., 3., 2.]]) >>> a.reshape(3,-1) # 當reshape中的參數為-1時,則對應維度的大小會自動計算 array([[ 7., 5., 9., 3.], [ 7., 2., 7., 8.], [ 6., 8., 3., 2.]])
Stacking together different arrays
numpy中提供了一下幾種方式來將不同的矩陣壓縮在一起:
>>> a = floor(10*random.random((2,2))) >>> a array([[ 1., 1.], [ 5., 8.]]) >>> b = floor(10*random.random((2,2))) >>> b array([[ 3., 3.], [ 6., 0.]]) >>> vstack((a,b)) array([[ 1., 1.], [ 5., 8.], [ 3., 3.], [ 6., 0.]]) >>> hstack((a,b)) array([[ 1., 1., 3., 3.], [ 5., 8., 6., 0.]])
函數column_stack
和raw_stack
可以將一維數組以列(行)的形式插入到二維數組中,其等價於1位數組的vstack
:
>>> column_stack((a,b)) # With 2D arrays array([[ 1., 1., 3., 3.], [ 5., 8., 6., 0.]]) >>> a=array([4.,2.]) >>> b=array([2.,8.]) >>> a[:,newaxis] # This allows to have a 2D columns vector array([[ 4.], [ 2.]]) >>> column_stack((a[:,newaxis],b[:,newaxis])) array([[ 4., 2.], [ 2., 8.]]) >>> vstack((a[:,newaxis],b[:,newaxis])) # The behavior of vstack is different array([[ 4.], [ 2.], [ 2.], [ 8.]])
Note
在一些復雜的例子中,r_[]
和 c_[]
將數字壓縮到數組中非常有用。他們允許使用range literals (":") :
>>> r_[1:4,0,4]
array([1, 2, 3, 0, 4])
Splitting one array into several smaller ones
Using hsplit, you can split an array along its horizontal axis, either by specifying the number of equally shaped arrays to return, or by specifying the columns after which the division should occur:
利用hsplit
和vsplit
,可以將矩陣按橫坐標和縱坐標進行分割,可以通過指定將數據等分為多少份或者指定分割后所在的列(行)進行分割:
>>> a = floor(10*random.random((2,12))) >>> a array([[ 8., 8., 3., 9., 0., 4., 3., 0., 0., 6., 4., 4.], [ 0., 3., 2., 9., 6., 0., 4., 5., 7., 5., 1., 4.]]) >>> hsplit(a,3) # Split a into 3 [array([[ 8., 8., 3., 9.], [ 0., 3., 2., 9.]]), array([[ 0., 4., 3., 0.], [ 6., 0., 4., 5.]]), array([[ 0., 6., 4., 4.], [ 7., 5., 1., 4.]])] >>> hsplit(a,(3,4)) # Split a after the third and the fourth column [array([[ 8., 8., 3.], [ 0., 3., 2.]]), array([[ 9.], [ 9.]]), array([[ 0., 4., 3., 0., 0., 6., 4., 4.], [ 6., 0., 4., 5., 7., 5., 1., 4.]])]
REFER:
https://github.com/wizardforcel/data-science-notebook/blob/master/numpy/NumPy%20%E4%BE%BF%E5%88%A9%E7%9A%84%E5%87%BD%E6%95%B0.md
http://scipy.github.io/old-wiki/pages/Tentative_NumPy_Tutorial#head-1529ae93dd5d431ffe3a1001a4ab1a394e70a5f2