numpy 中不常用的一些方法


作者:代碼律動
鏈接:https://zhuanlan.zhihu.com/p/36303821
來源:知乎
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

挑戰 1:引入 numpy 並查看 numpy 的版本。

要求:這是第一步,以后我們使用 numpy 時都將用別名 np。

# 答案 import numpy as np print(np.__version__) #> 1.13.3 

 

挑戰 2:創建數組

要求:創建一維數組,內容為從 0 到 9。

# 輸入數組 arr = np.arange(10) 

 

挑戰 3:創建布爾數組

要求:數組大小為 3*3,全部為 True。

# 答案一: np.full((3, 3), True, dtype=bool) # 答案二: np.ones((3,3), dtype=bool) 

 

挑戰 4:按要求抽取數組中的元素

要求:原數組為一維數組,內容為從 0 到 9,抽取出所有奇數。

# 輸入數組 arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # 答案 arr[arr % 2 == 1] #> array([1, 3, 5, 7, 9]) 

 

挑戰 5:按要求修改數組中的元素(原地修改)

要求:原數組為一維數組,內容為從 0 到 9,將所有奇數原地修改為 -1。

# 輸入數組 arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # 答案 arr[arr % 2 == 1] = -1 #> array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1]) 

 

挑戰 6:按要求修改數組中的元素(返回新數組)

要求:原數組為一維數組,內容為從 0 到 9,返回一個該數組的拷貝,其中奇數修改為 -1。

# 輸入數組 arr = np.arange(10) # 答案 out = np.where(arr % 2 == 1, -1, arr) #> array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1]) 

 

挑戰 7:修改數組的形狀

要求:將給定的一維數組 reshape 為二維數組,其中新數組的行數為2。

# 輸入數組 arr = np.arange(10) # 答案 arr.reshape(2, -1) # -1 表示自動計算該維度的大小 #> array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) 

 

挑戰 8:合並數組(列方向)

要求:將給定數組在列方向上合並。

# 輸入數組 a = np.arange(10).reshape(2,-1) b = np.repeat(1, 10).reshape(2,-1) # 答案 1: np.concatenate([a, b], axis=0) # 答案 2: np.vstack([a, b]) # 答案 3: np.r_[a, b] #> array([[0, 1, 2, 3, 4], #> [5, 6, 7, 8, 9], #> [1, 1, 1, 1, 1], #> [1, 1, 1, 1, 1]]) 

 

挑戰 9:合並數組(水平方向)

要求:將給定數組在水平方向上合並。

# 輸入數組 a = np.arange(10).reshape(2,-1) b = np.repeat(1, 10).reshape(2,-1) # 答案 1: np.concatenate([a, b], axis=1) # 答案 2: np.hstack([a, b]) # 答案 3: np.c_[a, b] #> array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1], #> [5, 6, 7, 8, 9, 1, 1, 1, 1, 1]]) 

 

挑戰 10:創建數組(進階)

要求:不用硬編碼,使用內置方法,從給定數組 a 生成數組 b。

# 輸入數組 a = np.array([1,2,3]) b = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]) # 答案 np.r_[np.repeat(a, 3), np.tile(a, 3)] 

 

挑戰 11:返回公共元素

要求:給定兩個數組,要求返回這兩個數組元素的交集。

# 輸入數組 a = np.array([1,2,3,2,3,4,3,4,5,6]) b = np.array([7,2,10,2,7,4,9,4,9,8]) # 答案: np.intersect1d(a,b) #> array([2, 4]) 

 

挑戰 12:刪除元素

要求:給定兩個數組 a、b,從數組 a 中刪除 b 中出現的元素。

# 輸入數組 a = np.array([1,2,3,4,5]) b = np.array([5,6,7,8,9]) # 答案 np.setdiff1d(a,b) #> array([1, 2, 3, 4]) 

 

挑戰 13:找出相同元素

要求:給定兩個數組 a、b,返回兩數組中相同元素的下標。

# 輸入數 a = np.array([1,2,3,2,3,4,3,4,5,6]) b = np.array([7,2,10,2,7,4,9,4,9,8]) # 答案: np.where(a == b) #> (array([1, 3, 5, 7]),) 

 

挑戰 14:按要求取出元素

要求:從數組中取出大於等於 5 且小於等於 10 的元素。

# 輸入數組 a = np.arange(15) # 答案 1: index = np.where((a >= 5) & (a <= 10)) a[index] # 答案 2: index = np.where(np.logical_and(a>=5, a<=10)) a[index] # 答案 3: a[(a >= 5) & (a <= 10)] #> (array([6, 9, 10]),) 

 

挑戰 15:實現 max 的 numpy 版

要求:給定長度相同的數組 a、b,返回一個新數組,數組上的每一個元素為 max(a_i, b_i)。

若 pair_max 為滿足要求的函數,則對於 a 和 b,期望輸出如下:

# 輸入數組 a = np.array([5, 7, 9, 8, 6, 4, 5]) b = np.array([6, 3, 4, 8, 9, 7, 1]) pair_max(a, b) #> 期望輸出:array([ 6., 7., 9., 8., 9., 7., 5.]) # 答案: def maxx(x, y): """Get the maximum of two items""" if x >= y: return x else: return y pair_max = np.vectorize(maxx, otypes=[float]) a = np.array([5, 7, 9, 8, 6, 4, 5]) b = np.array([6, 3, 4, 8, 9, 7, 1]) pair_max(a, b) 

 

挑戰 16:交換二維數組的列

要求:交換數組的第一第二列。

# 輸入數組 arr = np.arange(9).reshape(3,3) # 答案: arr[:, [1,0,2]] #> array([[1, 0, 2], #> [4, 3, 5], #> [7, 6, 8]]) 

 

挑戰 17:交換二維數組的行

要求:交換二維數組的第一第二行。

# 輸入數組 arr = np.arange(9).reshape(3,3) # 答案 arr[[1,0,2], :] #> array([[3, 4, 5], #> [0, 1, 2], #> [6, 7, 8]]) 

 

挑戰 18:將一個數組按行反序

要求:數組 arr 為二維數組,將其行反序。

# 輸入數組 arr = np.arange(9).reshape(3,3) # 答案: arr[::-1] 

 

挑戰 19:將一個數組按列反序

要求:數組 arr 為二維數組,將其列反序。

# 輸入數組 arr = np.arange(9).reshape(3,3) # 答案: arr[:, ::-1] 

 

挑戰 20:創建隨機數組

要求:創建一個 5*3 的數組,數組元素為 5 到 10 的隨機浮點數。

# 答案 1: rand_arr = np.random.randint(low=5, high=10, size=(5,3)) + np.random.random((5,3)) # print(rand_arr) # 答案 2: rand_arr = np.random.uniform(5,10, size=(5,3)) print(rand_arr) #> [[ 8.50061025 9.10531502 6.85867783] #> [ 9.76262069 9.87717411 7.13466701] #> [ 7.48966403 8.33409158 6.16808631] #> [ 7.75010551 9.94535696 5.27373226] #> [ 8.0850361 5.56165518 7.31244004]] 

 

挑戰 21:按要求打印數組(一)

要求:數組元素輸出時保留 3 位小數。

# 輸入數組 rand_arr = np.random.random([5,3]) # 答案: # 設置保留 3 位小數 np.set_printoptions(precision=3) rand_arr[:4] #> array([[ 0.443, 0.109, 0.97 ], #> [ 0.388, 0.447, 0.191], #> [ 0.891, 0.474, 0.212], #> [ 0.609, 0.518, 0.403]]) 

 

挑戰 22:按要求打印數組(二)

要求:數組為小數,使用小數點的形式來打印,而不是科學記數法(如1e-4)。

# 輸入數組 np.random.seed(100) rand_arr = np.random.random([3,3])/1e3 rand_arr #> array([[ 5.434049e-04, 2.783694e-04, 4.245176e-04], #> [ 8.447761e-04, 4.718856e-06, 1.215691e-04], #> [ 6.707491e-04, 8.258528e-04, 1.367066e-04]]) # 答案: np.set_printoptions(suppress=True, precision=6) # precision 是可選項 rand_arr #> array([[ 0.000543, 0.000278, 0.000425], #> [ 0.000845, 0.000005, 0.000122], #> [ 0.000671, 0.000826, 0.000137]]) 

 

挑戰 23:按要求打印數組(三)

要求:打印時省略中間元素,限制顯示數組元素的個數為 6。

# 輸入數組 a = np.arange(15) #> 原輸出 :[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] #> 目標輸出:[ 0 1 2 ..., 12 13 14] array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) # 答案: np.set_printoptions(threshold=6) a #> array([ 0, 1, 2, ..., 12, 13, 14]) 

 

挑戰 24:加載特殊矩陣

要求:著名的 iris 數據集是包含蘭花屬性和種類的數據集,其中每行屬性有數字和文字,用 numpy 來加載他們。

# 答案 url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris = np.genfromtxt(url, delimiter=',', dtype='object') names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species') # 輸出前三行 iris[:3] #> array([[b'5.1', b'3.5', b'1.4', b'0.2', b'Iris-setosa'], #> [b'4.9', b'3.0', b'1.4', b'0.2', b'Iris-setosa'], #> [b'4.7', b'3.2', b'1.3', b'0.2', b'Iris-setosa']], dtype=object) 

 

挑戰 25:重定義數組的元素范圍

要求:將 iris 數組集的第一個列的數據范圍縮放為 0 到 1。

# Input url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0]) # 答案 Smax, Smin = sepallength.max(), sepallength.min() S = (sepallength - Smin)/(Smax - Smin) # or S = (sepallength - Smin)/sepallength.ptp() print(S) #> [ 0.222 0.167 0.111 0.083 0.194 0.306 0.083 0.194 0.028 0.167 #> 0.306 0.139 0.139 0. 0.417 0.389 0.306 0.222 0.389 0.222 #> 0.306 0.222 0.083 0.222 0.139 0.194 0.194 0.25 0.25 0.111 #> 0.139 0.306 0.25 0.333 0.167 0.194 0.333 0.167 0.028 0.222 #> 0.194 0.056 0.028 0.194 0.222 0.139 0.222 0.083 0.278 0.194 #> 0.75 0.583 0.722 0.333 0.611 0.389 0.556 0.167 0.639 0.25 #> 0.194 0.444 0.472 0.5 0.361 0.667 0.361 0.417 0.528 0.361 #> 0.444 0.5 0.556 0.5 0.583 0.639 0.694 0.667 0.472 0.389 #> 0.333 0.333 0.417 0.472 0.306 0.472 0.667 0.556 0.361 0.333 #> 0.333 0.5 0.417 0.194 0.361 0.389 0.389 0.528 0.222 0.389 #> 0.556 0.417 0.778 0.556 0.611 0.917 0.167 0.833 0.667 0.806 #> 0.611 0.583 0.694 0.389 0.417 0.583 0.611 0.944 0.944 0.472 #> 0.722 0.361 0.944 0.556 0.667 0.806 0.528 0.5 0.583 0.806 #> 0.861 1. 0.583 0.556 0.5 0.944 0.556 0.583 0.472 0.722 #> 0.667 0.722 0.417 0.694 0.667 0.667 0.556 0.611 0.528 0.444] 

 

挑戰 26:根據百分比大小返回元素

要求:返回數組中按從小到大排序,位置為 5% 和 95% 的數。

# 輸入數組 url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0]) # 答案: np.percentile(sepallength, q=[5, 95]) #> array([ 4.6 , 7.255]) 

 

挑戰 27:找出數組的缺失值

要求:數組中有多處缺失值(答案nan),找出他們的位置。

# 輸入數組 url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3]) iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan # 答案: print("Number of missing values: \n", np.isnan(iris_2d[:, 0]).sum()) print("Position of missing values: \n", np.where(np.isnan(iris_2d[:, 0]))) #> Number of missing values: #> 5 #> Position of missing values: #> (array([ 39, 88, 99, 130, 147]),) 

 

挑戰 28:數組缺失值判斷

要求:返回數組是否具有缺失值。

# 輸入數組 url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3]) iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan # 答案: np.isnan(iris_2d).any() #> False 

 

挑戰 29:數組缺失值處理

要求:替換數組中的缺失值為0。

# 輸入數組 url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3]) iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan # 答案 iris_2d[np.isnan(iris_2d)] = 0 iris_2d[:4] #> array([[ 5.1, 3.5, 1.4, 0. ], #> [ 4.9, 3. , 1.4, 0.2], #> [ 4.7, 3.2, 1.3, 0.2], #> [ 4.6, 3.1, 1.5, 0.2]]) 

 

挑戰 30:數組的 unique 元素

要求:返回數組中出現的所有元素集合

# 輸入數組 url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris = np.genfromtxt(url, delimiter=',', dtype='object') # 答案: species = np.array([row.tolist()[4] for row in iris]) np.unique(species, return_counts=True) #> (array([b'Iris-setosa', b'Iris-versicolor', b'Iris-virginica'], #> dtype='|S15'), array([50, 50, 50])) 

 

挑戰 31:二維數組排序

要求:根據第一列排序二維數組

# 輸入數組 url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris = np.genfromtxt(url, delimiter=',', dtype='object') # 答案: print(iris[iris[:,0].argsort()][:5]) #> [[b'4.3' b'3.0' b'1.1' b'0.1' b'Iris-setosa'] #> [b'4.4' b'3.2' b'1.3' b'0.2' b'Iris-setosa'] #> [b'4.4' b'3.0' b'1.3' b'0.2' b'Iris-setosa'] #> [b'4.4' b'2.9' b'1.4' b'0.2' b'Iris-setosa'] #> [b'4.5' b'2.3' b'1.3' b'0.3' b'Iris-setosa'] 

 

挑戰 32:出現最頻繁的元素

要求:返回數組中出現最多的元素。

# 輸入數組: url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris = np.genfromtxt(url, delimiter=',', dtype='object') # 答案: vals, counts = np.unique(iris[:, 2], return_counts=True) print(vals[np.argmax(counts)]) #> b'1.5' 

 

挑戰 33:找出數組中某元素滿足第一次大於某數的下標

要求:在 iris 數據集中,返回第一個元素的下標,滿足第4列屬性大於1.0。

# 輸入數組: url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris = np.genfromtxt(url, delimiter=',', dtype='object') # 答案: np.argwhere(iris[:, 3].astype(float) > 1.0)[0] #> 50 

 

挑戰 34:設定數組元素的上下限

要求:給定數組 a,將數組中大於 30 的數截斷為 30,小於 10 的數截斷為 10。

# 輸入數組 np.set_printoptions(precision=2) np.random.seed(100) a = np.random.uniform(1,50, 20) # 答案 1: np.clip(a, a_min=10, a_max=30) # 答案 2: print(np.where(a < 10, 10, np.where(a > 30, 30, a))) #> [ 27.63 14.64 21.8 30. 10. 10. 30. 30. 10. 29.18 30. #> 11.25 10.08 10. 11.77 30. 30. 10. 30. 14.43] 

 

挑戰 35:去掉所有缺失值

要求:給定一維數組 a 包含缺失值,去掉他們。

# 輸入數組 a = np.array([1,2,3,np.nan,5,6,7,np.nan]) # 答案: a[~np.isnan(a)] #> array([ 1., 2., 3., 5., 6., 7.]) 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM