0%

# 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.

Solution

# 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.

Solution

# 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.

Solution 1
Solution 2

# 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.

Solution

# 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.

Solution

# 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.

Solution 1
Solution 2

# 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.

Solution

# 8. 如何垂直堆叠两个数组？

English Version

Title: How to stack two arrays vertically?

Difficulty Level: L2

Question: Stack arrays a and b vertically.

Solution 1
Solution 2
Solution 3

# 9. 如何水平堆叠两个数组？

English Version

Title: How to stack two arrays horizontally?

Difficulty Level: L2

Question: Stack the arrays a and b horizontally.

Solution 1
Solution 2
Solution 3

# 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.

Solution 1
Solution 2

# 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.

Solution

# 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.

Solution

# 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.

Solution

# 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.

Solution 1
Solution 2
Solution 3

# 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.

Solution

# 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.

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

# 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.

Solution

# 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.

Solution

# 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.

Solution 1
Solution 2
Solution 3

# 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.

Solution

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

Solution

# 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.

Solution

# 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.

Solution 1
Solution 2

# 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.

Iris Data Set 网页下载数据集 iris.data

Solution

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.

Solution 1
Solution 2

# 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.

Solution

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

Solution

# 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.

Solution

# 30. 如何计算 softmax 分数？

English Version

Title: How to compute the softmax score?

Difficulty Level: L3

Question: Compute the softmax score of sepal length.

Solution

According formula:

$S(x_i) = \frac{e^{x_i}}{\sum_j e^{x_j}}$

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

# 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.

Solution

# 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.

Solution 1
Solution 2

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

Solution 1
Solution 2

# 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.

Solution

# 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.

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

# 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.

Solution

# 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.

Solution

# 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:

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

# 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.

Solution

# 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

Solution

# 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.

Solution

# 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.

Solution

# 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.

Solution 1
Solution 2

# 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.

Solution 1
Solution 2
Solution 3

# 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.

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

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

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

# 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.

Solution

# 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.

Solution

# 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.

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

# 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.

Solution

# 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.

Solution

# 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.

Solution

# 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.

Solution

# 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.

Solution

# 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.

Solution 1
Solution 2

# 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.

Solution

# 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.

Solution

# 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.

Solution 1
Solution 2
Solution 3

# 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.

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

# 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.

Solution 1
Solution 2

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

Solution