Python | numpy
Numpy: Scientific computing with Python
- a powerful N-dimensional array object
- sophisticated (broadcasting) functions
- tools for integrating C/C++ and Fortran code
- useful linear algebra
,
Fourier transform,
and random number capabilities
Structure^1
Useful funcions^2
- Array Creation:
arange
,array
,copy
,empty
,empty_like
,eye
,fromfile
,fromfunction
,identity
,linspace
,logspace
,mgrid
,ogrid
,ones
,ones_like
,r
,zeros
,zeros_like
- Conversions:
ndarray.astype
,atleast_1d
,atleast_2d
,atleast_3d
,mat
- Manipulations:
array_split
,column_stack
,concatenate
,diagonal
,dsplit
,dstack
,hsplit
,hstack
,ndarray.item
,newaxis
,ravel
,repeat
,reshape
,resize
,squeeze
,swapaxes
,take
,transpose
,vsplit
,vstack
- Questions:
all
,any
,nonzero
,where
- Ordering:
argmax
,argmin
,argsort
,max
,min
,ptp
,searchsorted
,sort
- Operations:
choose
,compress
,cumprod
,cumsum
,inner
,ndarray.fill
,imag
,prod
,put
,putmask
,real
,sum
- Basic Statistics:
cov
,mean
,std
,var
- Basic Linear Algebra:
cross
,dot
,outer
,linalg.svd
,vdot
Ordering
argmax^3
返回最大数的索引,参数axis默认0表示维度数。
二维
1
2
3
4
5
6import numpy as np
a=np.array([[1,5,5,2],
[9,6,2,8],
[3,7,9,1]])
print(np.argmax(a,axis=0)) #[1,2,2,1]
print(np.argmax(a,axis=1)) #[1,0,2]axis=0意为,返回的是每一列的最大索引(a[0][j],a[1][j]…)即逐行比较。
三维
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20import numpy as np
a = np.array([
[
[ 1, 5, 5, 2],
[ 9,-6, 2, 8],
[-3, 7,-9, 1]
],
[
[-1, 5,-5, 2],
[ 9, 6, 2, 8],
[ 3, 7, 9, 1]
]
])
print(np.argmax(a, axis=0))
#[[0 0 0 0]
#[0 1 0 0]
#[1 0 1 0]]
print(np.argmax(a, axis=1))
#[[1 2 0 1]
# [1 2 2 1]]应当明确对应轴在数组中的表现。三维时,axis=0,逐片比较;axis=1,逐行比较。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 琴韵居!