當前位置:
首頁 > 新聞 > NumPy能力大評估:這裡有70道測試題

NumPy能力大評估:這裡有70道測試題

本 NumPy 測試題旨在為大家提供參考,讓大家可以使用 NumPy 的更多功能。問題共分為四個等級,L1 最簡單,難度依次增加。機器之心對該測試題進行了編譯介紹,希望能對大家有所幫助。每個問題之後附有代碼答案,參見原文。

原文鏈接:https://www.machinelearningplus.com/101-numpy-exercises-python/

NumPy能力大評估:這裡有70道測試題

如果你想先回顧一下 NumPy 的知識,推薦閱讀:

  • NumPy 基礎:https://www.machinelearningplus.com/numpy-tutorial-part1-array-python-examples

  • NumPy 高級教程:https://www.machinelearningplus.com/numpy-tutorial-python-part2

1. 將 NumPy 導入為 np,並查看版本

難度:L1

問題:將 NumPy 導入為 np,並輸出版本號。

2. 如何創建 1 維數組?

難度:L1

問題:創建數字從 0 到 9 的 1 維數組。

期望輸出:


#> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

3. 如何創建 boolean 數組?

難度:L1

問題:創建所有 True 的 3×3 NumPy 數組。

NumPy能力大評估:這裡有70道測試題

4. 如何從 1 維數組中提取滿足給定條件的項?

難度:L1

問題:從 arr 中提取所有奇數。

輸入:


arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])`

期望輸出:


#> array([1, 3, 5, 7, 9])

5. 如何將 NumPy 數組中滿足給定條件的項替換成另一個數值?

難度:L1

問題:將 arr 中的所有奇數替換成 -1。

輸入:


arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

期望輸出:


#> array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])

6. 如何在不影響原始數組的前提下替換滿足給定條件的項?

難度:L2

問題:將 arr 中所有奇數替換成 -1,且不改變 arr。

輸入:


arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

期望輸出:


out

#> array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])

arr

#> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

NumPy能力大評估:這裡有70道測試題

7. 如何重塑(reshape)數組?

難度:L1

問題:將 1 維數組轉換成 2 維數組(兩行)。

輸入:


np.arange(10)

#> array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

期望輸出


#> array([[0, 1, 2, 3, 4],

#> [5, 6, 7, 8, 9]])

8. 如何垂直堆疊兩個數組?

難度:L2

問題:垂直堆疊數組 a 和 b。

輸入:


a = np.arange(10).reshape(2,-1)

b = np.repeat(1, 10).reshape(2,-1)

期望輸出:


#> array([[0, 1, 2, 3, 4],

#> [5, 6, 7, 8, 9],

#> [1, 1, 1, 1, 1],

#> [1, 1, 1, 1, 1]])

9. 如何水平堆疊兩個數組?

難度:L2

問題:水平堆疊數組 a 和 b。

輸入:


a = np.arange(10).reshape(2,-1)

b = np.repeat(1, 10).reshape(2,-1)

期望輸出:


#> array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1],

#> [5, 6, 7, 8, 9, 1, 1, 1, 1, 1]])

10. 在不使用硬編碼的前提下,如何在 NumPy 中生成自定義序列?

難度:L2

問題:在不使用硬編碼的前提下創建以下模式。僅使用 NumPy 函數和以下輸入數組 a。

輸入:


a = np.array([1,2,3])`

期望輸出:


#> array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])

11. 如何獲得兩個 Python NumPy 數組中共同的項?

難度:L2

問題:獲取數組 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])

期望輸出:


array([2, 4])

12. 如何從一個數組中移除與另一個數組重複的項?

難度:L2

問題:從數組 a 中移除出現在數組 b 中的所有項。

輸入:


a = np.array([1,2,3,4,5])

b = np.array([5,6,7,8,9])

期望輸出:


array([1,2,3,4])

13. 如何獲取兩個數組匹配元素的位置?

難度:L2

問題:獲取數組 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])

期望輸出:


#> (array([1, 3, 5, 7]),)

14. 如何從 NumPy 數組中提取給定範圍內的所有數字?

難度:L2

問題:從數組 a 中提取 5 和 10 之間的所有項。

輸入:


a = np.arange(15)

期望輸出:


(array([ 5, 6, 7, 8, 9, 10]),)

15. 如何創建一個 Python 函數以對 NumPy 數組執行元素級的操作?

難度:L2

問題:轉換函數 maxx,使其從只能對比標量而變為對比兩個數組。

輸入:


def maxx(x, y):

"""Get the maximum of two items"""

if x >= y:

return x

else:

return y

maxx(1, 5)

#> 5

期望輸出:


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.])

16. 如何在 2d NumPy 數組中交換兩個列?

難度:L2

問題:在數組 arr 中交換列 1 和列 2。


arr = np.arange(9).reshape(3,3)

arr

17. 如何在 2d NumPy 數組中交換兩個行?

難度:L2

問題:在數組 arr 中交換行 1 和行 2。


arr = np.arange(9).reshape(3,3)

arr

18. 如何反轉 2D 數組的所有行?

難度:L2

問題:反轉 2D 數組 arr 中的所有行。


# Input

arr = np.arange(9).reshape(3,3)

19. 如何反轉 2D 數組的所有列?

難度:L2

問題:反轉 2D 數組 arr 中的所有列。


# Input

arr = np.arange(9).reshape(3,3)

20. 如何創建一個包含 5 和 10 之間隨機浮點的 2 維數組?

難度:L2

問題:創建一個形態為 5×3 的 2 維數組,包含 5 和 10 之間的隨機十進位小數。

21. 如何在 Python NumPy 數組中僅輸出小數點後三位的數字?

難度:L1

問題:輸出或顯示 NumPy 數組 rand_arr 中小數點後三位的數字。

輸入:


rand_arr = np.random.random((5,3))

22. 如何通過禁用科學計數法(如 1e10)列印 NumPy 數組?

難度:L1

問題:通過禁用科學計數法(如 1e10)列印 NumPy 數組 rand_arr。

輸入:


# Create the random array

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]])

期望輸出:


#> array([[ 0.000543, 0.000278, 0.000425],

#> [ 0.000845, 0.000005, 0.000122],

#> [ 0.000671, 0.000826, 0.000137]])

23. 如何限制 NumPy 數組輸出中項的數目?

難度:L1

問題:將 Python NumPy 數組 a 輸出的項的數目限制在最多 6 個元素。

輸入:


a = np.arange(15)

#> array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])

期望輸出:


#> array([ 0, 1, 2, ..., 12, 13, 14])

24. 如何在不截斷數組的前提下列印出完整的 NumPy 數組?

難度:L1

問題:在不截斷數組的前提下列印出完整的 NumPy 數組 a。

輸入:


np.set_printoptions(threshold=6)

a = np.arange(15)

a

#> array([ 0, 1, 2, ..., 12, 13, 14])

期望輸出:


a

#> array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])

25. 如何向 Python NumPy 導入包含數字和文本的數據集,同時保持文本不變?

難度:L2

問題:導入 iris 數據集,保持文本不變。

26. 如何從 1 維元組數組中提取特定的列?

難度:L2

問題:從前一個問題導入的 1 維 iris 中提取文本列 species。

輸入:


url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

iris_1d = np.genfromtxt(url, delimiter=",", dtype=None)

27. 如何將 1 維元組數組轉換成 2 維 NumPy 數組?

難度:L2

問題:忽略 species 文本欄位,將 1 維 iris 轉換成 2 維數組 iris_2d。

輸入:


url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

iris_1d = np.genfromtxt(url, delimiter=",", dtype=None)

28. 如何計算 NumPy 數組的平均值、中位數和標準差?

難度:L1

問題:找出 iris sepallength(第一列)的平均值、中位數和標準差。


url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

iris = np.genfromtxt(url, delimiter=",", dtype="object")

29. 如何歸一化數組,使值的範圍在 0 和 1 之間?

難度:L2

問題:創建 iris sepallength 的歸一化格式,使其值在 0 到 1 之間。

輸入:


url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

sepallength = np.genfromtxt(url, delimiter=",", dtype="float", usecols=[0])

30. 如何計算 softmax 分數?

難度:L3

問題:計算 sepallength 的 softmax 分數。


url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

sepallength = np.genfromtxt(url, delimiter=",", dtype="float", usecols=[0])

31. 如何找到 NumPy 數組的百分數?

難度:L1

問題:找出 iris sepallength(第一列)的第 5 個和第 95 個百分數。


url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

sepallength = np.genfromtxt(url, delimiter=",", dtype="float", usecols=[0])

32. 如何在數組的隨機位置插入值?

難度:L2

問題:在 iris_2d 數據集中的 20 個隨機位置插入 np.nan 值。


# Input

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

iris_2d = np.genfromtxt(url, delimiter=",", dtype="object")

33. 如何在 NumPy 數組中找出缺失值的位置?

難度:L2

問題:在 iris_2d 的 sepallength(第一列)中找出缺失值的數目和位置。


# Input

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

iris_2d = np.genfromtxt(url, delimiter=",", dtype="float")

iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan

34. 如何基於兩個或以上條件過濾 NumPy 數組?

難度:L3

問題:過濾 iris_2d 中滿足 petallength(第三列)> 1.5 和 sepallength(第一列)< 5.0 的行。


# Input

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])

35. 如何在 NumPy 數組中刪除包含缺失值的行?

難度:L3

問題:選擇 iris_2d 中不包含 nan 值的行。


# Input

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])

36. 如何找出 NumPy 數組中兩列之間的關聯性?

難度:L2

問題:找出 iris_2d 中 SepalLength(第一列)和 PetalLength(第三列)之間的關聯性。


# Input

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])

37. 如何確定給定數組是否有空值?

難度:L2

問題:確定 iris_2d 是否有缺失值。


# Input

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])

38. 如何在 NumPy 數組中將所有缺失值替換成 0?

難度:L2

問題:在 NumPy 數組中將所有 nan 替換成 0。


# Input

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

39. 如何在 NumPy 數組中找出唯一值的數量?

難度:L2

問題:在 iris 的 species 列中找出唯一值及其數量。


# Input

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")

40. 如何將一個數值轉換為一個類別(文本)數組?

難度:L2

問題:將 iris_2d 的 petallength(第三列)轉換以構建一個文本數組,按如下規則進行轉換:

  • Less than 3 –> 『small』

  • 3-5 –> "medium"

  • ">=5 –> "large"

# Input

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")

41. 如何基於 NumPy 數組現有列創建一個新的列?

難度:L2

問題:為 iris_2d 中的 volume 列創建一個新的列,volume 指 (pi x petallength x sepal_length^2)/3。


# Input

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

iris_2d = np.genfromtxt(url, delimiter=",", dtype="object")

names = ("sepallength", "sepalwidth", "petallength", "petalwidth", "species")

42. 如何在 NumPy 中執行概率採樣?

難度:L3

問題:隨機採樣 iris 數據集中的 species 列,使得 setose 的數量是 versicolor 和 virginica 數量的兩倍。


# Import iris keeping the text column intact

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

iris = np.genfromtxt(url, delimiter=",", dtype="object")

43. 如何在多維數組中找到一維的第二最大值?

難度:L2

問題:在 species setosa 的 petallength 列中找到第二最大值。


# Input

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")

44. 如何用給定列將 2 維數組排序?

難度:L2

問題:基於 sepallength 列將 iris 數據集排序。


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")

45. 如何在 NumPy 數組中找到最頻繁出現的值?

難度:L1

問題:在 iris 數據集中找到 petallength(第三列)中最頻繁出現的值。


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")

46. 如何找到第一個大於給定值的數的位置?

難度:L2

問題:在 iris 數據集的 petalwidth(第四列)中找到第一個值大於 1.0 的數的位置。


# Input:

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

iris = np.genfromtxt(url, delimiter=",", dtype="object")

47. 如何將數組中所有大於給定值的數替換為給定的 cutoff 值?

難度:L2

問題:對於數組 a,將所有大於 30 的值替換為 30,將所有小於 10 的值替換為 10。

輸入:


np.random.seed(100)

np.random.uniform(1,50, 20)

48. 如何在 NumPy 數組中找到 top-n 數值的位置?

難度:L2

問題:在給定數組 a 中找到 top-5 最大值的位置。


np.random.seed(100)

a = np.random.uniform(1,50, 20)

49. 如何逐行計算數組中所有值的數量?

難度:L4

問題:逐行計算唯一值的數量。

輸入:


np.random.seed(100)

arr = np.random.randint(1,11,size=(6, 10))

arr

> array([[ 9, 9, 4, 8, 8, 1, 5, 3, 6, 3],

> [ 3, 3, 2, 1, 9, 5, 1, 10, 7, 3],

> [ 5, 2, 6, 4, 5, 5, 4, 8, 2, 2],

> [ 8, 8, 1, 3, 10, 10, 4, 3, 6, 9],

> [ 2, 1, 8, 7, 3, 1, 9, 3, 6, 2],

> [ 9, 2, 6, 5, 3, 9, 4, 6, 1, 10]])

期望輸出:


> [[1, 0, 2, 1, 1, 1, 0, 2, 2, 0],

> [2, 1, 3, 0, 1, 0, 1, 0, 1, 1],

> [0, 3, 0, 2, 3, 1, 0, 1, 0, 0],

> [1, 0, 2, 1, 0, 1, 0, 2, 1, 2],

> [2, 2, 2, 0, 0, 1, 1, 1, 1, 0],

> [1, 1, 1, 1, 1, 2, 0, 0, 2, 1]]

輸出包含 10 個列,表示從 1 到 10 的數字。這些數值分別代表每一行的計數數量。例如,Cell(0,2) 中有值 2,這意味著,數字 3 在第一行出現了兩次。

50. 如何將 array_of_arrays 轉換為平面 1 維數組?

難度:L2

問題:將 array_of_arrays 轉換為平麵線性 1 維數組。


# Input:

arr1 = np.arange(3)

arr2 = np.arange(3,7)

arr3 = np.arange(7,10)

array_of_arrays = np.array([arr1, arr2, arr3])

array_of_arrays#> array([array([0, 1, 2]), array([3, 4, 5, 6]), array([7, 8, 9])], dtype=object)

期望輸出:


#> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

51. 如何為 NumPy 數組生成 one-hot 編碼?

難度:L4

問題:計算 one-hot 編碼。

輸入:


np.random.seed(101)

arr = np.random.randint(1,4, size=6)

arr

#> array([2, 3, 2, 2, 2, 1])

輸出:


#> array([[ 0., 1., 0.],

#> [ 0., 0., 1.],

#> [ 0., 1., 0.],

#> [ 0., 1., 0.],

#> [ 0., 1., 0.],

#> [ 1., 0., 0.]])

52. 如何創建由類別變數分組確定的一維數值?

難度:L3

問題:創建由類別變數分組的行數。使用以下來自 iris species 的樣本作為輸入。

輸入:


url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

species = np.genfromtxt(url, delimiter=",", dtype="str", usecols=4)

species_small = np.sort(np.random.choice(species, size=20))

species_small

#> array(["Iris-setosa", "Iris-setosa", "Iris-setosa", "Iris-setosa",

#> "Iris-setosa", "Iris-setosa", "Iris-versicolor", "Iris-versicolor",

#> "Iris-versicolor", "Iris-versicolor", "Iris-versicolor",

#> "Iris-versicolor", "Iris-virginica", "Iris-virginica",

#> "Iris-virginica", "Iris-virginica", "Iris-virginica",

#> "Iris-virginica", "Iris-virginica", "Iris-virginica"],

#> dtype="<U15")

期望輸出:


#> [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7]

53. 如何基於給定的類別變數創建分組 id?

難度:L4

問題:基於給定的類別變數創建分組 id。使用以下來自 iris species 的樣本作為輸入。

輸入:


url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

species = np.genfromtxt(url, delimiter=",", dtype="str", usecols=4)

species_small = np.sort(np.random.choice(species, size=20))

species_small

#> array(["Iris-setosa", "Iris-setosa", "Iris-setosa", "Iris-setosa",

#> "Iris-setosa", "Iris-setosa", "Iris-versicolor", "Iris-versicolor",

#> "Iris-versicolor", "Iris-versicolor", "Iris-versicolor",

#> "Iris-versicolor", "Iris-virginica", "Iris-virginica",

#> "Iris-virginica", "Iris-virginica", "Iris-virginica",

#> "Iris-virginica", "Iris-virginica", "Iris-virginica"],

#> dtype="<U15")

期望輸出:


#> [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]

54. 如何使用 NumPy 對數組中的項進行排序?

難度:L2

問題:為給定的數值數組 a 創建排序。

輸入:


np.random.seed(10)

a = np.random.randint(20, size=10)print(a)#> [ 9 4 15 0 17 16 17 8 9 0]

期望輸出:


[4 2 6 0 8 7 9 3 5 1]

55. 如何使用 NumPy 對多維數組中的項進行排序?

難度:L3

問題:給出一個數值數組 a,創建一個形態相同的排序數組。

輸入:


np.random.seed(10)

a = np.random.randint(20, size=[2,5])print(a)#> [[ 9 4 15 0 17]#> [16 17 8 9 0]]

期望輸出:


#> [[4 2 6 0 8]

#> [7 9 3 5 1]]

56. 如何在 2 維 NumPy 數組中找到每一行的最大值?

難度:L2

問題:在給定數組中找到每一行的最大值。


np.random.seed(100)

a = np.random.randint(1,10, [5,3])

a

#> array([[9, 9, 4],

#> [8, 8, 1],

#> [5, 3, 6],

#> [3, 3, 3],

#> [2, 1, 9]])

57. 如何計算 2 維 NumPy 數組每一行的 min-by-max?

難度:L3

問題:給定一個 2 維 NumPy 數組,計算每一行的 min-by-max。


np.random.seed(100)

a = np.random.randint(1,10, [5,3])

a

#> array([[9, 9, 4],

#> [8, 8, 1],

#> [5, 3, 6],

#> [3, 3, 3],

#> [2, 1, 9]])

58. 如何在 NumPy 數組中找到重複條目?

難度:L3

問題:在給定的 NumPy 數組中找到重複條目(從第二次出現開始),並將其標記為 True。第一次出現的條目需要標記為 False。


# Input

np.random.seed(100)

a = np.random.randint(0, 5, 10)

print("Array: ", a)

#> Array: [0 0 3 0 2 4 2 2 2 2]

期望輸出:


#> [False True False True False False True True True True]

59. 如何找到 NumPy 的分組平均值?

難度:L3

問題:在 2 維 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")

期望解:


#> [[b"Iris-setosa", 3.418],

#> [b"Iris-versicolor", 2.770],

#> [b"Iris-virginica", 2.974]]

60. 如何將 PIL 圖像轉換成 NumPy 數組?

難度:L3

問題:從以下 URL 中導入圖像,並將其轉換成 NumPy 數組。


URL = "https://upload.wikimedia.org/wikipedia/commons/8/8b/Denali_Mt_McKinley.jpg"

61. 如何刪除 NumPy 數組中所有的缺失值?

難度:L2

問題:從 1 維 NumPy 數組中刪除所有的 nan 值。

輸入:


np.array([1,2,3,np.nan,5,6,7,np.nan])

期望輸出:


array([ 1., 2., 3., 5., 6., 7.])

62. 如何計算兩個數組之間的歐幾里得距離?

難度:L3

問題:計算兩個數組 a 和 b 之間的歐幾里得距離。

輸入:


a = np.array([1,2,3,4,5])

b = np.array([4,5,6,7,8])

63. 如何在一個 1 維數組中找到所有的局部極大值(peak)?

難度:L4

問題:在 1 維數組 a 中找到所有的 peak,peak 指一個數字比兩側的數字都大。

輸入:


a = np.array([1, 3, 7, 1, 2, 6, 0, 1])

期望輸出:


#> array([2, 5])

64. 如何從 2 維數組中減去 1 維數組,從 2 維數組的每一行分別減去 1 維數組的每一項?

難度:L2

問題:從 2 維數組 a_2d 中減去 1 維數組 b_1d,即從 a_2d 的每一行分別減去 b_1d 的每一項。

輸入:


a_2d = np.array([[3,3,3],[4,4,4],[5,5,5]])

b_1d = np.array([1,1,1]

期望輸出:


#> [[2 2 2]

#> [2 2 2]

#> [2 2 2]]

65. 如何在數組中找出某個項的第 n 個重複索引?

難度:L2

問題:找到數組 x 中數字 1 的第 5 個重複索引。


x = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2])

66. 如何將 NumPy 的 datetime64 對象(object)轉換為 datetime 的 datetime 對象?

難度:L2

問題:將 NumPy 的 datetime64 對象(object)轉換為 datetime 的 datetime 對象。


# Input: a numpy datetime64 object

dt64 = np.datetime64("2018-02-25 22:10:10")

67. 如何計算 NumPy 數組的移動平均數?

難度:L3

問題:給定 1 維數組,計算 window size 為 3 的移動平均數。

輸入:


np.random.seed(100)

Z = np.random.randint(10, size=10)

68. 給定起始數字、length 和步長,如何創建一個 NumPy 數組序列?

難度:L2

問題:從 5 開始,創建一個 length 為 10 的 NumPy 數組,相鄰數字的差是 3。

69. 如何在不規則 NumPy 日期序列中填充缺失日期?

難度:L3

問題:給定一個非連續日期序列的數組,通過填充缺失的日期,使其變成連續的日期序列。

輸入:


# Input

dates = np.arange(np.datetime64("2018-02-01"), np.datetime64("2018-02-25"), 2)

print(dates)

#> ["2018-02-01" "2018-02-03" "2018-02-05" "2018-02-07" "2018-02-09"

#> "2018-02-11" "2018-02-13" "2018-02-15" "2018-02-17" "2018-02-19"

#> "2018-02-21" "2018-02-23"]

70. 如何基於給定的 1 維數組創建 strides?

難度:L4

問題:給定 1 維數組 arr,使用 strides 生成一個 2 維矩陣,其中 window length 等於 4,strides 等於 2,例如 [[0,1,2,3], [2,3,4,5], [4,5,6,7]..]。

輸入:


arr = np.arange(15)

arr

#> array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])

期望輸出:


#> [[ 0 1 2 3]

#> [ 2 3 4 5]

#> [ 4 5 6 7]

#> [ 6 7 8 9]

#> [ 8 9 10 11]

#> [10 11 12 13]]

所有問題的解決方案參見原文:https://www.machinelearningplus.com/101-numpy-exercises-python/

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 機器之心 的精彩文章:

專屬 AI 晶元加持, 亞馬遜 Echo 將更加難以被複制和超越

TAG:機器之心 |