Python中的Numpy入門教程_python

來源:互聯網
上載者:User

1、Numpy是什麼

很簡單,Numpy是Python的一個科學計算的庫,提供了矩陣運算的功能,其一般與Scipy、matplotlib一起使用。其實,list已經提供了類似於矩陣的表示形式,不過numpy為我們提供了更多的函數。如果接觸過matlab、scilab,那麼numpy很好入手。 在以下的程式碼範例中,總是先匯入了numpy:

複製代碼 代碼如下:

>>> import numpy as np
>>> print np.version.version
1.6.2


2、多維陣列

多維陣列的類型是:numpy.ndarray。

使用numpy.array方法

以list或tuple變數為參數產生一維數組:

複製代碼 代碼如下:
>>> print np.array([1,2,3,4])
[1 2 3 4]
>>> print np.array((1.2,2,3,4))
[ 1.2  2.   3.   4. ]
>>> print type(np.array((1.2,2,3,4)))
<type 'numpy.ndarray'>

以list或tuple變數為元素產生二維數組:
複製代碼 代碼如下:

>>> print np.array([[1,2],[3,4]])
[[1 2]
 [3 4]]

產生數組的時候,可以指定資料類型,例如numpy.int32, numpy.int16, and numpy.float64等:
複製代碼 代碼如下:

>>> print np.array((1.2,2,3,4), dtype=np.int32)
[1 2 3 4]

使用numpy.arange方法
複製代碼 代碼如下:

>>> print np.arange(15)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
>>> print type(np.arange(15))
<type 'numpy.ndarray'>
>>> print np.arange(15).reshape(3,5)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
>>> print type(np.arange(15).reshape(3,5))
<type 'numpy.ndarray'>

使用numpy.linspace方法

例如,在從1到3中產生9個數:

複製代碼 代碼如下:

>>> print np.linspace(1,3,9)
[ 1.    1.25  1.5   1.75  2.    2.25  2.5   2.75  3.  ]

使用numpy.zeros,numpy.ones,numpy.eye等方法可以構造特定的矩陣

例如:

複製代碼 代碼如下:

>>> print np.zeros((3,4))
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
>>> print np.ones((3,4))
[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]
>>> print np.eye(3)
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

建立一個三維數組:
複製代碼 代碼如下:

>>> print np.zeros((2,2,2))
[[[ 0.  0.]
  [ 0.  0.]]

 [[ 0.  0.]
  [ 0.  0.]]]


擷取數組的屬性:
複製代碼 代碼如下:

>>> a = np.zeros((2,2,2))
>>> print a.ndim   #數組的維數
3
>>> print a.shape  #數組每一維的大小
(2, 2, 2)
>>> print a.size   #數組的元素數
8
>>> print a.dtype  #元素類型
float64
>>> print a.itemsize  #每個元素所佔的位元組數
8


數組索引,切片,賦值

樣本:

複製代碼 代碼如下:

>>> a = np.array( [[2,3,4],[5,6,7]] )
>>> print a
[[2 3 4]
 [5 6 7]]
>>> print a[1,2]
7
>>> print a[1,:]
[5 6 7]
>>> print a[1,1:2]
[6]
>>> a[1,:] = [8,9,10]
>>> print a
[[ 2  3  4]
 [ 8  9 10]]

使用for操作元素
複製代碼 代碼如下:

>>> for x in np.linspace(1,3,3):
...     print x
...
1.0
2.0
3.0


基本的數組運算

先構造數組a、b:

複製代碼 代碼如下:

>>> a = np.ones((2,2))
>>> b = np.eye(2)
>>> print a
[[ 1.  1.]
 [ 1.  1.]]
>>> print b
[[ 1.  0.]
 [ 0.  1.]]

數組的加減乘除:
複製代碼 代碼如下:

>>> print a > 2
[[False False]
 [False False]]
>>> print a+b
[[ 2.  1.]
 [ 1.  2.]]
>>> print a-b
[[ 0.  1.]
 [ 1.  0.]]
>>> print b*2
[[ 2.  0.]
 [ 0.  2.]]
>>> print (a*2)*(b*2)
[[ 4.  0.]
 [ 0.  4.]]
>>> print b/(a*2)
[[ 0.5  0. ]
 [ 0.   0.5]]
>>> print (a*2)**4
[[ 16.  16.]
 [ 16.  16.]]

 使用數組對象內建的方法:

複製代碼 代碼如下:

>>> a.sum()
4.0
>>> a.sum(axis=0)   #計算每一列(二維數組中類似於矩陣的列)的和
array([ 2.,  2.])
>>> a.min()
1.0
>>> a.max()
1.0

使用numpy下的方法:

複製代碼 代碼如下:

>>> np.sin(a)
array([[ 0.84147098,  0.84147098],
       [ 0.84147098,  0.84147098]])
>>> np.max(a)
1.0
>>> np.floor(a)
array([[ 1.,  1.],
       [ 1.,  1.]])
>>> np.exp(a)
array([[ 2.71828183,  2.71828183],
       [ 2.71828183,  2.71828183]])
>>> np.dot(a,a)   ##矩陣乘法
array([[ 2.,  2.],
       [ 2.,  2.]])


合并數組

使用numpy下的vstack和hstack函數:

複製代碼 代碼如下:

>>> a = np.ones((2,2))
>>> b = np.eye(2)
>>> print np.vstack((a,b))
[[ 1.  1.]
 [ 1.  1.]
 [ 1.  0.]
 [ 0.  1.]]
>>> print np.hstack((a,b))
[[ 1.  1.  1.  0.]
 [ 1.  1.  0.  1.]]

看一下這兩個函數有沒有涉及到淺拷貝這種問題:

複製代碼 代碼如下:

>>> c = np.hstack((a,b))
>>> print c
[[ 1.  1.  1.  0.]
 [ 1.  1.  0.  1.]]
>>> a[1,1] = 5
>>> b[1,1] = 5
>>> print c
[[ 1.  1.  1.  0.]
 [ 1.  1.  0.  1.]]

可以看到,a、b中元素的改變並未影響c。


深拷貝數組

數組對象內建了淺拷貝和深拷貝的方法,但是一般用深拷貝多一些:

複製代碼 代碼如下:
>>> a = np.ones((2,2))
>>> b = a
>>> b is a
True
>>> c = a.copy()  #深拷貝
>>> c is a
False

基本的矩陣運算

轉置:

複製代碼 代碼如下:

>>> a = np.array([[1,0],[2,3]])
>>> print a
[[1 0]
 [2 3]]
>>> print a.transpose()
[[1 2]
 [0 3]]

跡:
複製代碼 代碼如下:
>>> print np.trace(a)
4

numpy.linalg模組中有很多關於矩陣運算的方法:
複製代碼 代碼如下:

>>> import numpy.linalg as nplg

特徵值、特徵向量:

複製代碼 代碼如下:

>>> print nplg.eig(a)
(array([ 3.,  1.]), array([[ 0.        ,  0.70710678],
       [ 1.        , -0.70710678]]))

3、矩陣

numpy也可以構造矩陣對象,這裡不做討論。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.