# numpy.amin(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,# where=np._NoValue]) Return the minimum of an array or minimum along an axis.
# 【例】计算最小值import numpy as np
x=np.array([[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]])
y=np.amin(x)print(y)#axis=0,行,axis=1,列
y=np.amin(x,axis=0)print(y)
y=np.amin(x,axis=1)print(y)
11
[11 12 13 14 15]
[11 16 21 26 31]
2.计算最大值
# 参数'''numpy.amax(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
where=np._NoValue]) Return the maximum of an array or maximum along an axis.'''
'numpy.amax(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,\nwhere=np._NoValue]) Return the maximum of an array or maximum along an axis.'
x = np.array([[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]])
y=np.amax(x)print(y)
y=np.amax(x,axis=0)print(y)
y=np.amax(x,axis=1)print(y)
35
[31 32 33 34 35]
[15 20 25 30 35]
3.计算极差
'''numpy.ptp(a, axis=None, out=None, keepdims=np._NoValue) Range of values (maximum -
minimum) along an axis. The name of the function comes from the acronym for 'peak to
peak'.
'''
np.random.seed(20200623)
x=np.random.randint(0,20,size=[4,5])print(x)print(np.ptp(x))#行极差print(np.ptp(x,axis=0))#列极差print(np.ptp(x,axis=1))
'''numpy.percentile(a, q, axis=None, out=None, overwrite_input=False,
interpolation='linear', keepdims=False) Compute the q-th percentile of the data along
the specified axis. Returns the q-th percentile(s) of the array elements.
a:array,用来算分位数的对象,可以是多维的数组。
q:介于0-100的float,用来计算是几分位的参数,如四分之一位就是25,如要算两个位置
的数就[25,75]。
axis:坐标轴的方向,一维的就不用考虑了,多维的就用这个调整计算的维度方向,取值范
围0/1。'''
np.random.seed(20200623)
x=np.random.randint(0,20,size=[4,5])print(x)print(np.percentile(x,[25,50]))print(np.percentile(x,[25,50],axis=0))print(np.percentile(x,[25,50],axis=1))
二、均值与方差
1.计算中位数
'''numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False) Compute
the median along the specified axis. Returns the median of the array elements.'''# 【例】计算中位数
np.random.randint(0,20,size=[4,5])print(x)print(np.percentile(x,50))print(np.median(x))print(np.percentile(x,50,axis=0))print(np.median(x,axis=0))print(np.percentile(x,50,axis=1))print(np.median(x,axis=1))
'''numpy.mean(a[, axis=None, dtype=None, out=None, keepdims=np._NoValue)]) Compute the
arithmetic mean along the specified axis.
'''import numpy as np
# 例】计算平均值(沿轴的元素的总和除以元素的数量)。
x=np.array([[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]])
y=np.mean(x)print(y)
y=np.mean(x,axis=0)print(y)
y=np.mean(x,axis=1)print(y)
'''numpy.std(a[, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue]) Compute
the standard deviation along the specified axis.
'''# 标准差是一组数据平均值分散程度的一种度量,是方差的算术平方根。
y=np.std(x)print(y)
y=np.sqrt(np.var(x))print(y)
y=np.std(x,axis=0)print(y)
y=np.std(x,axis=1)print(y)
三、相关
1.计算协方差矩阵
'''numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,aweights=None)
Estimate a covariance matrix, given data and weights.'''# 【例】计算协方差矩阵
x =[1,2,3,4,6]
y =[0,2,5,6,7]print(np.cov(x))print(np.cov(y))print(np.cov(x,y))
'''
numpy.digitize(x, bins, right=False) Return the indices of the bins to which each value
in input array belongs.
x:numpy数组
bins:一维单调数组,必须是升序或者降序
right:间隔是否包含最右
返回值:x在bins中的位置。
'''# numpy.digitize 返回输入数组中每个值所属的bin的索引
x=np.array([0.2,6.4,3.0,1.6])
bins=np.array([0.0,1.0,2.5,4.0,10.0])
inds=np.digitize(x,bins)
inds