Numpy实用函数总结TOP50

创建数组

1. array

2. linspace

-> start  : [optional] start of interval range. By default start = 0
-> stop   : end of interval range
-> restep : If True, return (samples, step). By default restep = False
-> num    : [int, optional] No. of samples to generate
-> dtype  : type of output array

3. arange

根据start与stop指定的范围以及step设定的步长,生成一个 ndarray。

arange允许步长为小数,而range不允许。

The built-in range generates Python built-in integers that have arbitrary size, while numpy.arange produces numpy.int32 or numpy.int64 numbers. This may result in incorrect results for large integer values:

4. Uniform

从下限值和上限值之间的均匀分布生成随机样本。

5. Random.randint

在一个范围内生成 n 个随机整数样本。

6. Random.rand

7. Random.randn

8. Random.random

9. Random.choice

从给定数组a中随机选择,p可以指定a中每个元素被选择的概率

10. Random.seed()

11. Logspace

12. Zeroes

13. Ones

14. Array of k Random Value

15. Identity

操纵数组

16. min

17. max

18. Unique

19. Mean

20. Median

21. Digitize

Return the indices of the bins to which each value in the input array belongs.

Exp        Value
x < 0     :   0
0 <= x <1 :   1
1 <= x <2 :   2
2 <= x <3 :   3
3 <=x     :   4
Compares -0.9 to 0, here x < 0 so Put 0 in resulting array.
Compares  0.5 to 0, here 0 <= x <1 so Put 1.
.
Compares 5.4 to 4, here 3<=x so Put 4

22. Reshape

It Returns an array containing the same data with a new shape.

23. Expand Dimensions

It is used to expand the dimensions of an array. This method is useful for creating sample test data for testing a machine learning model.

24. Squeeze

Reduce the dimension of an array by removing single-dimensional entries.

25. Count Non-Zero

26. argwhere

Find and return all the indices of non-zero elements.

29. argmax & argmin

argmax returns the index of the max element from the array. It can be used to get the index of high probability predicted labels in multiclass image classification problems.

30. Sort

31. abs

32. round

33. clip

It is used to keep the values of an array within a range.

数值替换

34. where

35. put

Replaces specified elements of an array with given values.

36. copyto

集合操作

37. intersect1d

np.intersect1d function returns all the unique values from both arrays in a sorted manner.

图片名称

array([0, 2, 3, 4], dtype=int64) : Indices of common elements inside first array.
array([5, 0, 1, 2], dtype=int64)) : Indices of common elements inside second array.

38. difference

np.setdiff1d function returns all the unique elements from array 1 that are not present in arr2.

图片名称

39. union

union1d function will combine both arrays into one.

图片名称

分割

40. Horizontal Split

hsplit function will split the data horizontally into n equal parts.

图片名称

41. Vertical Split

vsplit will split the data vertically into n equal parts.

图片名称

合并

42. Horizontal Stacking

hstack will stack appends one array at the end of another.

图片名称

43. Vertical Stacking

vstack will stack one array on top of another.

图片名称

比较

44. allclose

np.allclose function finds whether two arrays are equal or approximately equal to each other based on some tolerance value if the shape of both arrays is the same.

If the following equation is element-wise True, then allclose returns True.

absolute(a - b) <= (atol + rtol * absolute(b))

45. equals

It compares each element of both arrays and if elements matched returns True.

重复

46. repeat

It is used to repeat elements of an array for n number of times.

47. tile

Construct an array by repeating A the number of times given by reps.

代数计算

48. Einsum

This function is used to compute many multi-dimensional and linear algebraic operations on arrays.

统计分析

49. Histogram

It is an important statistical analysis function of NumPy that computes histogram values for a set of data.

50. Percentile

It computes the q-th percentile of the data along a specified axis.

51. Standard Deviation

52. Variance

输出设置

53. 小数保留

54. 全部展示

增加宽度

存储导入

55. savetxt

56. loadtxt

补充

57. flatten

58. ceil

59. floor

60. rint

61. sign

62. mod

63. floor_divide

64. shuffle

65. argsort