import numpy as np
ndarray.shape
a = numpy.arange(12) array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) a.shape=(2,6) [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11]] a.shape=(3,2,2) array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]]])
ndarray.ndim
數組維數,一維是1,二維是2...
ndarray.size
數組中的元素 數量,總的數據量 二維矩陣5行8列數量爲40
ndarray.itemsize
一個數組元素的 空間大小(字節)
ndarray.dtype
ndarray的類型python
np.bool 用一個字節存儲的布爾類型(True或False) 'b' np.int8 一個字節大小,-128 至 127 'i' np.int16 整數,-32768 至 32767 'i2' np.int32 整數,-2 31 至 2 32 -1 'i4' np.int64 整數,-2 63 至 2 63 - 1 'i8' np.uint8 無符號整數,0 至 255 'u' np.uint16 無符號整數,0 至 65535 'u2' np.uint32 無符號整數,0 至 2 ** 32 - 1 'u4' np.uint64 無符號整數,0 至 2 ** 64 - 1 'u8' np.float16 半精度浮點數:16位,正負號1位,指數5位,精度10位 'f2' np.float32 單精度浮點數:32位,正負號1位,指數8位,精度23位 'f4' np.float64 雙精度浮點數:64位,正負號1位,指數11位,精度52位 'f8' np.complex64 複數,分別用兩個32位浮點數表示實部和虛部 'c8' np.complex128 複數,分別用兩個64位浮點數表示實部和虛部 'c16' np.object_ python對象 'O' np.string_ 字符串 'S' np.unicode_ unicode類型 'U'
建立數組的時候指定類型數組
a = np.array([[1, 2, 3],[4, 5, 6]], dtype=np.float32) a.dtype dtype('float32') arr = np.array(['python', 'tensorflow', 'scikit-learn', 'numpy'], dtype = np.string_) arr array([b'python', b'tensorflow', b'scikit-learn', b'numpy'], dtype='|S12')
若不指定,整數默認int64,小數默認float64
字符串 np.string_dom
建立
ui
a = numpy.arange(15) b = a.reshape(3,5) [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] a.reshape((3,-1)) [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] a.reshape((5,-1)) [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11] [12 13 14]] c = a.reshape((3,-1)) e = c.T c [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] e [[ 0 5 10] [ 1 6 11] [ 2 7 12] [ 3 8 13] [ 4 9 14]]
生成隨機數組
數組的索引、切片
形狀修改
類型修改
spa
建立配置文件 jupyter notebook --generate-config vi ~/.jupyter/jupyter_notebook_config.py 取消註釋,多增長 ## (bytes/sec) Maximum rate at which messages can be sent on iopub before they # are limited. c.NotebookApp.iopub_data_rate_limit = 10000000
數組去重
3d
ndarray運算
code
a = numpy.array([1,2,3,4]) print(a==4) >> [False False False True] numpy會用矩陣對象的每個數據和值做比較,返回布爾值 b = numpy.array([[1,2,3,4],[2,3,4,5]]) print(b == 5) >> [[False False False False] [False False False True]] b = numpy.array([[1,2,3,4],[2,3,4,5]]) c = (b==5) print(b[c]) >>[5]
a = numpy.array([1,2,3,0]) print(a.min()) .>>> 0 print(a.max()) >>> 3
數組間運算
對象
a = numpy.array([[1,5],[5,0]]) b = numpy.array([[2,8],[1,10]]) 二維矩陣運算 a [[1 5] [5 0]] b [[ 2 8] [ 1 10]] 2 numpy.dot(a,b) a.dot(b) 規則: 第一行乘第一列相加獲得矩陣第一位 第一行乘第二列相加獲得矩陣第二位 第二行乘第一列相加獲得矩陣第三位 第二行乘第二列相加獲得矩陣第四位 1*2 +5*1 =7 1*8 + 5*10 = 58 5*2 +0*1 =10 5*8 +0*10 = 40 [[ 7 58] [10 40]]
a = numpy.array([1,2,3,4]) print(a,a.dtype) >>>> [1 2 3 4] int32 a = numpy.array([1,2,3,4.0]) print(a,a.dtype) >>> [1. 2. 3. 4.] float64 a = numpy.array([1,2,3,'4']) print(a,a.dtype) >>> ['1' '2' '3' '4'] <U11 a = numpy.array([1,2,3,4]) a >> [1 2 3 4] b = numpy.array([[1,2,3,4],[2,3,4,5]]) b >> [[1 2 3 4] [2 3 4 5]] c = numpy.array([[[1,2,3,4],[2,3,4,5],[5,6,7,8]]]) c >> [[[1 2 3 4] [2 3 4 5] [5 6 7 8]]]
a = numpy.array([1,2,3,4]) print(a[1:3]) >> [2 3] a = numpy.array([[ 1 3 4 6 7] [ 2 4 2 5 76] [41 13 42 63 71]]) print(a[0:2]) >> [[ 1 3 4 6 7] [ 2 4 2 5 76]] print(a[0:2,2]) 可取列值 >> [4 2] b = a[:,0:2] 全部行第0列和第1列的數據 [[ 1 3] [ 2 4] [41 13]]
按照行 c = numpy.array( [[ [10,20,30,40], [20,30,40,50], [50,60,70,80] ]]) 1 print(c.sum(axis=1)) >> [100 140 260] 三維 c = numpy.array( [[ [10,20,30,40], [20,30,40,50], [50,60,70,80] ]]) 2 print(c.sum(axis=2)) >> [[100 140 260]] 按照列 c = numpy.array( [[ [10,20,30,40], [20,30,40,50], [50,60,70,80] ]]) 1 print(c.sum(axis=1)) >> [100 140 260] 三維 c = numpy.array( [[ [10,20,30,40], [20,30,40,50], [50,60,70,80] ]]) 2 print(c.sum(axis=2)) >> [[100 140 260]]
a = numpy.arange(15) print(a) [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] a = numpy.arange(2,30,2) [ 2 4 6 8 10 12 14 16 18 20 22 24 26 28] 不包括30 numpy.linspace(0,100,11) [ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] 初始化 a = numpy.zeros((4,)) print(a) [0. 0. 0. 0.] a = numpy.zeros((4,6)) print(a) [[0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0.]] numpy.ones((元組)) a = numpy.ones((2,3,4)) print(a) [[[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]] [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]]] 三維 3行 4列
隨機
numpy.random.random((2,3))
生成2行3列的矩陣 介於0-1之間blog
[ [0.07261529 0.37003586 0.00799021] [0.24945076 0.92461768 0.80258728] ]
e的次冪計算 a = numpy.arange(3) numpy.exp(a)) 分別得出 e的0次 e的1次 e的2次 e=2.7182 [1. 2.71828183 7.3890561 ] 求開方 a = numpy.sqrt(81) a 9.0 type(a) <class 'numpy.float64'> a.dtype float64 a = numpy.arange(3) numpy.sqrt(a) [0. 1. 1.41421356] 以矩陣傳入參數,得出每一個數據的開方
numpy.floor(矩陣) a = numpy.array([1.3,2.4,5.6,6.1,6.9]) [1.3 2.4 5.6 6.1 6.9] b = numpy.floor(a) [1. 2. 5. 6. 6.] 橫向拼接 a = numpy.floor(10*numpy.random.random((2,3))) b = numpy.floor(10*numpy.random.random((2,3))) a [[9. 1. 6.] [1. 3. 8.]] b [[7. 7. 0.] [1. 9. 8.]] numpy.hstack((a,b)) [[9. 1. 6. 7. 7. 0.] [1. 3. 8. 1. 9. 8.]] 縱向拼接 [[1. 0. 6.] [0. 8. 8.]] [[4. 8. 2.] [9. 7. 4.]] vstack [[1. 0. 6.] [0. 8. 8.] [4. 8. 2.] [9. 7. 4.]] 縱向切分 [[3. 4. 4. 2. 9. 3.] [7. 9. 1. 1. 8. 7.] [8. 8. 6. 2. 2. 7.] [5. 5. 8. 7. 8. 1.]] numpy.vsplit(a,2) [ array([[3., 4., 4., 2., 9., 3.], [7., 9., 1., 1., 8., 7.]]), array([[8., 8., 6., 2., 2., 7.], [5., 5., 8., 7., 8., 1.]]) ] numpy.hsplit(a,(3,4)) a= numpy.floor(10 * numpy.random.random((2, 12))) [[3. 8. 3. 5. 1. 9. 7. 5. 9. 1. 1. 0.] [0. 4. 3. 9. 9. 0. 0. 2. 8. 1. 0. 7.]] numpy.hsplit(a,(3,5)) [ array([[3., 8., 3.], [0., 4., 3.]]), array([[5., 1.], [9., 9.]]), array([[9., 7., 5., 9., 1., 1., 0.], [0., 0., 2., 8., 1., 0., 7.]]) ] 在3-5構成第二個array view 淺複製 a = numpy.arange(12) b = a.view() b is a false id 不同 b.shape=2,6 改變b的結構 [[ 0 1 2 3 1234 5] [ 6 7 8 9 10 11]] a [ 0 1 2 3 4 5 6 7 8 9 10 11] b[0,4] =1234 改變b中一個值 b [[ 0 1 2 3 1234 5] [ 6 7 8 9 10 11]] a [ 0 1 2 3 1234 5 6 7 8 9 10 11] copy 深複製 a = numpy.arange(12) b = a.copy() [ 0 1 2 3 4 5 6 7 8 9 10 11] [ 0 1 2 3 4 5 6 7 8 9 10 11] b.shape=3,4 [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] b[0,1] = 32 [[ 0 32 2 3] [ 4 5 6 7] [ 8 9 10 11]] a [ 0 1 2 3 4 5 6 7 8 9 10 11] a.argmax(axis=0) a = numpy.array([[1,3,61,5],[4,2,55,6]]) [[ 1 3 61 5] [ 4 2 55 6]] index = a.argmax(axis=0) [1 0 0 1] 按照列, 第一列 索引1 的4大 第二列 索引0 的3大 。。。。。 得出 1001 按照索引取出相應值 max = a[index,range(a.shape[1])] [ 4 3 61 6] a.argmax(axis=1) index1 = a.argmax(axis=1) [2 2]
a = numpy.array([[1,3,61,5],[4,2,55,6]]) [[ 1 3 61 5] [ 4 2 55 6]] b = numpy.sort(a) [[ 1 3 5 61] [ 2 4 6 55]]