1. 程式人生 > >numpy 基礎學習筆記(1)

numpy 基礎學習筆記(1)

NumPy的主要物件是同種元素的多維陣列。這是一個所有的元素都是一種型別、通過一個正整數元組索引的元素表格(通常是元素是數字)。在NumPy中維度(dimensions)叫做軸(axes),軸的個數叫做秩(rank)。
例如,在3D空間一個點的座標 [1, 2, 3] 是一個秩為1的陣列,因為它只有一個軸。那個軸長度為3.又例如,在以下例子中,陣列的秩為2(它有兩個維度).第一個維度長度為2,第二個維度長度為3.

[[ 1., 0., 0.],
 [ 0., 1., 2.]]

NumPy的陣列類被稱作 ndarray 。通常被稱作陣列。注意numpy.array和標準Python庫類array.array並不相同,後者只處理一維陣列和提供少量功能。更多重要ndarray物件屬性有:

  • ndarray.ndim
    陣列軸的個數,在python的世界中,軸的個數被稱作秩

  • ndarray.shape
    陣列的維度。這是一個指示陣列在每個維度上大小的整數元組。例如一個n排m列的矩陣,它的shape屬性將是(n,m),簡單說就是ndarray.shape = (行數,列數),這個元組的長度顯然是秩,即維度或者ndim屬性

  • ndarray.size
    陣列元素的總個數,等於shape屬性中元組元素的乘積。

  • ndarray.dtype
    一個用來描述陣列中元素型別的物件,可以通過創造或指定dtype使用標準Python型別。另外NumPy提供它自己的資料型別。

  • ndarray.itemsize
    陣列中每個元素的位元組大小。例如,一個元素型別為float64的陣列itemsiz屬性值為8(=64/8),又如,一個元素型別為complex32的陣列item屬性為4(=32/8).

  • ndarray.data
    包含實際陣列元素的緩衝區,通常我們不需要使用這個屬性,因為我們總是通過索引來使用陣列中的元素。

1.用numpy生成陣列

In[74]: a = np.arange(5)
In[75]: a.dtype
Out[75]: dtype('int32')
In[76]: a.shape
Out[76]: (5,)
In[77]: a
Out[77]: array([0, 1, 2, 3, 4])

2.建立多維陣列

In[78]: m = np.array([np.arange(2),np.arange(2)])
In[79]: print m
[[0 1]
 [0 1]]
In[80]: print m.shape (2, 2) In[81]: print m.dtype int32 In[82]: np.zeros(10) Out[82]: array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) In[83]: np.zeros((2,3)) Out[83]: array([[ 0., 0., 0.], [ 0., 0., 0.]]) In[84]: np.empty((2,3,2)) Out[84]: array([[[ 2.02566915e-322, 0.00000000e+000], [ 0.00000000e+000, 0.00000000e+000], [ 0.00000000e+000, 0.00000000e+000]], [[ 0.00000000e+000, 0.00000000e+000], [ 0.00000000e+000, 0.00000000e+000], [ 0.00000000e+000, 0.00000000e+000]]]) In[85]: np.arange(15) Out[85]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])

3.選取陣列元素

In[86]: a = np.array([[1,2],[3,4]])
In[87]: print a
[[1 2]
 [3 4]]
In[88]: print a[0][0]
1
In[91]: print a[0]
[1 2]
In[92]: print a[:][1]
[3 4]

4 numpy 資料型別

In[3]: import numpy as np
In[4]: print np.float64(42)
42.0
In[5]: print np.int8(42)
42
In[6]: print np.bool(42)
True
In[7]: print np.bool(0)
False
In[8]: print np.bool(42.0)
True
In[9]: print np.float(True)
1.0
In[10]: print np.float(False)
0.0
In[12]: print np.arange(7,dtype = np.uint16)
[0 1 2 3 4 5 6]
In[13]: try:
...     print np.int(42.0 + 1.j)
... except TypeError:
...     print 'TypeError'
...     
TypeError

5 資料型別轉換

In[14]: arr = np.array([1,2,3,4,5])
In[15]: arr
Out[15]: array([1, 2, 3, 4, 5])
In[16]: arr.astype(np.int32)
Out[16]: array([1, 2, 3, 4, 5])
In[17]: arr = np.array([3.7,-1.2,-2.6,0.5,12.9,10.1])
In[18]: arr
Out[18]: array([  3.7,  -1.2,  -2.6,   0.5,  12.9,  10.1])
In[19]: arr.astype(np.int32)
Out[19]: array([ 3, -1, -2,  0, 12, 10])
In[20]: arr.astype(np.float64)
Out[20]: array([  3.7,  -1.2,  -2.6,   0.5,  12.9,  10.1])
In[21]: arr.dtype
Out[21]: dtype('float64')
In[22]: numeric_strings = np.array(['1.24','-9.7','42'])
In[23]: numeric_strings
Out[23]: 
array(['1.24', '-9.7', '42'],
      dtype='|S4')
In[24]: numeric_strings.astype(float)
Out[24]: array([  1.24,  -9.7 ,  42.  ])

6 資料型別物件

In[25]: a = np.array([[1,2],[3,4]])
In[26]: print a
[[1 2]
 [3 4]]
In[27]: print a.dtype
int32
In[28]: print a.dtype.byteorder
=
In[29]: print a.dtype.itemsize
4

7 字元編碼

In[30]: print np.arange(7,dtype = 'f')
[ 0.  1.  2.  3.  4.  5.  6.]
In[31]: print np.arange(7,dtype = 'D')
[ 0.+0.j  1.+0.j  2.+0.j  3.+0.j  4.+0.j  5.+0.j  6.+0.j]
In[32]: print np.dtype(float)
float64
In[33]: print np.dtype('f')
float32
In[34]: print np.dtype('f8')
float64

8 dtype類的屬性

In[36]: t = np.dtype('Float64')
C:\Program Files (x86)\JetBrains\PyCharm 2016.2\helpers\pydev\pydevconsole.py:1: DeprecationWarning: Numeric-style type codes are deprecated and will result in an error in the future.
  '''
In[37]: print t.char
d
In[38]: print t.type
<type 'numpy.float64'>
In[39]: print t.str
<f8

9 建立自定義資料型別

In[40]: t = np.dtype([('name',np.str_,40),('numitems',np.int32),('price',np.float32)])
In[41]: print t
[('name', 'S40'), ('numitems', '<i4'), ('price', '<f4')]
In[42]: print t['name']
|S40
In[43]: print t
[('name', 'S40'), ('numitems', '<i4'), ('price', '<f4')]
In[44]: itemz = np.array([('Meaning of life DVD',52,3.14),('Butter',13,2.72)],dtype = t)
In[45]: print itemz
[('Meaning of life DVD', 52,  3.1400001 ) ('Butter', 13,  2.72000003)]
In[46]: print itemz[1]
('Butter', 13,  2.72000003)

10 陣列與標量的運算

In[47]: arr = np.array([[1.0,2.0,3.0],[4.0,5.0,6.0]])
In[48]: arr
Out[48]: 
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
In[49]: arr * arr
Out[49]: 
array([[  1.,   4.,   9.],
       [ 16.,  25.,  36.]])
In[50]: arr - arr
Out[50]: 
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
In[51]: 1/arr
Out[51]: 
array([[ 1.        ,  0.5       ,  0.33333333],
       [ 0.25      ,  0.2       ,  0.16666667]])
In[52]: arr ** 0.5
Out[52]: 
array([[ 1.        ,  1.41421356,  1.73205081],
       [ 2.        ,  2.23606798,  2.44948974]])