Python資料分析基礎——Numpy tutorial

來源:互聯網
上載者:User

標籤:二維數組   span   資料分析   概念   exp   1.2   nump   def   切片   

參考link  https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

基礎

Numpy主要用於處理多維陣列,數組中元素通常是數字,索引值為自然數

在Numpy中,維度被稱為axes,axes的總數為rank (秩)

(關於矩陣秩的概念,可以參考https://www.zhihu.com/question/21605094 與 

https://www.applysquare.com/topic-cn/78QfWkiPt/)

Numpy的數組類稱為 ndarray,別名array

(numpy.array與 array.array不同,後者只處理一維數組)

ndarray屬性

1. ndim

返回數組的秩

2.shape

返回數組各個維度大小

3.size

數組所有元素總個數,與shape結果相等

4.dtype

數組元素類型

5.itemsize

位元組表示的元素類型大小,與ndarray.dtype.itemsize相等

int32 -> 4 (32/8)  int64 -> 8 (64/8)

6.data

包含數組實際元素的緩衝區,通常不使用

舉例

 1 import numpy as np 2 a=np.arange(15).reshape(3,5) 3 print(a) 4  5 out: 6 array([[ 0,  1,  2,  3,  4], 7        [ 5,  6,  7,  8,  9], 8        [10, 11, 12, 13, 14]]) 9 10 print(type(a))11 print(a.shape)12 print(a.ndim)13 print(a.dtype.name)14 print(a.item.size)15 16 out:17 numpy.ndarray18 (3,5)19 220 ‘int32‘21 4

建立數組

 1 #將列錶轉換為array 2 >>> import numpy as np 3 >>> a = np.array([2,3,4]) 4 >>> a 5 array([2, 3, 4]) 6 >>> a.dtype 7 dtype(‘int32‘) 8 >>> b = np.array([1.2, 3.5, 5.1]) 9 >>> b.dtype10 dtype(‘float64‘)11 12 #建立二維數組array13 >>> b = np.array([(1.5,2,3), (4,5,6)])14 >>> b15 array([[ 1.5,  2. ,  3. ],16        [ 4. ,  5. ,  6. ]])17 18 #建立一維數組19 >>> np.arange( 10, 30, 5 )20 array([10, 15, 20, 25])21 >>> np.arange( 0, 2, 0.3 )               22 array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])23 24 #使用linespace建立數組25 >>> from numpy import pi26 >>> np.linspace( 0, 2, 9 )         27 array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])28 >>> x = np.linspace( 0, 2*pi, 100 )        29 >>> f = np.sin(x)
 1 >>> b = np.arange(12).reshape(3,4) 2 >>> b 3 array([[ 0,  1,  2,  3], 4        [ 4,  5,  6,  7], 5        [ 8,  9, 10, 11]]) 6 >>> 7 >>> b.sum(axis=0)                   # 按列求和 8 array([12, 15, 18, 21]) 9 >>> b.sum(axis=1)                  # 按行求和10 array([ 6, 22, 38])11 >>> b.min(axis=1)                   # 每行最小值12 array([0, 4, 8])13 >>> b.cumsum(axis=1)            # 按列累積求和14 array([[ 0,  1,  3,  6],15        [ 4,  9, 15, 22],16        [ 8, 17, 27, 38]])
 1 # 常用函數 2 >>> B = np.arange(3) 3 >>> B 4 array([0, 1, 2]) 5 >>> np.exp(B) 6 array([ 1.  ,  2.71828183,  7.3890561 ]) 7 >>> np.sqrt(B) 8 array([ 0.  ,  1.  ,  1.41421356]) 9 >>> C = np.array([2., -1., 4.])10 >>> np.add(B, C)11 array([ 2.,  0.,  6.])
 1 # 索引、切片、迭代 2 # 一維數組 3 >>a=np.arange(10)**3 4 >>print(a) 5 array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729], dtype=int32) 6 >>a[2] 7 8 8 >>a[2:5] 9 array([ 8, 27, 64], dtype=int32)10 >>a[:6:2]=-10011 >>a12 array([-10,   1, -10,  27, -10, 125, 216, 343, 512, 729], dtype=int32)13 >>a[::-1]14 array([729, 512, 343, 216, 125, -10,  27, -10,   1, -10], dtype=int32)15 16 #多維陣列17 >>> def f(x,y):18 ...     return 10*x+y19 ...20 >>> b = np.fromfunction(f,(5,4),dtype=int)21 >>> b22 array([[ 0,  1,  2,  3],23        [10, 11, 12, 13],24        [20, 21, 22, 23],25        [30, 31, 32, 33],26        [40, 41, 42, 43]])27 >>> b[2,3]28 2329 >>> b[0:5, 1]                       #第二列30 array([ 1, 11, 21, 31, 41])31 >>> b[ : ,1]                        #第二列32 array([ 1, 11, 21, 31, 41])33 >>> b[1:3, : ]                      #第二行、第三行34 array([[10, 11, 12, 13],35        [20, 21, 22, 23]])36 >>b[-1]                             #相當於 b[-1,:],最後一行37 array([40, 41, 42, 43])

 

Python資料分析基礎——Numpy tutorial

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.