numpy常用函数

max

max(a, axis=None, out=None, keepdims=np._NoValue)
Return the maximum of an array or maximum along an axis.
返回array的最大值或者沿axis轴的最大值

axis参数详解

np.max(a,axis)表示在axis轴上取得的最值
axis的取值范围为[0,len(a.shape))
axis也可以是一个元组axis=(0,1)表示在第一个轴和第二个轴相交的空间上取得的最值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>> a=np.array(
[[[5., 0., 7., 0.],
[0., 0., 7., 0.],
[0., 0., 0., 3.]],

[[9., 5., 6., 0.],
[0., 6., 0., 0.],
[0., 0., 0., 4.]]])

>>> np.max(a,axis=0)
a的shape为(2,3,4),axis为0,表示沿着第一轴方向上的最值,可以得到一个(3,4)的array
array([[9., 5., 7., 0.],
[0., 6., 7., 0.],
[0., 0., 0., 4.]])

>>> np.max(a,axis=(0,1))
axis为(0,1),表示在第一轴上和第二轴相交空间上的最值,得到一个(4,)的array
array([9., 6., 7., 4.]

>>> np.max(a,axis=(0,1,2))
则等同于np.max(a)

where

where(condition, [x, y])
Return elements, either from x or y, depending on condition.If only condition is given, return condition.nonzero().
按照给定condition返回元素xy.如果只给定condition,则返回condition.nonzero().

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
>>> a=np.arange(9).reshape(3,3)
>>> np.where(a>3)
#输出为一个二维array,第一行表示a中的行坐标,第二行表示a中列坐标(可延伸至多维)
#a[1,1]=4, a[1,2]=5, a[2,0]=6....a[2,2]=8
(array([1, 1, 2, 2, 2], dtype=int64), array([1, 2, 0, 1, 2], dtype=int64))
>>> a>3
array([[False, False, False],
[False, True, True],
[ True, True, True]])
>>> (a>3).nonzero()
(array([1, 1, 2, 2, 2], dtype=int64), array([1, 2, 0, 1, 2], dtype=int64))

如果同时给定这三个参数,x和y可以为任意类型的数据,str、tuple、list、int
如果是多维数据其shape必须和condition有一定的联系(进行替换的时候不能有歧义)
>>> np.where(a>3,0,-1)
array([[-1, -1, -1],
[-1, 0, 0],
[ 0, 0, 0]])
符合条件的为0,不符合条件为-1

如果x和y为list,tuple
>>> x=np.arange(-1,-10,-1).reshape(3,3)
>>> np.where(a>3,x,(0))
array([[ 0, 0, 0],
[ 0, -5, -6],
[-7, -8, -9]])

>>> x=np.arange(-1,-4,-1)
>>> np.where(a>3,x,(0))
array([[ 0, 0, 0],
[ 0, -2, -3],
[-1, -2, -3]])

x和y是多维数据时需要有一定联系,方便进行选择,否则报错
>>> x=np.arange(-1,-7,-1)
>>> np.where(a>3,x,(0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (3,3) (6,) ()

cumsum

cumsum(a, axis=None, dtype=None, out=None)
Return the cumulative sum of the elements along a given axis.
返回给定轴上元素累计和.也可以称作累加函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> a=np.arange(6).reshape(2,3)
array([[0, 1, 2],
[3, 4, 5]])

>>> np.cumsum(a)
array([ 0, 1, 3, 6, 10, 15], dtype=int32)

>>> np.cumsum(a,axis=0)
array([[0, 1, 2],
[3, 5, 7]], dtype=int32)
>>> np.cumsum(a,axis=1)
array([[ 0, 1, 3],
[ 3, 7, 12]], dtype=int32)

cumprod

cumprod(a, axis=None, dtype=None, out=None):
Return the cumulative product of elements along a given axis.
返回给定轴上元素累计积.也可以称作累积函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> a=np.arange(1,7).reshape(2,3)
array([[1, 2, 3],
[4, 5, 6]])

>>> np.cumprod(a)
array([ 1, 2, 6, 24, 120, 720], dtype=int32)

>>> np.cumprod(a,axis=0)
array([[ 1, 2, 3],
[ 4, 10, 18]], dtype=int32)
>>> np.cumprod(a,axis=1)
array([[ 1, 2, 6],
[ 4, 20, 120]], dtype=int32)

unique

asarray

asarray(a, dtype=None, order=None)
Convert the input to an array.
将输入转化为array

1
2
3
4
5
>>> a=[1,2,3,4]
>>> a=np.asarray(a)
array([1, 2, 3, 4])
>>> np.asarray(a)
array('123', dtype='<U3')

errstate

np.errstate(**kwargs)
kwargs:{divide,over,under,invalid}
关键字参数。有效的关键字是可能的浮点异常。每个关键字都应该有一个字符串值来定义特定错误的处理。可能的值为{‘ignore’,’warn’,’raise’,’call’,’print’,’log’}。

transpose

transpose(a, axes=None)
Permute the dimensions of an array.
将矩阵a进行转置, 其中当a为一维时不起作用, 转置物理意义为切换坐标轴, 将x轴换为y轴, y轴换为x轴.

aa=np.arange(4).reshape(2,2)
aa
array([[0, 1],
[2, 3]])
bb =aa.transpose()
array([[0, 2],
[1, 3]])

#aa.transpose() <===> aa.transpose(1,0), 将0轴(数据下标)换为1轴, 将1轴换为0轴. 如a[0][1] 变换为 bb[1][0]

aa=np.arange(27).reshape(-1,3,3)
aa
array([[[ 0, 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]]])

bb=aa.transpose()
array([[[ 0, 9, 18],
[ 3, 12, 21],
[ 6, 15, 24]],

   [[ 1, 10, 19],
    [ 4, 13, 22],
    [ 7, 16, 25]],

   [[ 2, 11, 20],
    [ 5, 14, 23],
    [ 8, 17, 26]]])

#此时 aa.transpose() <===> aa.transpose(2,1,0), 将0轴换为2轴, 1轴不变, 2轴换为0轴. 如 aa[2][1][0] 变换为 bb[0][1][2]

bb= aa.transpose(2,0,1)
array([[[ 0, 3, 6],
[ 9, 12, 15],
[18, 21, 24]],

   [[ 1,  4,  7],
    [10, 13, 16],
    [19, 22, 25]],

   [[ 2,  5,  8],
    [11, 14, 17],
    [20, 23, 26]]])

#同理, aa[2][0][1] == bb[1][2][0]

clip

clip(a, a_min, a_max, out=None)
Clip (limit) the values in an array.

将a限制在a_min, a_max内,

a = np.arange(10)
np.clip(a, 1, 8)
array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8])
np.clip(a, 1, 8) <===> b=np.where(a<1, 1, a) 和 b =np.where(b>8,8,b)

设置精度

np.set_printoptions(precision=2)

np.squeeze

empty

随机初始化数组

genfromtxt

1
2
3
4
5
6
7
8
def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
skip_header=0, skip_footer=0, converters=None,
missing_values=None, filling_values=None, usecols=None,
names=None, excludelist=None,
deletechars=''.join(sorted(NameValidator.defaultdeletechars)),
replace_space='_', autostrip=False, case_sensitive=True,
defaultfmt="f%i", unpack=None, usemask=False, loose=True,
invalid_raise=True, max_rows=None, encoding='bytes'):

Load data from a text file, with missing values handled as specified.
Each line past the first skip_header lines is split at the delimiter
character, and characters following the comments character are discarded.

identity

np.identity(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

numpy.ndarray.flatten()