numpy 学习

numpy 参考文档

基础

NumPy 提供一个 N-dimensional 的数组,称为 ndarray,是一些相同类型和大小的items的集合体。

在方法中用 axis 来表示 dimensional。

ndarray 类

官方文档

ndarray 属性

ndarray.shape 通常用于获取数组的当前形状,也可用于重塑数组。
ndarray.size 数组元素个数
ndarray.ndim 数组维度数
ndarray.nbytes 数组总字节数
ndarray.itemsize 数组元素字节大小
ndarray.flags 数组内存分布信息
ndarray.strides Tuple of bytes to step in each dimension when traversing an array.
ndarray.data Python buffer object pointing to the start of the array’s data.
ndarray.base Base object if memory is from some other object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# ndarray.shape
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)
>>> y.shape = (3, 8)
>>> y
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])

# ndarray.ndim
>>> x = np.array([1, 2, 3])
>>> x.ndim
1
>>> y = np.zeros((2, 3, 4))
>>> y.ndim
3

# ndarray.size
>>> x = np.zeros((3, 5, 2), dtype=np.complex128)
>>> x.size
30

# ndarray.itemsize/nbytes
>>> x = np.zeros((3,5,2), dtype=np.complex128)
>>> x.nbytes
480
>>> np.prod(x.shape) * x.itemsize
480

ndarray 方法

一般习惯用外部的方法。

索引&切片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
###  基础 索引&切片
# 注意基础的切片返回的是view,及目标和结果是同一个object。
>>> x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# start:stop:step
>>> x[1:7:2]
array([1, 3, 5])

# 负数i,以n+i处理
>>> x[-2:10]
array([8, 9])
>>> x[-3:3:-1]
array([7, 6, 5, 4])

# '::' 等同于 ':'。未填的项表示选取所有。
>>> x[5:]
array([5, 6, 7, 8, 9])

# 当选取元祖维度不够时,未填的项表示选取所有。
>>> x = np.array([[[1],[2],[3]], [[4],[5],[6]]])
>>> x.shape
(2, 3, 1)
>>> x[1:2] #只对第一维进行选取操作。
array([[[4],
[5],
[6]]])
>>> x[1] #注意:没有取范围,所以维度减1
array([[4],
[5],
[6]])
# 如果不用':',而是直接取,则会造成维度减少。
>>> x[:,1,:]
array([[2],
[5]])
>>> x[:,1:2,:]
array([[[2]],
[[5]]])

# '...'省略号代表所有的':'
>>> x[...,0]
array([[1, 2, 3],
[4, 5, 6]])
>>> x[:,:,0] # 等同
array([[1, 2, 3],
[4, 5, 6]])

# newaxis 则是会直接添加一个轴
>>> x[:,np.newaxis,:,:].shape
(2, 1, 3, 1)

###### 高级索引&切片
## 高级的切片得到的数组是copy的。

#### 数值型高级索引。
>>> x = np.array([[1, 2], [3, 4], [5, 6]])
>>> x[[0, 1, 2], [0, 1, 0]] # 前一个代表行索引,后一个代表列索引。
array([1, 4, 5])
# 多维的索引数组
>>> x = array([[ 0, 1, 2],
true [ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
>>> rows = np.array([[0, 0],
[3, 3]], dtype=np.intp)
>>> columns = np.array([[0, 2],
[0, 2]], dtype=np.intp)
>>> x[rows, columns] # 结果数组的格式与索引数组的格式一致。
array([[ 0, 2],
[ 9, 11]])
# 使用np.newaxis使得行列索引数组大小不同,触发广播broadcasting
>>> rows = np.array([0, 3], dtype=np.intp)
>>> columns = np.array([0, 2], dtype=np.intp)
>>> rows[:, np.newaxis]
array([[0],
[3]])
>>> x[rows[:, np.newaxis], columns]
array([[ 0, 2],
[ 9, 11]])
# 使用ix_()触发广播
>>> x[np.ix_(rows, columns)]
array([[ 0, 2],
[ 9, 11]])

#### Boolean型高级索引
>>> x = np.array([[1., 2.], [np.nan, 3.], [np.nan, np.nan]])
>>> x[~np.isnan(x)]
array([ 1., 2., 3.])
>>> x = np.array([1., -1., -2., 3])
>>> x[x < 0] += 20
>>> x
array([ 1., 19., 18., 3.])
# 选取和小于等于2的行
>>> x = np.array([[0, 1], [1, 1], [2, 2]])
>>> rowsum = x.sum(-1)
>>> x[rowsum <= 2, :]
array([[0, 1],
[1, 1]])

通用函数 ufunc

广播 Broadcasting

执行 broadcast 的前提在于,两个 ndarray 执行的是 element-wise(按位加减等) 的运算,而不是矩阵乘法的运算,矩阵乘法运算时需要维度之间严格匹配。

broadcasting rules:

  1. 两个数组shape相等。
  2. 两个数组shape不相同,但其中一个为1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 例子:
Image (3d array): 256 x 256 x 3
Scale (1d array): 3
Result (3d array): 256 x 256 x 3

A (4d array): 8 x 1 x 6 x 1
B (3d array): 7 x 1 x 5
Result (4d array): 8 x 7 x 6 x 5

A (2d array): 5 x 4
B (1d array): 1
Result (2d array): 5 x 4

A (2d array): 15 x 3 x 5
B (1d array): 15 x 1 x 5
Result (2d array): 15 x 3 x 5

数学运算

add Add arguments element-wise.
subtract Subtract arguments, element-wise.
multiply Multiply arguments element-wise.
divide Returns a true division of the inputs, element-wise.
logaddexp Logarithm of the sum of exponentiations of the inputs.
logaddexp2 Logarithm of the sum of exponentiations of the inputs in base-2.
true_divide Returns a true division of the inputs, element-wise.
floor_divide Return the largest integer smaller or equal to the division of the inputs.
negative Numerical negative, element-wise.
positive Numerical positive, element-wise.
power First array elements raised to powers from second array, element-wise.
remainder Return element-wise remainder of division.
mod Return element-wise remainder of division.
fmod Return the element-wise remainder of division.
divmod Return element-wise quotient and remainder simultaneously.
absolute Calculate the absolute value element-wise.
fabs Compute the absolute values element-wise.
rint Round elements of the array to the nearest integer.
sign Returns an element-wise indication of the sign of a number.
heaviside Compute the Heaviside step function.
conj Return the complex conjugate, element-wise.
exp Calculate the exponential of all elements in the input array.
exp2 Calculate 2**p for all p in the input array.
log Natural logarithm, element-wise.
log2 Base-2 logarithm of x.
log10 Return the base 10 logarithm of the input array, element-wise.
expm1 Calculate exp(x) - 1 for all elements in the array.
log1p Return the natural logarithm of one plus the input array, element-wise.
sqrt Return the non-negative square-root of an array, element-wise.
square Return the element-wise square of the input.
cbrt Return the cube-root of an array, element-wise.
reciprocal Return the reciprocal of the argument, element-wise.
gcd Returns the greatest common divisor of ` x1 and x2 `
lcm Returns the lowest common multiple of ` x1 and x2 `

三角运算

sin Trigonometric sine, element-wise.
cos Cosine element-wise.
tan Compute tangent element-wise.
arcsin Inverse sine, element-wise.
arccos Trigonometric inverse cosine, element-wise.
arctan Trigonometric inverse tangent, element-wise.
arctan2 Element-wise arc tangent of x1/x2 choosing the quadrant correctly.
hypot Given the “legs” of a right triangle, return its hypotenuse.
sinh Hyperbolic sine, element-wise.
cosh Hyperbolic cosine, element-wise.
tanh Compute hyperbolic tangent element-wise.
arcsinh Inverse hyperbolic sine element-wise.
arccosh Inverse hyperbolic cosine, element-wise.
arctanh Inverse hyperbolic tangent element-wise.
deg2rad Convert angles from degrees to radians.
rad2deg Convert angles from radians to degrees.

对比运算

greater Return the truth value of (x1 > x2) element-wise.
greater_equal Return the truth value of (x1 >= x2) element-wise.
less Return the truth value of (x1 < x2) element-wise.
less_equal Return the truth value of (x1 =< x2) element-wise.
not_equal Return (x1 != x2) element-wise.
equal Return (x1 == x2) element-wise.
logical_and Compute the truth value of x1 AND x2 element-wise.
logical_or Compute the truth value of x1 OR x2 element-wise.
logical_xor Compute the truth value of x1 XOR x2, element-wise.
logical_not Compute the truth value of NOT x element-wise.
maximum Element-wise maximum of array elements.
minimum Element-wise minimum of array elements.
fmax Element-wise maximum of array elements.
fmin Element-wise minimum of array elements.

常用功能

数组生成

完整文档

Ones and zeros

Ones and zeros
empty Return a new array of given shape and type, without initializing entries.
empty_like Return a new array with the same shape and type as a given array.
eye Return a 2-D array with ones on the diagonal and zeros elsewhere.
identity Return the identity array.
ones Return a new array of given shape and type, filled with ones.
ones_like Return an array of ones with the same shape and type as a given array.
zeros Return a new array of given shape and type, filled with zeros.
zeros_like Return an array of zeros with the same shape and type as a given array.
full Return a new array of given shape and type, filled with fill_value.
full_like Return a full array with the same shape and type as a given array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
## 常用例子
# .eye
>>> np.eye(2, dtype=int)
array([[1, 0],
[0, 1]])
>>> np.eye(3, k=1)
array([[ 0., 1., 0.],
[ 0., 0., 1.],
[ 0., 0., 0.]])

# .ones()
>>> np.ones(5)
array([ 1., 1., 1., 1., 1.])
>>> np.ones((5,), dtype=int)
array([1, 1, 1, 1, 1])
>>> np.ones((2, 1))
array([[ 1.],
[ 1.]])

# .ones_like()
>>> y = np.arange(3, dtype=float)
>>> y
array([ 0., 1., 2.])
>>> np.ones_like(y)
array([ 1., 1., 1.])

# .zeros()
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0., 0.],
[ 0., 0.]])

# .zeros_like()
>>> y = np.arange(3, dtype=float)
>>> y
array([ 0., 1., 2.])
>>> np.zeros_like(y)
array([ 0., 0., 0.])

# .full()
>>> np.full((2, 2), np.inf)
array([[ inf, inf],
[ inf, inf]])
>>> np.full((2, 2), 10)
array([[10, 10],
[10, 10]])

# .full_like()
>>> x = np.arange(6, dtype=int)
>>> np.full_like(x, 1)
array([1, 1, 1, 1, 1, 1])
>>> np.full_like(x, 0.1)
array([0, 0, 0, 0, 0, 0])
>>> np.full_like(x, 0.1, dtype=np.double)
array([ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])
>>> np.full_like(x, np.nan, dtype=np.double)
array([ nan, nan, nan, nan, nan, nan])

From existing data

From existing data
array Create an array.
asarray Convert the input to an array.
asanyarray Convert the input to an ndarray, but pass ndarray subclasses through.
ascontiguousarray Return a contiguous array in memory (C order).
asmatrix Interpret the input as a matrix.
copy Return an array copy of the given object.
frombuffer Interpret a buffer as a 1-dimensional array.
fromfile Construct an array from data in a text or binary file.
fromfunction Construct an array by executing a function over each coordinate.
fromiter Create a new 1-dimensional array from an iterable object.
fromstring A new 1-D array initialized from text data in a string.
loadtxt Load data from a text file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
## 常用例子
# .array()
>>> np.array([1, 2, 3])
array([1, 2, 3])
>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j, 2.+0.j, 3.+0.j])

# .copy()
>>> x = np.array([1, 2, 3])
>>> y = x
>>> z = np.copy(x)
>>> x[0] = 10
>>> x[0] == y[0]
True
>>> x[0] == z[0]
False

# .fromfunction()
>>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4]])
>>> np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)
array([[ True, False, False],
[False, True, False],
[False, False, True]])

# .fromString()
>>> np.fromstring('1 2', dtype=int, sep=' ')
array([1, 2])
>>> np.fromstring('1, 2', dtype=int, sep=',')
array([1, 2])

Numerical ranges

Numerical ranges
arange Return evenly spaced values within a given interval.
linspace Return evenly spaced numbers over a specified interval.
logspace Return numbers spaced evenly on a log scale.
geomspace Return numbers spaced evenly on a log scale (a geometric progression).
meshgrid Return coordinate matrices from coordinate vectors.
mgrid nd_grid instance which returns a dense multi-dimensional “meshgrid”.
ogrid nd_grid instance which returns an open multi-dimensional “meshgrid”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
## 常用例子
# .arange()
>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0., 1., 2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])

# .linspace()
>>> np.linspace(2.0, 3.0, num=5)
array([ 2. , 2.25, 2.5 , 2.75, 3. ])
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
array([ 2. , 2.2, 2.4, 2.6, 2.8])
>>> np.linspace(2.0, 3.0, num=5, retstep=True)
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)

数组操作

完整文档

Changing array shape

Changing array shape
reshape Gives a new shape to an array without changing its data.
ravel Return a contiguous flattened array.
ndarray.flat A 1-D iterator over the array.
ndarray.flatten Return a copy of the array collapsed into one dimension.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
## 常用函数
# .reshape
>>> a = np.arange(6).reshape((3, 2))
>>> a
array([[0, 1],
[2, 3],
[4, 5]])
>>> np.reshape(a, (3,-1)) # 形状的一个维度可以是-1,自适应
array([[0, 1],
[2, 3],
[4, 5]])

# .ravel 等同于.reshape(-1)
>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> print(np.ravel(x))
[1 2 3 4 5 6]

Transpose-like operations

Transpose-like operations
moveaxis Move axes of an array to new positions.
rollaxis Roll the specified axis backwards, until it lies in a given position. 可用moveaxis实现。
swapaxes Interchange two axes of an array.
ndarray.T Same as self.transpose(), except that self is returned if self.ndim < 2.
transpose Permute the dimensions of an array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
## 常用例子
# .moveaxis() 移动指定轴到指定位置,其他轴不变
>>> x = np.zeros((3, 4, 5))
>>> np.moveaxis(x, 0, -1).shape
(4, 5, 3)
>>> np.moveaxis(x, -1, 0).shape
(5, 3, 4)

# .swapaxes() 交换两个轴
>>> x = np.array([[1,2,3]])
>>> np.swapaxes(x,0,1)
array([[1],
[2],
[3]])

# ndarray.T .transpose() 转置
>>> x = np.array([[1.,2.],[3.,4.]])
>>> x
array([[ 1., 2.],
[ 3., 4.]])
>>> x.T
array([[ 1., 3.],
[ 2., 4.]])
>>> np.transpose(x)
array([[ 1., 3.],
[ 2., 4.]])

Changing number of dimensions

Changing number of dimensions
atleast_1d Convert inputs to arrays with at least one dimension.
atleast_2d View inputs as arrays with at least two dimensions.
atleast_3d View inputs as arrays with at least three dimensions.
broadcast Produce an object that mimics broadcasting.
broadcast_to Broadcast an array to a new shape.
broadcast_arrays Broadcast any number of arrays against each other.
expand_dims(a, axis) Expand the shape of an array.
squeeze(a[, axis]) Remove single-dimensional entries from the shape of an array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## 常用函数
# .expand_dims() 添加一个新轴 效果等同于np.newaxis
>>> x = np.array([1,2])
>>> x.shape
(2,)
>>> y = np.expand_dims(x, axis=0) # 等同于 x[np.newaxis,:]和x[np.newaxis]
>>> y
array([[1, 2]])
>>> y.shape
(1, 2)
>>> y = np.expand_dims(x, axis=1) # 等同于 x[:,np.newaxis]
>>> y
array([[1],
[2]])
>>> y.shape
(2, 1)

# .squeeze() 压缩数组,去除长度为1的轴
>>> x = np.array([[[0], [1], [2]]])
>>> x.shape
(1, 3, 1)
>>> np.squeeze(x).shape
(3,)
>>> np.squeeze(x, axis=0).shape
(3, 1)
>>> np.squeeze(x, axis=1).shape
Traceback (most recent call last):

Changing kind of array

Changing kind of array
asarray Convert the input to an array.
asanyarray Convert the input to an ndarray, but pass ndarray subclasses through.
asmatrix Interpret the input as a matrix.
asfarray Return an array converted to a float type.
asfortranarray Return an array laid out in Fortran order in memory.
ascontiguousarray Return a contiguous array in memory (C order).
asarray_chkfinite Convert the input to an array, checking for NaNs or Infs.
asscalar Convert an array of size 1 to its scalar equivalent.
require Return an ndarray of the provided type that satisfies requirements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
## 常用函数
# .asarray() 比.array()更强大一点点
>>> a = [1, 2] # Convert a list into an array:
>>> np.asarray(a)
array([1, 2])
>>> a = np.array([1, 2]) # Existing arrays are not copied:
>>> np.asarray(a) is a
True
#If dtype is set, array is copied only if dtype does not match:
>>> a = np.array([1, 2], dtype=np.float32)
>>> np.asarray(a, dtype=np.float32) is a
True
>>> np.asarray(a, dtype=np.float64) is a
False

Joining arrays

Joining arrays
concatenate Join a sequence of arrays along an existing axis.
stack Join a sequence of arrays along a new axis.
column_stack Stack 1-D arrays as columns into a 2-D array.
dstack Stack arrays in sequence depth wise (along third axis).
hstack Stack arrays in sequence horizontally (column wise).
vstack Stack arrays in sequence vertically (row wise).
block Assemble an nd-array from nested lists of blocks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
### 常用函数
# .concatenate 将两个数组沿已有的轴连接,两个数组除了连接轴形状必须相同!
>>> a = np.array([[1, 2], [3, 4]]) # 2*2
>>> b = np.array([[5, 6]]) # 1*2
>>> np.concatenate((a, b), axis=0) # 3*2
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
[3, 4, 6]])
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])

# .stack() 将一系列数组沿一个新的轴连接,每个数组的形状必须相同!
>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.stack((a, b)) # 维度会加1
array([[1, 2, 3],
[2, 3, 4]])
>>> np.stack((a, b), axis=-1) # -1表示在最后加一维。0表示在开头加一维。
array([[1, 2],
[2, 3],
[3, 4]])

# .dstack .hstack .vstack 都可以用.stack()和.concatenate实现

Splitting arrays

Splitting arrays
split Split an array into multiple sub-arrays.
array_split Split an array into multiple sub-arrays.
dsplit Split array into multiple sub-arrays along the 3rd axis (depth).
hsplit Split an array into multiple sub-arrays horizontally (column-wise).
vsplit Split an array into multiple sub-arrays vertically (row-wise).
1
2
3
4
5
6
7
8
9
10
11
12
### 常用函数
# .split()
>>> x = np.arange(9.0)
>>> np.split(x, 3)
[array([ 0., 1., 2.]), array([ 3., 4., 5.]), array([ 6., 7., 8.])]
>>> x = np.arange(8.0)
>>> np.split(x, [3, 5, 6, 10])
[array([ 0., 1., 2.]),
array([ 3., 4.]),
array([ 5.]),
array([ 6., 7.]),
array([], dtype=float64)]

输入输出

完整文档

NumPy binary files (NPY, NPZ)

NumPy binary files (NPY,NPZ)
load Load arrays or pickled objects from .npy, .npz or pickled files.
save Save an array to a binary file in NumPy .npy format.
savez Save several arrays into a single file in uncompressed .npz format.
savez_compressed Save several arrays into a single file in compressed .npz format.
1
2
3
4
5
### 常用函数
>>> x = np.arange(10)
>>> np.save(outfile, x)
>>> np.load(outfile)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Text files

Text files
loadtxt Load data from a text file.
savetxt Save an array to a text file.
genfromtxt Load data from a text file, with missing values handled as specified.
fromregex Construct an array from a text file, using regular expression parsing.
fromstring A new 1-D array initialized from text data in a string.
ndarray.tofile Write array to a file as text or binary (default).
ndarray.tolist Return the array as a (possibly nested) list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
### 常用函数
# .loadtxt 每一行的数要一样多。
>>> from io import StringIO # StringIO behaves like a file object
>>> c = StringIO(u"0 1\n2 3")
>>> np.loadtxt(c)
array([[ 0., 1.],
[ 2., 3.]])

# .savetxt 注意因为只能保存一维或两维的数组,所以不常用!!
>>> x = y = z = np.arange(0.0,5.0,1.0)
>>> np.savetxt('test.out', x, delimiter=',') # X is an array
>>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays
>>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation

# .fromstring() 只能转换一维数组,不实用
>>> np.fromstring('1 2', dtype=int, sep=' ')
array([1, 2])
>>> np.fromstring('1, 2', dtype=int, sep=',')
array([1, 2])

Text formatting options

Text formatting options
set_printoptions Set printing options.
get_printoptions Return the current print options.
set_string_function Set a Python function to be used when pretty printing arrays.
printoptions Context manager for setting print options.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
### 常用例子
# printing 规则:
# 1. 最后的axis,从左往右打印。
# 2. 倒数第2个axis,从上向下打印。
# 3. 其他axis,也从上向下打印,每个切片之间以空行隔开。

# 默认设置
>>> np.set_printoptions(edgeitems=3,infstr='inf',
... linewidth=75, nanstr='nan', precision=8,
... suppress=False, threshold=1000, formatter=None)

# float 显示位数
>>> np.set_printoptions(precision=4)
>>> print(np.array([1.123456789]))
[ 1.1235]

# 打印完整的数组
>>> np.set_printoptions(threshold=np.nan)

# 不用科学计数法表示
>>> np.set_printoptions(suppress=True)

随机采样(numpy.random)

完整文档

Simple random data

Simple random data
rand Random values in a given shape.
randn Return a sample (or samples) from the “standard normal” distribution.
randint Return random integers from low (inclusive) to high (exclusive).
random_integers Random integers of type np.int between low and high, inclusive.
random_sample Return random floats in the half-open interval [0.0, 1.0).
random Return random floats in the half-open interval [0.0, 1.0).
ranf Return random floats in the half-open interval [0.0, 1.0).
sample Return random floats in the half-open interval [0.0, 1.0).
choice Generates a random sample from a given 1-D array
bytes Return random bytes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
### 常用函数

# .rand .random .random_sample 功能一致,生成指定shape的[0,1)的均匀分布
>>> np.random.rand(3,2) # 只是输入格式不同
array([[ 0.14022471, 0.96360618], #random
[ 0.37601032, 0.25528411], #random
[ 0.49313049, 0.94909878]]) #random
>>> 5 * np.random.random_sample((3, 2)) - 5 #[-5,0)
array([[-3.99149989, -0.52338984],
[-2.99091858, -0.79479508],
[-1.23204345, -1.75224494]])
>>> 5 * np.random.random((3, 2)) - 5 #[-5,0)
array([[-3.99149989, -0.52338984],
[-2.99091858, -0.79479508],
[-1.23204345, -1.75224494]])


# .randn 生成指定shape的标准正太分布(normal,Gaussian)
>>> 2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], #random
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) #random

#### Permutations

Permutations 随机排列
shuffle Modify a sequence in-place by shuffling its contents.
permutation Randomly permute a sequence, or return a permuted range.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
### 常用函数
# .shuffle() 随机打乱数组,但对于多维数组只打乱第一维。
>>> arr = np.arange(10)
>>> np.random.shuffle(arr)
>>> arr
[1 7 5 2 9 4 3 6 0 8]
>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.shuffle(arr)
>>> arr
array([[3, 4, 5],
[6, 7, 8],
[0, 1, 2]])

# .permutation 与 .shuffle()功能相似

Distributions

Distributions 输入各个分布的参数,获得相应shape的样本。
beta Draw samples from a Beta distribution.
binomial Draw samples from a binomial distribution.
chisquare Draw samples from a chi-square distribution.
dirichlet Draw samples from the Dirichlet distribution.
exponential Draw samples from an exponential distribution.
f Draw samples from an F distribution.
gamma Draw samples from a Gamma distribution.
geometric Draw samples from the geometric distribution.
gumbel Draw samples from a Gumbel distribution.
hypergeometric Draw samples from a Hypergeometric distribution.
laplace Draw samples from the Laplace or double exponential distribution with specified location (or mean) and scale (decay).
logistic Draw samples from a logistic distribution.
lognormal Draw samples from a log-normal distribution.
logseries Draw samples from a logarithmic series distribution.
multinomial Draw samples from a multinomial distribution.
multivariate_normal Draw random samples from a multivariate normal distribution.
negative_binomial Draw samples from a negative binomial distribution.
noncentral_chisquare Draw samples from a noncentral chi-square distribution.
noncentral_f Draw samples from the noncentral F distribution.
normal Draw random samples from a normal (Gaussian) distribution.
pareto Draw samples from a Pareto II or Lomax distribution with specified shape.
poisson Draw samples from a Poisson distribution.
power Draws samples in [0, 1] from a power distribution with positive exponent a - 1.
rayleigh Draw samples from a Rayleigh distribution.
standard_cauchy Draw samples from a standard Cauchy distribution with mode = 0.
standard_exponential Draw samples from the standard exponential distribution.
standard_gamma Draw samples from a standard Gamma distribution.
standard_normal Draw samples from a standard Normal distribution (mean=0, stdev=1).
standard_t Draw samples from a standard Student’s t distribution with df degrees of freedom.
triangular Draw samples from the triangular distribution over the interval [left, right].
uniform Draw samples from a uniform distribution.
vonmises Draw samples from a von Mises distribution.
wald Draw samples from a Wald, or inverse Gaussian, distribution.
weibull Draw samples from a Weibull distribution.
zipf Draw samples from a Zipf distribution.

排序,搜索,统计

完整文档

Sorting

Sorting
sort Return a sorted copy of an array.
lexsort Perform an indirect stable sort using a sequence of keys.
argsort Returns the indices that would sort an array.
ndarray.sort Sort an array, in-place.
msort Return a copy of an array sorted along the first axis.
sort_complex Sort a complex array using the real part first, then the imaginary part.
partition Return a partitioned copy of an array.
argpartition Perform an indirect partition along the given axis using the algorithm specified by the kindkeyword.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
### 常用函数

# .sort 默认以最后一维排序。返回的是一个copy。
>>> a = np.array([[1,4],[3,1]])
>>> np.sort(a) # sort along the last axis
array([[1, 4],
[1, 3]])
>>> np.sort(a, axis=None) # sort the flattened array
array([1, 1, 3, 4])
>>> np.sort(a, axis=0) # sort along the first axis
array([[1, 1],
[3, 4]])

# .ardsort() 功能和.sort一致,但返回的是索引值。
>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])

Searching

Searching
argmax Returns the indices of the maximum values along an axis.
nanargmax Return the indices of the maximum values in the specified axis ignoring NaNs.
argmin Returns the indices of the minimum values along an axis.
nanargmin Return the indices of the minimum values in the specified axis ignoring NaNs.
argwhere Find the indices of array elements that are non-zero, grouped by element.
nonzero Return the indices of the elements that are non-zero.
flatnonzero Return indices that are non-zero in the flattened version of a.
where Return elements, either from x or y, depending on condition.
searchsorted Find indices where elements should be inserted to maintain order.
extract Return the elements of an array that satisfy some condition.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
### 常用函数

# .argmax 返回最大值的索引 .nanargmax 与其相似
# .argmin .nanargmin 同理
>>> a = np.arange(6).reshape(2,3)
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> np.argmax(a) # 默认会将数组进行flattened
5
>>> np.argmax(a, axis=0)
array([1, 1, 1])
>>> np.argmax(a, axis=1)
array([2, 2])

# .argwhere() 寻找非0的值的索引,等同于np.transpose(np.nonzero(a))
# 结合一些简单的对比运算,可以搜索更多的条件。
>>> x = np.arange(6).reshape(2,3)
>>> x
array([[0, 1, 2],
[3, 4, 5]])
>>> np.argwhere(x>1)
array([[0, 2],
[1, 0],
[1, 1],
[1, 2]])

# .nonzero() 寻找非0值索引
>>> x = np.array([[1,0,0], [0,2,0], [1,1,0]])
>>> x
array([[1, 0, 0],
[0, 2, 0],
[1, 1, 0]])
>>> np.nonzero(x)
(array([0, 1, 2, 2]), array([0, 1, 0, 1]))
>>> np.transpose(np.nonzero(x)) # 等同于.argwhere()
array([[0, 0],
[1, 1],
[2, 0],
[2, 1]])

Counting

counting
count_nonzero(a[, axis]) Counts the number of non-zero values in the array a
1
2
3
4
5
6
7
8
9
# .cout_nonzero() 返回非零值的个数。
>>> np.count_nonzero(np.eye(4)) # 默认axis=None,及对flattened数组进行操作。
4
>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]])
5
>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=0)
array([1, 1, 1, 1, 1])
>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=1)
array([2, 3])
-------------本文结束感谢您的阅读-------------