1. 将 NumPy 导入为 np,并查看版本
English Version
Title: Import numpy as np and see the version
Difficulty Level: L1
Question: Import numpy as np and print the version number.
难度:L1
问题:将 NumPy 导入为 np,并输出版本号。
Solution
1 | import numpy as np |
2. 如何创建 1 维数组?
English Version
Title: How to create a 1D array?
Difficulty Level: L1
Question: Create a 1D array of numbers from 0 to 9.
难度:L1
问题:创建数字从 0 到 9 的 1 维数组。
期望输出:
1 | array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
Solution
1 | 10) arr = np.arange( |
3. 如何创建 boolean 数组?
English Version
Title: How to create a boolean array?
Difficulty Level: L1
Question: Create a 3×3 numpy array of all True’s.
难度:L1
问题:创建所有值为 True 的 3×3 NumPy 数组。
Solution 1
1 | 3, 3), True) np.full(( |
Solution 2
1 | 3, 3), dtype=bool) np.ones(( |
4. 如何从 1 维数组中提取满足给定条件的项?
English Version
Title: How to extract items that satisfy a given condition from 1D array?
Difficulty Level: L1
Question: Extract all odd numbers from arr
.
难度:L1
问题:从 arr
中提取所有奇数。
输入:
1 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) arr = np.array([ |
期望输出:
1 | array([1, 3, 5, 7, 9]) |
Solution
1 | 2 == 1] arr[arr % |
5. 如何将 NumPy 数组中满足给定条件的项替换成另一个数值?
English Version
Title: How to replace items that satisfy a condition with another value in numpy array?
Difficulty Level: L1
Question: Replace all odd numbers in arr
with -1.
难度:L1
问题:将 arr
中的所有奇数替换成 -1。
输入:
1 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) arr = np.array([ |
期望输出:
1 | array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1]) |
Solution
1 | 2 == 1] = -1 arr[arr % |
6. 如何在不影响原始数组的前提下替换满足给定条件的项?
English Version
Title: How to replace items that satisfy a condition without affecting the original array?
Difficulty Level: L2
Question: Replace all odd numbers in arr
with -1 without changing arr
.
难度:L2
问题:将 arr
中所有奇数替换成 -1,且不改变 arr
。
输入:
1 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) arr = np.array([ |
期望输出:
1 | out |
Solution 1
1 | out = np.copy(arr) |
Solution 2
1 | 2 == 1, -1, arr) out = np.where(arr % |
7. 如何重塑(reshape)数组?
English Version
Title: How to reshape an array?
Difficulty Level: L1
Question: Convert a 1D array to a 2D array with 2 rows.
难度:L1
问题:将 1 维数组转换成 2 维数组(两行)。
输入:
1 | 10) arr = np.arange( |
期望输出:
1 | array([[0, 1, 2, 3, 4], |
Solution
1 | 2, -1)) arr.reshape(( |
8. 如何垂直堆叠两个数组?
English Version
Title: How to stack two arrays vertically?
Difficulty Level: L2
Question: Stack arrays a
and b
vertically.
难度:L2
问题:垂直堆叠数组 a
和 b
。
输入:
1 | 10).reshape(2, -1) a = np.arange( |
期望输出:
1 | array([[0, 1, 2, 3, 4], |
Solution 1
1 | 0) np.concatenate((a, b), axis= |
Solution 2
1 | np.vstack((a, b)) |
Solution 3
1 | np.r_[a, b] |
9. 如何水平堆叠两个数组?
English Version
Title: How to stack two arrays horizontally?
Difficulty Level: L2
Question: Stack the arrays a
and b
horizontally.
难度:L2
问题:水平堆叠数组 a
和 b
。
输入:
1 | 10).reshape(2, -1) a = np.arange( |
期望输出:
1 | array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1], |
Solution 1
1 | 1) np.concatenate((a, b), axis= |
Solution 2
1 | np.hstack((a, b)) |
Solution 3
1 | np.c_[a, b] |
10. 在不使用硬编码的前提下,如何在 NumPy 中生成自定义序列?
English Version
Title: How to generate custom sequences in numpy without hardcoding?
Difficulty Level: L2
Question: Create the following pattern without hardcoding. Use only numpy functions and the below input array a
.
难度:L2
问题:在不使用硬编码的前提下创建以下模式。仅使用 NumPy 函数和以下输入数组 a
。
输入
1 | 1, 2, 3]) a = np.array([ |
期望输出:
1 | array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]) |
Solution 1
1 | 3), np.tile(a, 3))) np.concatenate((np.repeat(a, |
Solution 2
1 | 3), np.tile(a, 3)] np.r_[np.repeat(a, |
11. 如何获得两个 Python NumPy 数组中共同的项?
English Version
Title: How to get the common items between two python numpy arrays?
Difficulty Level: L2
Question: Get the common items between a
and b
.
难度:L2
问题:获取数组 a
和 b
中的共同项。
输入:
1 | 1, 2, 3, 2, 3, 4, 3, 4, 5, 6]) a = np.array([ |
期望输出:
1 | array([2, 4]) |
Solution
1 | np.intersect1d(a, b) |
12. 如何从一个数组中移除与另一个数组重复的项?
English Version
Title: How to remove from one array those items that exist in another?
Difficulty Level: L2
Question: From array a
remove all items present in array b
.
难度:L2
问题:从数组 a
中移除出现在数组 b
中的所有项。
输入:
1 | 1, 2, 3, 4, 5]) a = np.array([ |
期望输出:
1 | array([1, 2, 3, 4]) |
Solution
1 | np.setdiff1d(a, b) |
13. 如何获取两个数组匹配元素的位置?
English Version
Title: How to get the positions where elements of two arrays match?
Difficulty Level: L2
Question: Get the positions where elements of a
and b
match.
难度:L2
问题:获取数组 a 和 b 中匹配元素的位置。
输入:
1 | 1, 2, 3, 2, 3, 4, 3, 4, 5, 6]) a = np.array([ |
期望输出:
1 | (array([1, 3, 5, 7]), ) |
Solution
1 | np.where(a == b) |
14. 如何从 NumPy 数组中提取给定范围内的所有数字?
English Version
Title: How to extract all numbers between a given range from a numpy array?
Difficulty Level: L2
Question: Get all items between 5 and 10 from a
.
难度:L2
问题:从数组 a
中提取 5 和 10 之间的所有项。
输入:
1 | 2, 6, 1, 9, 10, 3, 27]) a = np.array([ |
期望输出:
1 | array([6, 9, 10]) |
Solution 1
1 | 5) & (a <= 10)] a[(a >= |
Solution 2
1 | 5) & (a <= 10)) index = np.where((a >= |
Solution 3
1 | 5, a<=10)) index = np.where(np.logical_and(a>= |
15. 如何创建一个 Python 函数以对 NumPy 数组执行元素级的操作?
English Version
Title: How to make a python function that handles scalars to work on numpy arrays?
Difficulty Level: L2
Question: Convert the function maxx
that works on two scalars, to work on two arrays.
难度:L2
问题:转换函数 maxx
,使其从只能对比标量而变为对比两个数组。
输入:
1 | def maxx(x, y): |
期望输出:
1 | 5, 7, 9, 8, 6, 4, 5]) a = np.array([ |
Solution
1 | float]) pair_max = np.vectorize(maxx, otypes=[ |
16. 如何在 2d NumPy 数组中交换两个列?
English Version
Title: How to swap two columns in a 2d numpy array?
Difficulty Level: L2
Question: Swap columns 1 and 2 in the array arr
.
难度:L2
问题:在数组 arr
中交换列 1 和列 2。
1 | 9).reshape(3, 3) arr = np.arange( |
Solution 1
1 | 1, 0, 2]] arr[:, [ |
Solution 2
1 | # Swap in-place |
17. 如何在 2d NumPy 数组中交换两个行?
English Version
Title: How to swap two rows in a 2d numpy array?
Difficulty Level: L2
Question: Swap rows 1 and 2 in the array arr
.
难度:L2
问题:在数组 arr
中交换行 1 和行 2。
1 | 9).reshape(3, 3) arr = np.arange( |
Solution 1
1 | 1, 0, 2], :] arr[[ |
Solution 2
1 | # Swap in-place |
18. 如何反转 2D 数组的所有行?
English Version
Title: How to reverse the rows of a 2D array?
Difficulty Level: L2
Question: Reverse the rows of a 2D array arr
.
难度:L2
问题:反转 2D 数组 arr
中的所有行。
1 | 9).reshape(3, 3) arr = np.arange( |
Solution
1 | 1] arr[::- |
19. 如何反转 2D 数组的所有列?
English Version
Title: How to reverse the columns of a 2D array?
Difficulty Level: L2
Question: Reverse the columns of a 2D array arr
.
难度:L2
问题:反转 2D 数组 arr
中的所有列。
1 | 9).reshape(3, 3) arr = np.arange( |
Solution
1 | 1] arr[:, ::- |
20. 如何创建一个包含 5 和 10 之间浮点数的随机 2 维数组?
English Version
Title: How to create a 2D array containing random floats between 5 and 10?
Difficulty Level: L2
Question: Create a 2D array of shape 5x3 to contain random decimal numbers between 5 and 10.
难度:L2
问题:创建一个形态为 5×3 的 2 维数组,包含 5 和 10 之间的随机十进制小数。
Solution 1
1 | 100) np.random.seed( |
Solution 2
1 | 100) np.random.seed( |
Solution 3
1 | # Maybe different from other solutions |
21. 如何在 Python NumPy 数组中仅输出小数点后三位的数字?
English Version
Title: How to print only 3 decimal places in python numpy array?
Difficulty Level: L1
Question: Print or show only 3 decimal places of the numpy array rand_arr
.
难度:L1
问题:输出或显示 NumPy 数组 rand_arr
中小数点后三位的数字。
输入:
1 | rand_arr = np.random.random((5, 3)) |
Solution
1 | 3) np.set_printoptions(precision= |
22. 如何通过禁用科学计数法(如 1e10)打印 NumPy 数组?
English Version
Title: How to pretty print a numpy array by suppressing the scientific notation (like 1e10)?
Difficulty Level: L1
Question: Pretty print rand_arr
by suppressing the scientific notation (like 1e10).
难度:L1
问题:通过禁用科学计数法(如 1e10)打印 NumPy 数组 rand_arr
。
输入:
1 | # Create the random array |
期望输出:
1 | array([[0.000543, 0.000278, 0.000425], |
Solution
1 | # precision is optional |
23. 如何限制 NumPy 数组输出中项的数目?
English Version
Title: How to limit the number of items printed in output of numpy array?
Difficulty Level: L1
Question: Limit the number of items printed in python numpy array a
to a maximum of 6 elements.
难度:L1
问题:将 Python NumPy 数组 a
输出的项的数目限制在最多 6 个元素。
输入:
1 | 15) a = np.arange( |
期望输出:
1 | array([ 0, 1, 2, ..., 12, 13, 14]) |
Solution
1 | 6) np.set_printoptions(threshold= |
24. 如何在不截断数组的前提下打印出完整的 NumPy 数组?
English Version
Title: How to print the full numpy array without truncating
Difficulty Level: L1
Question: Print the full numpy array a
without truncating.
难度:L1
问题:在不截断数组的前提下打印出完整的 NumPy 数组 a。
输入:
1 | 6) np.set_printoptions(threshold= |
期望输出:
1 | a |
Solution 1
1 | np.set_printoptions(threshold=np.nan) |
Solution 2
1 | 1000) np.set_printoptions(threshold= |
25. 如何向 Python NumPy 导入包含数字和文本的数据集,同时保持文本不变?
English Version
Title: How to import a dataset with numbers and texts keeping the text intact in python numpy?
Difficulty Level: L2
Question: Import the iris dataset keeping the text intact.
难度:L2
问题:导入 iris 数据集,保持文本不变。
从 Iris Data Set 网页下载数据集 iris.data
。
Solution
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Since we want to retain the species, a text field, I have set the dtype
to object
. Had I set dtype=None
, a 1d array of tuples would have been returned.
26. 如何从 1 维元组数组中提取特定的列?
English Version
Title: How to extract a particular column from 1D array of tuples?
Difficulty Level: L2
Question: Extract the text column species from the 1D iris_1d
.
难度:L2
问题:从导入的 1 维 iris_1d
中提取文本列 species。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution 1
1 | 4] for row in iris_1d]) species = np.array([row[ |
Solution 2
1 | lambda x: x[4]) vfunc = np.vectorize( |
27. 如何将 1 维元组数组转换成 2 维 NumPy 数组?
English Version
Title: How to convert a 1d array of tuples to a 2d numpy array?
Difficulty Level: L2
Question: Convert the 1D iris_1d
to 2D array iris_2d
by omitting the species text field.
难度:L2
问题:忽略 species 文本字段,将 1 维 iris_1d
转换成 2 维数组 iris_2d
。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution
1 | 4] for row in iris_1d]) iris_2d = np.array([row.tolist()[: |
28. 如何计算 NumPy 数组的平均值、中位数和标准差?
English Version
Title: How to compute the mean, median, standard deviation of a numpy array?
Difficulty: L1
Question: Find the mean, median, standard deviation of iris’s sepal length
(1st column).
难度:L1
问题:找出 iris sepal length
(第一列)的平均值、中位数和标准差。
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution
1 | 0] for row in iris_1d]) sepal_length = np.array([row[ |
29. 如何归一化数组,使值的范围在 0 和 1 之间?
English Version
Title: How to normalize an array so the values range exactly between 0 and 1?
Difficulty: L2
Question: Create a normalized form of iris’s sepal length
whose values range exactly between 0 and 1 so that the minimum has value 0 and maximum has value 1.
难度:L2
问题:创建 iris sepal length
的归一化格式,使其值在 0 到 1 之间。
输入:
1 | url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" |
Solution
1 | max(sepal_length) max_value = np. |
30. 如何计算 softmax 分数?
English Version
Title: How to compute the softmax score?
Difficulty Level: L3
Question: Compute the softmax score of sepal length
.
难度:L3
问题:计算 sepal length
的 softmax 分数。
1 | url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" |
Solution
According formula:
$$
S(x_i) = \frac{e^{x_i}}{\sum_j e^{x_j}}
$$
1 | sepal_length_exp = np.exp(sepal_length) |
For numerical stability, the formula changes to:
$$
S(x_i) = \frac{e^{(x_i - x_{max})}}{\sum_j e^{(x_j - x_{max})}}
$$
where $x_{max} = max(x)$.
1 | max(sepal_length)) sepal_length_exp = np.exp(sepal_length - np. |
31. 如何找到 NumPy 数组的百分数?
English Version
Title: How to find the percentile scores of a numpy array?
Difficulty Level: L1
Question: Find the 5th and 95th percentile of iris’s sepal length
.
难度:L1
问题:找出 iris sepal length
(第一列)的第 5 个和第 95 个百分数。
1 | url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" |
Solution
1 | 5, 95]) np.percentile(sepallength, [ |
32. 如何在数组的随机位置插入值?
English Version
Title: How to insert values at random positions in an array?
Difficulty Level: L2
Question: Insert np.nan
values at 20 random positions in iris_2d
dataset.
难度:L2
问题:在 iris_2d
数据集中的 20 个随机位置插入 np.nan
值。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution 1
1 | 0], size=20) rand_row = np.random.randint(iris_2d.shape[ |
Solution 2
1 | i, j = np.where(iris_2d) |
33. 如何在 NumPy 数组中找出缺失值的位置?
English Version
Title: How to find the position of missing values in numpy array?
Difficulty Level: L2
Question: Find the number and position of missing values in iris_2d
‘s sepal length
(1st column).
难度:L2
问题:在 iris_2d
的 sepal length
(第一列)中找出缺失值的数目和位置。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution 1
1 | # number of nan |
Solution 2
1 | 0]) nan_bools = np.isnan(iris_2d[:, |
34. 如何基于两个或以上条件过滤 NumPy 数组?
English Version
Title: How to filter a numpy array based on two or more conditions?
Difficulty Level: L3
Question: Filter the rows of iris_2d
that has petal length (3rd column) > 1.5
and sepal length (1st column) < 5.0
.
难度:L3
问题:过滤 iris_2d
中满足 petal length(第三列)> 1.5
和 sepal length(第一列)< 5.0
的行。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution
1 | 2] > 1.5) & (iris_2d[:, 0] < 5.0) condition = (iris_2d[:, |
35. 如何在 NumPy 数组中删除包含缺失值的行?
English Version
Title: How to drop rows that contain a missing value from a numpy array?
Difficulty Level: L3:
Question: Select the rows of iris_2d
that does not have any nan
value.
难度:L3
问题:选择 iris_2d
中不包含 nan
值的行。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution 1
1 | sum(np.isnan(iris_2d), axis=1) == 0][:5] iris_2d[np. |
Solution 2
1 | any(np.isnan(row)) for row in iris_2d]) any_nan_in_row = np.array([~np. |
36. 如何找出 NumPy 数组中两列之间的关联性?
English Version
Title: How to find the correlation between two columns of a numpy array?
Difficulty Level: L2
Question: Find the correlation between sepal length
(1st column) and petal length
(3rd column) in iris_2d
.
难度:L2
问题:找出 iris_2d
中 sepal length
(第一列)和 petal length
(第三列)之间的关联性。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution 1
1 | 0], iris_2d[:, 2])[0, 1] np.corrcoef(iris_2d[:, |
Solution 2
1 | from scipy.stats.stats import pearsonr |
37. 如何确定给定数组是否有空值?
English Version
Title: How to find if a given array has any null values?
Difficulty Level: L2
Question: Find out if iris_2d
has any missing values.
难度:L2
问题:确定 iris_2d
是否有缺失值。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution 1
1 | sum(np.isnan(iris_2d)) > 0 np. |
Solution 2
1 | any() np.isnan(iris_2d). |
38. 如何在 NumPy 数组中将所有缺失值替换成0?
English Version
Title: How to replace all missing values with 0 in a numpy array?
Difficulty Level: L2
Question: Replace all ccurrences of nan
with 0 in numpy array.
难度:L2
问题:在 NumPy 数组中将所有 nan
替换成 0。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution
1 | 0 iris_2d[np.isnan(iris_2d)] = |
39. 如何在 NumPy 数组中找出唯一值的数量?
English Version
Title: How to find the count of unique values in a numpy array?
Difficulty Level: L2
Question: Find the unique values and the count of unique values in iris’s species
.
难度:L2
问题:在 iris 的 species
列中找出唯一值及其数量。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution
1 | 4], return_counts=True) unique, counts = np.unique(iris[:, |
40. 如何将一个数值转换为一个类别(文本)数组?
English Version
Title: How to convert a numeric to a categorical (text) array?
Difficulty Level: L2
Question: Bin the petal length (3rd) column of iris_2d
to form a text array, such that if petal length is:
1 | Less than 3 --> 'small' |
难度:L2
问题:将 iris_2d
的 petal length(第三列)转换以构建一个文本数组,按如下规则进行转换:
1 | Less than 3 –> 'small' |
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution 1
1 | # Bin petallength |
Solution 2
1 | 2].astype(float) petal_length = iris[:, |
41. 如何基于 NumPy 数组现有列创建一个新的列?
English Version
Title: How to create a new column from existing columns of a numpy array?
Difficulty Level: L2
Question: Create a new column for volume in iris_2d
, where volume is (pi x petallength x sepal_length^2)/3
.
难度:L2
问题:为 iris_2d
中的 volume 列创建一个新的列,volume 指 (pi x petal_length x sepal_length^2)/3
。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution 1
1 | 2].astype(float) * (iris_2d[:, 0].astype(float))**2) / 3 volume = (np.pi * iris_2d[:, |
Solution 2
1 | # Compute volume |
42. 如何在 NumPy 中执行概率采样?
English Version
Title: How to do probabilistic sampling in numpy?
Difficulty Level: L3
Question: Randomly sample iris’s species
such that setosa
is twice the number of versicolor
and virginica
.
难度:L3
问题:随机采样 iris 数据集中的 species
列,使得 setosa
的数量是 versicolor
和 virginica
数量的两倍。
1 | # Import iris keeping the text column intact |
Solution
1 | # Get the species column |
43. 如何在多维数组中找到一维的第二最大值?
English Version
Title: How to get the second largest value of an array when grouped by another array?
Difficulty Level: L2
Question: What is the value of second longest petal length
of species setosa
难度:L2
问题:在 species setosa
的 petal length
列中找到第二最大值。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution
1 | 4] == b"Iris-setosa", :] iris_setosa = iris[iris[:, |
44. 如何用给定列将 2 维数组排序?
English Version
Title: How to sort a 2D array by a column
Difficulty Level: L2
Question: Sort the iris dataset based on sepal length
column.
难度:L2
问题:基于 sepal length
列将 iris 数据集排序。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution
1 | 0]) index = np.argsort(iris[:, |
45. 如何在 NumPy 数组中找到最频繁出现的值?
English Version
Title: How to find the most frequent value in a numpy array?
Difficulty Level: L1
Question: Find the most frequent value of petal length
(3rd column) in iris dataset.
难度:L1
问题:在 iris 数据集中找到 petal length
(第三列)中最频繁出现的值。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution
1 | 2], return_counts=True) uniques, counts = np.unique(iris[:, |
46. 如何找到第一个大于给定值的数的位置?
English Version
Title: How to find the position of the first occurrence of a value greater than a given value?
Difficulty Level: L2
Question: Find the position of the first occurrence of a value greater than 1.0 in petal width
4th column of iris dataset.
难度:L2
问题:在 iris 数据集的 petal width
(第四列)中找到第一个值大于 1.0 的数的位置。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
Solution 1
1 | 3].astype(float) > 1.0)[0][0] np.argwhere(iris[:, |
Solution 2
1 | len(iris)) index = np.arange( |
47. 如何将数组中所有大于给定值的数替换为给定的 cutoff 值?
English Version
Title: How to replace all values greater than a given value to a given cutoff?
Difficulty Level: L2
Question: From the array a
, replace all values greater than 30 to 30 and less than 10 to 10.
难度:L2
问题:对于数组 a
,将所有大于 30 的值替换为 30,将所有小于 10 的值替换为 10。
输入:
1 | 100) np.random.seed( |
Solution 1
1 | # Cutoff in-place |
Solution 2
1 | 10, a_max=30) a_cutoff = np.clip(a, a_min= |
Solution 3
1 | 10, 10, np.where(a > 30, 30, a)) a_cutoff = np.where(a < |
48. 如何在 NumPy 数组中找到 top-n 数值的位置?
English Version
Title: How to get the positions of top n values from a numpy array?
Difficulty Level: L2
Question: Get the positions of top 5 maximum values in a given array a
.
难度:L2
问题:在给定数组 a
中找到 top-5 最大值的位置。
输入:
1 | 100) np.random.seed( |
Solution 1
1 | 1] index = np.argsort(a)[::- |
Solution 2
1 | # Assume each element in array `a` is nonnegative |
49. 如何逐行计算数组中所有值的数量?
English Version
Title: How to compute the row wise counts of all possible values in an array?
Difficulty Level: L4
Question: Compute the counts of unique values row-wise.
难度:L4
问题:逐行计算唯一值的数量。
输入:
1 | 100) np.random.seed( |
期望输出:
1 | [[1, 0, 2, 1, 1, 1, 0, 2, 2, 0], |
输出包含 10 个列,表示从 1 到 10 的数字。这些数值分别代表每一行的计数数量。例如,Cell(0, 2) 中有值 2,这意味着,数字 3 在第一行出现了两次。
Solution 1
1 | # Assume each number is in [1, 10] |
Solution 2
1 | # More general |
50. 如何将 array_of_arrays 转换为平面 1 维数组?
English Version
Title: How to convert an array of arrays into a flat 1d array?
Difficulty Level: 2
Question: Convert array_of_arrays
into a flat linear 1d array.
难度:L2
问题:将 array_of_arrays
转换为平面线性 1 维数组。
输入:
1 | 3) arr1 = np.arange( |
期望输出:
1 | array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
Solution 1
1 | for arr in array_of_arrays]) arr2d = np.concatenate([arr |
Solution 2
1 | for arr in array_of_arrays for a in arr]) arr2d = np.array([a |
51. 如何为 NumPy 数组生成 one-hot 编码?
English Version
Title: How to generate one-hot encodings for an array in numpy?
Difficulty Level L4
Question: Compute the one-hot encodings (dummy binary variables for each unique value in the array).
难度:L4
问题:计算 one-hot 编码。
输入:
1 | 101) np.random.seed( |
期望输出:
1 | array([[0., 1., 0.], |
Solution 1
1 | 1 arr_shift = arr - |
Solution 2
1 | def one_hot_encodings(arr): |
52. 如何创建由类别变量分组确定的一维数值?
English Version
Title: How to create row numbers grouped by a categorical variable?
Difficulty Level: L3
Question: Create row numbers grouped by a categorical variable. Use the following sample from iris species
as input.
难度:L3
问题:创建由类别变量分组的行数。使用以下来自 iris species
的样本作为输入。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
期望输出:
1 | [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5] |
Solution 1
1 | groups = [] |
Solution 2
1 | for val in np.unique(species_small) for i, grp in enumerate(species_small[species_small==val])] [i |
53. 如何基于给定的类别变量创建分组 id?
English Version
Title: How to create groud ids based on a given categorical variable?
Difficulty Level: L4
Question: Create group ids based on a given categorical variable. Use the following sample from iris species
as input.
难度:L4
问题:基于给定的类别变量创建分组 id。使用以下来自 iris species
的样本作为输入。
输入:
1 | "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" url = |
期望输出:
1 | [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2] |
Solution
1 | len(species_small), 0) output = np.full( |
54. 如何使用 NumPy 对数组中的项进行排序?
English Version
Title: How to rank items in an array using numpy?
Difficulty Level: L2
Question: Create the ranks for the given numeric array a
.
难度:L2
问题:为给定的数值数组 a
创建排序。
输入:
1 | 10) np.random.seed( |
期望输出:
1 | array([4, 2, 6, 0, 8, 7, 9, 3, 5, 1]) |
Solution
1 | np.argsort(np.argsort(a)) |
55. 如何使用 NumPy 对多维数组中的项进行排序?
English Version
Title: How to rank items in a multidimensional array using numpy?
Difficulty Level: L3
Question: Create a rank array of the same shape as a given numeric array a
.
难度:L3
问题:给出一个数值数组 a
,创建一个形态相同的排序数组。
输入:
1 | 10) np.random.seed( |
期望输出:
1 | array([[4, 2, 6, 0, 8], |
Solution 1
1 | a_flat = a.flatten() |
Solution 2
1 | a.ravel().argsort().argsort().reshape(a.shape) |
56. 如何在 2 维 NumPy 数组中找到每一行的最大值?
English Version
Title: How to find the maximum value in each row of a numpy array 2d?
Difficulty Level: L2
Question: Compute the maximum for each row in the given array.
难度:L2
问题:在给定数组中找到每一行的最大值。
1 | 100) np.random.seed( |
Solution 1
1 | 1) np.amax(a, axis= |
Solution 2
1 | max, arr=a, axis=1) np.apply_along_axis(np. |
57. 如何计算 2 维 NumPy 数组每一行的 min-by-max?
English Version
Title: How to compute the min-by-max for each row for a numpy array 2d?
Difficulty Level: L3
Question: Compute the min-by-max for each row for given 2d numpy array.
难度:L3
问题:给定一个 2 维 NumPy 数组,计算每一行的 min-by-max。
1 | 100) np.random.seed( |
Solution
1 | lambda x: np.min(x)/np.max(x), axis=1, arr=a) np.apply_along_axis( |
58. 如何在 NumPy 数组中找到重复条目?
English Version
Title: How to find the duplicate records in a numpy array?
Difficulty Level: L3
Question: Find the duplicate entries (2nd occurrence onwards) in the given numpy array and mark them as True
. First time occurrences should be False
.
难度:L3
问题:在给定的 NumPy 数组中找到重复条目(从第二次出现开始),并将其标记为 True
。第一次出现的条目需要标记为 False
。
输入:
1 | 100) np.random.seed( |
期望输出:
1 | array([False, True, False, True, False, False, True, True, True, |
Solution
1 | 0], True) out = np.full(a.shape[ |
59. 如何找到 NumPy 的分组平均值?
English Version
Title: How to find the grouped mean in numpy?
Difficulty Level L3
Question: Find the mean of a numeric column grouped by a categorical column in a 2D numpy array.
难度:L3
问题:在 2 维 NumPy 数组的类别列中找到数值 sepal length
的平均值。
输入:
1 | url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" |
期望输出:
1 | [[b'Iris-setosa', 3.418], |
Solution
1 | 4]) uniques = np.unique(iris[:, |
60. 如何将 PIL 图像转换成 NumPy 数组?
English Version
Title: How to convert a PIL image to numpy array?
Difficulty Level: L3
Question: Import the image from the following url and convert it to a numpy array.
难度:L3
问题:从以下 url 中导入图像,并将其转换成 NumPy 数组。
1 | "https://upload.wikimedia.org/wikipedia/commons/8/8b/Denali_Mt_McKinley.jpg" url = |
Solution
1 | import requests |
61. 如何删除 NumPy 数组中所有的缺失值?
English Version
Title: How to drop all missing values from a numpy array?
Difficulty Level: L2
Question: Drop all nan
values from a 1D numpy array.
难度:L2
问题:从 1 维 NumPy 数组中删除所有的 nan
值。
输入:
1 | 1, 2, 3, np.nan, 5, 6, 7, np.nan]) arr = np.array([ |
期望输出:
1 | array([1., 2., 3., 5., 6., 7.]) |
Solution
1 | arr[~np.isnan(arr)] |
62. 如何计算两个数组之间的欧几里得距离?
English Version
Title: How to compute the euclidean distance between two arrays?
Difficulty Level: L1
Question: Compute the euclidean distance between two arrays a
and b
.
难度:L1
问题:计算两个数组 a
和 b
之间的欧几里得距离。
输入:
1 | 1, 2, 3, 4, 5]) a = np.array([ |
Solution 1
1 | sum((a-b)**2)) np.sqrt(np. |
Solution 2
1 | np.linalg.norm(a-b) |
63. 如何在一个 1 维数组中找到所有的局部极大值(peak)?
English Version
Title: How to find all the local maxima (or peaks) in a 1d array?
Difficulty Level: L4
Question: Find all the peaks in a 1D numpy array a
. Peaks are points surrounded by smaller values on both sides.
难度:L4
问题:在 1 维数组 a
中找到所有的 peak。peak 是指一个数字比两侧的数字都大。
输入:
1 | 1, 3, 7, 1, 2, 6, 0, 1]) a = np.array([ |
期望输出:
1 | array([2, 5]) |
其中 2 和 5 是局部最大值 7 和 6 的下标。
Solution
1 | double_diff = np.diff(np.sign(np.diff(a))) |
64. 如何从 2 维数组中减去 1 维数组,从 2 维数组的每一行分别减去 1 维数组的每一项?
English Version
Title: How to subtract a 1d array from a 2d array, where each item of 1d array subtracts from respective row?
Difficulty Level: L2
Question: Subtract the 1d array b_1d
from the 2d array a_2d
, such that each item of b_1d
subtracts from respective row of a_2d
.
难度:L2
问题:从 2 维数组 a_2d
中减去 1 维数组 b_1d
,即从 a_2d
的每一行分别减去 b_1d
的每一项。
输入:
1 | 3, 3, 3],[4, 4, 4],[5, 5, 5]]) a_2d = np.array([[ |
期望输出:
1 | array([[2, 2, 2], |
Solution
1 | a_2d - b_1d[:, np.newaxis] |
65. 如何在数组中找出某个项的第 n 个重复索引?
English Version
Title: How to find the index of n’th repetition of an item in an array
Difficulty Level L2
Question: Find the index of 5th repetition of number 1 in x
.
难度:L2
问题:找到数组 x
中数字 1 的第 5 个重复索引。
输入:
1 | 1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2]) x = np.array([ |
Solution 1
1 | 5 n = |
Solution 2
1 | 5 n = |
Solution 3
1 | 5 n = |
66. 如何将 NumPy 的 datetime64 对象(object)转换为 datetime 的 datetime 对象?
English Version
Title: How to convert numpy’s datetime64 object to datetime’s datetime object?
Difficulty Level: L2
Question: Convert numpy’s datetime64
object to datetime’s datetime
object.
难度:L2
问题:将 NumPy 的 datetime64
对象转换为 datetime 的 datetime
对象。
1 | # Input: a numpy datetime64 object |
Solution 1
1 | dt64.tolist() |
Solution 2
1 | from datetime import datetime |
67. 如何计算 NumPy 数组的移动平均数?
English Version
Title: How to compute the moving average of a numpy array?
Difficulty Level: L3
Question: Compute the moving average of window size 3, for the given 1D array.
难度:L3
问题:给定 1 维数组,计算 window size 为 3 的移动平均数。
输入:
1 | 100) np.random.seed( |
Solution 1
Source: How to calculate moving average using NumPy?
1 | def moving_average(a, n=3): |
Solution 2
1 | 3)/3, mode="valid").round(2) np.convolve(Z, np.ones( |
68. 给定起始数字、length 和步长,如何创建一个 NumPy 数组序列?
English Version
Title: How to create a numpy array sequence given only the starting point, length and the step?
Difficulty Level: L2
Question: Create a numpy array of length 10, starting from 5 and has a step of 3 between consecutive numbers.
难度:L2
问题:从 5 开始,创建一个 length 为 10 的 NumPy 数组,相邻数字的差是 3。
Solution 1
1 | def seq(start, length, step): |
Solution 2
1 | 5, 5+3*10, 3) np.arange( |
69. 如何在不规则 NumPy 日期序列中填充缺失日期?
English Version
Title: How to fill in missing dates in an irregular series of numpy dates?
Difficulty Level: L3
Question: Given an array of a non-continuous sequence of dates. Make it a continuous sequence of dates, by filling in the missing dates.
难度:L3
问题:给定一个非连续日期序列的数组,通过填充缺失的日期,使其变成连续的日期序列。
输入:
1 | "2018-02-01"), np.datetime64("2018-02-25"), 2) dates = np.arange(np.datetime64( |
Solution 1
1 | out = [] |
Solution 2
1 | for date, d in zip(dates, np.diff(dates))]).reshape(-1) filled_in = np.array([np.arange(date, (date+d)) |
70. 如何基于给定的 1 维数组创建 strides?
English Version
Title: How to create strides from a given 1D array?
Difficulty Level: L4
Question: From the given 1d array arr
, generate a 2d matrix using strides, with a window length of 4 and strides of 2, like [[0,1,2,3], [2,3,4,5], [4,5,6,7]..]
难度:L4
问题:给定 1 维数组 arr
,使用 strides 生成一个 2 维矩阵,其中 window length 等于 4,strides 等于 2,例如 [[0,1,2,3], [2,3,4,5], [4,5,6,7]..]。
输入:
1 | 15) arr = np.arange( |
期望输出:
1 | array([[ 0, 1, 2, 3], |
Solution
1 | def gen_strides(a, stride_len=5, window_len=5): |