Numpy:基礎

1.Numpy中的ndarray是一個多維數組對象,其包含了數組中的實際數據和描述該數組的元數據。大部分數組的操作僅僅改變元數據的部分,而不改變實際的數據。

>>> 
#維度元素大小不同的情況
m=numpy.array([numpy.arange(2),numpy.arange(3)])
>>> m
array([array([0, 1]), array([0, 1, 2])], dtype=object)
>>> m.shape
(2,)
>>> 
#維度元素大小相同的情況
m=numpy.array([numpy.arange(2),numpy.arange(2)])
>>> 
>>> m
array([[0, 1],
       [0, 1]])
>>> m.dtype##數組中數據的類型
dtype('int32')
>>> m.shape##數組的維度信息
(2, 2)

2.數組中的元素選取使用array[dimen1,dimen2….]這種格式

>>> k
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])
>>> k.shape
(3, 3)
>>> k.dtype
dtype('int32')
>>> k[0,0]
0
>>> k[2,2]
2
>>> k[0,]##取出整個維度的數據
array([0, 1, 2])
>>> k[,2]#想去每一個維度同一位置的做法卻不可取
SyntaxError: invalid syntax
>>> 

3.Numpy中的數據類型

numpy中許多函數是可以指定數據類型的,比如arange(),array(),ndarray()。數據類型對象是numpy.dtype類的實例。如前所述,NumPy數組是有數據類型的,更確切
地說,NumPy數組中的每一個元素均爲相同的數據類型。數據類型對象可以給出單個數組元素在
內存中佔用的字節數,即dtype類的itemsize屬性:

>>> p=numpy.arange(5,dtype=numpy.float32)
>>> p
array([ 0.,  1.,  2.,  3.,  4.], dtype=float32)
#數據類型對象的大小
>>> p.dtype.itemsize
4
  1. Numpy 中的數據類型編碼
    這裏寫圖片描述
Numpy中的數據類型列表,數據類型對象有多個屬性,其中包括類型編碼,在內存中的存儲格式等。
>>> numpy.sctypeDict.keys()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 'unicode', 23, 'cfloat', 'longfloat', 'Int32', 'Complex64', 'unicode_', 'complex', 'timedelta64', 'uint16', 'c16', 'float32', 'int32', 'D', 'float96', 'H', 'void', 'unicode0', 'L', 'P', 'half', 'void0', 'd', 'h', 'l', 'p', 22, 'Timedelta64', 'object0', 'b1', 'M8', 'String0', 'float16', 'ulonglong', 'i1', 'uint32', '?', 'Void0', 'complex64', 'G', 'O', 'UInt8', 'S', 'byte', 'UInt64', 'g', 'float64', 'ushort', 'Complex96', 'float_', 'uint', 'object_', 'Float16', 'complex_', 'Unicode0', 'uintp', 'intc', 'csingle', 'datetime64', 'float', 'bool8', 'Bool', 'intp', 'uintc', 'bytes_', 'u8', 'Float96', 'u4', 'int_', 'cdouble', 'u1', 'complex128', 'u2', 'f8', 'Datetime64', 'ubyte', 'm8', 'complex192', 'B', 'uint0', 'F', 'bool_', 'uint8', 'c8', 'Int64', 'Int8', 'Complex32', 'V', 'int8', 'uint64', 'b', 'c24', 'f', 'double', 'UInt32', 'clongdouble', 'str', 'f12', 'f2', 'f4', 'int', 'longdouble', 'single', 'string', 'q', 'Int16', 'Float64', 'longcomplex', 'UInt16', 'bool', 'Float32', 'string0', 'longlong', 'i8', 'int16', 'str_', 'I', 'object', 'M', 'i4', 'singlecomplex', 'Q', 'string_', 'U', 'a', 'short', 'e', 'i', 'clongfloat', 'm', 'Object0', 'int64', 'i2', 'int0']

numpyz中允許自定義異構數據類型。

##自定義異構數據類型y
>>> y=numpy.dtype([('name',str,40),('numitem',numpy.int32),('price',numpy.float32)])
>>> y
dtype([('name', 'S40'), ('numitem', '<i4'), ('price', '<f4')])
>>> y.str
'|V48'
###使用自定義數據類型定義數組
>>> item=numpy.array([("onething",001,34.3),("otherthing",002,34.5),("onemorething",003,45.6)],dtype=y)
>>> item
array([('onething', 1, 34.29999923706055), ('otherthing', 2, 34.5),
       ('onemorething', 3, 45.599998474121094)], 
      dtype=[('name', 'S40'), ('numitem', '<i4'), ('price', '<f4')])
>>> 

5.numpy中的array可以使用reshape()函數或者resize()函數,不同的是resize()函數會對原數組直接修改。reshape()對原數組不影響。重構數組爲多維數組,重構後的數組可以使用原python中的切片索引對數組進行操作。比如o=numpy.arange(24).reshape(2,3,4)就可以把一維的數組重構爲三維的數組。重構時要注意數組元素個數的一致性。對重構後的數組切片操作和原python類似。
6.numpy.array的高維對象可以使用arrayobject.reval()(函數只是返回一個視圖顯示效果)或arrayobject.flatten()(函數返回的結果會有相應內存分配存儲數據)函數使高維數組展位一維。

>>> i
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> i[...,1]
#ravel和flatten返回值形式無區別,內存結構有區別
array([[ 1,  5,  9],
       [13, 17, 21]])
>>> i.flatten()
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])  
>>> i.ravel()
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])         

7numpy中支持對數組進行組合操作。我們按照水平、垂直和深度等方式進行了組合數組的操作。我們使用了vstack(垂直組合,垂直方向組合兩個數組)、dstack(深度組合,兩個元組組合爲一個列表元素)、hstack(水平方向上組合)、column_stack(列方向組合數組,這個特別點)、row_stack(行方向組合數組,這個特別點)以及concatenate(可以設置哪個軸方向組合數組)函數。vstack垂直組合實際上是axis=1軸上的組合,hstack實際上是axis=0方向上的組合。二維以上的數組,row_stack和vstack的效果一樣而column_stack和hstack的效果一樣。

>>> numpy.column_stack((a,b))
array([[[  0,   1,   2,   3],
        [  4,   5,   6,   7],
        [  8,   9,  10,  11],
        [  0,   1,   4,   9],
        [ 16,  25,  36,  49],
        [ 64,  81, 100, 121]],

       [[ 12,  13,  14,  15],
        [ 16,  17,  18,  19],
        [ 20,  21,  22,  23],
        [144, 169, 196, 225],
        [256, 289, 324, 361],
        [400, 441, 484, 529]]])
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b
array([[[  0,   1,   4,   9],
        [ 16,  25,  36,  49],
        [ 64,  81, 100, 121]],

       [[144, 169, 196, 225],
        [256, 289, 324, 361],
        [400, 441, 484, 529]]])
 >>> numpy.row_stack((a,b))==numpy.vstack((a,b))
array([[[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]],

       [[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]],

       [[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]],

       [[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]]], dtype=bool)
>>> 

8.Numpy可以使用hsplit,vsplit,dsplit或者split對數組按照特定的條件分爲子數組。hsplit水平方向上的分隔,相當於axis=0方向的分隔,numpy.hsplit(array,num)其中num數字爲該維能均分的數字。numpy.vsplit(array,num)與hsplit一樣,只是維度爲axis=1。numpy.split(array,num,axis=axisnum)。numpy.dsplit(array,num)只用於最少三維的數組進行分隔。

9.數組中的屬性。array.dtype求元素的數據類型, array.ndim求數組的軸數, array.size求數組中共有多少個元素,array.T求數組的轉置(一維數組的裝置是其本身),array.nbytes求整個數組元素所佔的內存大小,array.itemsize求單個數組元素所佔內存大小,array.real求數組的實部,array.imag求數組的虛部
array.flat會返回一個numpy.flatiter對象,該對象是“扁平迭代器”能遍歷整個多維數組。
這裏寫圖片描述

>>> a=numpy.arange(12).reshape(3,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> a.flat
<numpy.flatiter object at 0x02F86940>
>>> for i in a.flat:##迭代器
    print i


0
1
2
3
4
5
6
7
8
9
10
11
>>> a.flat[4]=89##可以對flatiter裏邊的單個元素賦值
>>> a
array([[ 0,  1,  2,  3],
       [89,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> 

10.可以使用array.tolist()函數使numpy數組轉換爲python列表。使用array.astype(dtype)轉換數據類型,若是複數轉爲整數會丟失虛部。

>>> a
array([[ 0,  1,  2,  3],
       [89,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> a.tolist()
[[0, 1, 2, 3], [89, 5, 6, 7], [8, 9, 10, 11]]
>>> a.astype(float)
array([[  0.,   1.,   2.,   3.],
       [ 89.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.]])
>>> 

11.這裏學習了numpy中的數據類型和數組。對於數組而言,有很多屬性可以用來描述數組,數據類型就是其中之一。在NumPy中,數組的數據類型是用對象來表示的。類似於Python列表,NumPy數組也可以方便地進行切片和索引操作。在多維數組上,NumPy有明顯的優勢。涉及改變數組維度的操作有很多種——組合、調整、設置維度和分割等。在這一章中,對很多改變數組維度的實用函數進行了說明。

發佈了30 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章