目錄html
===python
import numpy as np np.random.seed(0) x1 = np.random.randint(10,size=6) x2 = np.random.randint(10,size=(3,4)) x3 = np.random.randint(10,size=(3,4,5)) print("x3 ndim:",x3.ndim) print("x3 shape:",x3.shape) print("x3 size:",x3.size) print("x3 dtype:",x3.dtype) print("x3 itemsize:",x3.itemsize,"bytes") print("x3 nbytes:",x3.nbytes,"bytes")
x3 ndim: 3 x3 shape: (3, 4, 5) x3 size: 60 x3 dtype: int32 x3 itemsize: 4 bytes x3 nbytes: 240 bytes
print(x1) print(x1[0]) print(x1[5])
[5 0 3 3 7 9] 5 9
print(x1[-1]) print(x1[-3])
9 3
print(x2) print(x2[1,2]) print(x2[2,1]) print(x2[-1,-2])
[[3 5 2 4] [7 6 8 8] [1 6 7 7]] 8 6 7
x2[0,0] = 23 print(x2)
[[23 5 2 4] [ 7 6 8 8] [ 1 6 7 7]]
# 切片格式: # x[start:stop:step] x = np.arange(10) print("原數組:\n",x) print("前5個元素:\n",x[:5]) print("索引5以後元素:\n",x[5:]) print("中間的子數組:\n",x[4:7]) print("每隔一個元素:\n",x[::2]) print("每隔一個元素,從索引1開始:\n",x[1::2]) print("全部元素,逆序:\n",x[::-1]) print("從索引4開始每隔一個元素逆序:\n",x[4::-2])
原數組: [0 1 2 3 4 5 6 7 8 9] 前5個元素: [0 1 2 3 4] 索引5以後元素: [5 6 7 8 9] 中間的子數組: [4 5 6] 每隔一個元素: [0 2 4 6 8] 每隔一個元素,從索引1開始: [1 3 5 7 9] 全部元素,逆序: [9 8 7 6 5 4 3 2 1 0] 從索引4開始每隔一個元素逆序: [4 2 0]
print("原數組:\n",x2) print("兩行三列:\n",x2[:2,:3]) print("全部行,每隔一列:\n",x2[:,::2]) print("行逆序:\n",x2[:,::-1]) print("列逆序:\n",x2[::-1,:]) print("行列逆序:\n",x2[::-1,::-1])
原數組: [[23 5 2 4] [ 7 6 8 8] [ 1 6 7 7]] 兩行三列: [[23 5 2] [ 7 6 8]] 全部行,每隔一列: [[23 2] [ 7 8] [ 1 7]] 行逆序: [[ 4 2 5 23] [ 8 8 6 7] [ 7 7 6 1]] 列逆序: [[ 1 6 7 7] [ 7 6 8 8] [23 5 2 4]] 行列逆序: [[ 7 7 6 1] [ 8 8 6 7] [ 4 2 5 23]]
print("原數組:\n",x2) print("獲取指定行:\n",x2[2,:]) print("獲取指定列:\n",x2[:,1]) print("獲取行時,能夠省略空的切片:\n",x2[0]) # 等價於 x2[0,:]
原數組: [[23 5 2 4] [ 7 6 8 8] [ 1 6 7 7]] 獲取指定行: [1 6 7 7] 獲取指定列: [5 6 6] 獲取行時,能夠省略空的切片: [23 5 2 4]
print("原數組:\n",x2) sub_arr = x2[:2,:2] print("抽取2×2的子數組:\n",sub_arr) sub_arr[0,0] = 88 print("修改後的子數組:\n",sub_arr) print("原數組:\n",x2) # 數組的視圖修改值會修改數組自己,它意味着在處理很是大的數據集時,能夠獲取或處理這些數據集的片斷,而不用複製底層的數據緩存。
原數組: [[88 5 2 4] [ 7 6 8 8] [ 1 6 7 7]] 抽取2×2的子數組: [[88 5] [ 7 6]] 修改後的子數組: [[88 5] [ 7 6]] 原數組: [[88 5 2 4] [ 7 6 8 8] [ 1 6 7 7]]
print("原數組;\n",x2) sub_arr_copy = x2[:2,:2].copy() print("拷貝2×2的子數組:\n",sub_arr_copy) sub_arr_copy[0,0] = 100 print("修改後的子數組:\n",sub_arr_copy) print("原數組;\n",x2)
原數組; [[88 5 2 4] [ 7 6 8 8] [ 1 6 7 7]] 拷貝2×2的子數組: [[88 5] [ 7 6]] 修改後的子數組: [[100 5] [ 7 6]] 原數組; [[88 5 2 4] [ 7 6 8 8] [ 1 6 7 7]]
# 數組的變形最靈活的方式是經過reshape() 函數來實現。 grid = np.arange(1,10).reshape(3,3) print("將數字1-9放入一個3×3的矩陣中:\n",grid) # 請注意,若是該方法可行,那麼原數組的大小必須和變形後的數組大小一致。若是知足這個條件, # reshape方法將會用到原數組的一個非副本視圖。但實際狀況是,在非連續的數據緩存的狀況下, # 返回非副本視圖每每不可能實現。 # 另外一種常見的變形模式時將一個一維數組變形爲二維數組的行或列的矩陣。你也能夠經過reshape # 方法來實現,或者更簡單的經過在一個切片操做中利用newaxis關鍵字: x = np.array([1,2,3]) print("原數組:\n",x) print("經過變形得到行向量:\n",x.reshape(1,3)) print("經過newaxis獲取行向量:\n",x[np.newaxis,:]) print("經過變形獲取列向量:\n",x.reshape((3,1))) print("經過newaxis獲取列向量:\n",x[:,np.newaxis])
將數字1-9放入一個3×3的矩陣中: [[1 2 3] [4 5 6] [7 8 9]] 原數組: [1 2 3] 經過變形得到行向量: [[1 2 3]] 經過newaxis獲取行向量: [[1 2 3]] 經過變形獲取列向量: [[1] [2] [3]] 經過newaxis獲取列向量: [[1] [2] [3]]
# 拼接numpy數組主要由 np.concatenate、np.vstack和np.hstack函數實現。 x = np.array([1,2,3]) y = np.array((7,6,15)) z = np.array([43,3,53]) arr_2n = np.array([[1,2,3], [4,5,6], [7,8,9]]) # 一維數組的拼接 print("拼接x和y數組:\n",np.concatenate([x,y])) # concatenate傳入元祖 print("拼接x和y和z數組:\n",np.concatenate((x,x,z))) # concatenate傳入列表 # 二維數組的拼接 print("沿第一個軸拼接:\n",np.concatenate([arr_2n,arr_2n])) print("沿第二個軸拼接:\n",np.concatenate((arr_2n,arr_2n),axis=1)) # 沿着固定維度處理數據時,使用np.vstack(垂直棧)和np.hstack(水平棧)函數會更簡潔: print("垂直棧數組:\n",np.vstack([x,arr_2n])) print("水平棧數組:\n",np.hstack([arr_2n,x[:,np.newaxis]]))
拼接x和y數組: [ 1 2 3 7 6 15] 拼接x和y和z數組: [ 1 2 3 1 2 3 43 3 53] 沿第一個軸拼接: [[1 2 3] [4 5 6] [7 8 9] [1 2 3] [4 5 6] [7 8 9]] 沿第二個軸拼接: [[1 2 3 1 2 3] [4 5 6 4 5 6] [7 8 9 7 8 9]] 垂直棧數組: [[1 2 3] [1 2 3] [4 5 6] [7 8 9]] 水平棧數組: [[1 2 3 1] [4 5 6 2] [7 8 9 3]]
# 分裂numpy數組主要由 np.split、np.vsplit和np.hsplit函數實現。 # 能夠向以上函數傳傳入一個索引列表做爲參數,索引列表記錄的是分裂點的位置: split_date = np.array([2,3,5,1,3,5,6,34,43,23,1]) grid = np.arange(16).reshape(4,4) print("原數組split_date:\n",split_date,"\n原數組grid:\n",grid) x1,x2,x3 = np.split(split_date,[3,5]) print("分裂後的數組:\n",x1,x2,x3) upper,lower = np.vsplit(grid,[2]) print("垂直分裂upper:\n",upper,"\n垂直分裂lower:\n",lower) left,right = np.hsplit(grid,[2]) print("水平分裂left:\n",left,"\n水平分裂right:\n",right)
原數組split_date: [ 2 3 5 1 3 5 6 34 43 23 1] 原數組grid: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] 分裂後的數組: [2 3 5] [1 3] [ 5 6 34 43 23 1] 垂直分裂upper: [[0 1 2 3] [4 5 6 7]] 垂直分裂lower: [[ 8 9 10 11] [12 13 14 15]] 水平分裂left: [[ 0 1] [ 4 5] [ 8 9] [12 13]] 水平分裂right: [[ 2 3] [ 6 7] [10 11] [14 15]]