np.amin(), np.amax()
np.ptp()
np.percentile()
np.median()
np.mean()
np.average()
np.std(), np.var()
np.sum()
numpy.amin() 计算数组中的元素沿指定轴的最小值
numpy.amax() 计算数组中的元素沿指定轴的最大值
import numpy as np
a = np.array([[3,7,5],
[8,4,3],
[2,4,9]])
print (a)
print ('--------------------')
# 返回矩阵a中每一行最小的元素
print (np.amin(a,1))
print ('--------------------')
# 返回矩阵a中每一列最小的元素
print (np.amin(a,0))
print ('--------------------')
# 返回矩阵a所有元素的最大值
print (np.amax(a))
print ('--------------------')
# 返回矩阵a中每一列最大的元素
print (np.amax(a, axis = 0))
import numpy as np
a = np.array([[3,7,5],
[8,4,3],
[2,4,9]])
print (a)
print ('--------------------')
# 最大值与最小值之差9-2=7
print (np.ptp(a))
print ('--------------------')
# 每行最大值与最小值之差
print (np.ptp(a, axis = 1))
print ('--------------------')
# 每列最大值与最小值之差
print (np.ptp(a, axis = 0))
np.percentile(a, q, axis)
a:输入数组
q:要计算的百分位数,0~100之间
axis:沿指定轴计算百分位数
import numpy as np
a = np.array([[10, 7, 4],
[3, 2, 1]])
print (a)
print ('--------------------')
# 50% 的分位数,就是 a 里排序之后的中位数
print (np.percentile(a, 50))
print ('--------------------')
# axis 为 0,沿列
print (np.percentile(a, 50, axis=0))
print ('--------------------')
# axis 为 1,沿行
print (np.percentile(a, 50, axis=1))
print ('--------------------')
# 保持维度不变
print (np.percentile(a, 50, axis=1, keepdims=True))
import numpy as np
a = np.array([[30,65,70],
[80,95,10],
[50,90,60]])
print (a)
print ('--------------------')
# 计算所有元素中位数
print (np.median(a))
print ('--------------------')
# 沿列计算元素中位数
print (np.median(a, axis = 0))
print ('--------------------')
# 沿行计算元素中位数
print (np.median(a, axis = 1))
import numpy as np
a = np.array([[1,2,3],
[3,4,5],
[4,5,6]])
print (a)
print ('--------------------')
# 计算所有元素的算术平均值
print (np.mean(a))
print ('--------------------')
# 沿列计算算术平均值
print (np.mean(a, axis = 0))
print ('--------------------')
# 沿行计算算术平均值
print (np.mean(a, axis = 1))
import numpy as np
a = np.array([1,2,3,4])
print (a)
print ('--------------------')
# 未指定权重时,相当于mean
print (np.average(a))
print ('--------------------')
# 指定的权重wts
wts = np.array([4,3,2,1])
# 计算加权平均值
print (np.average(a,weights = wts))
print ('--------------------')
# 如果 returned 参数设为 true,则返回权重的和
print (np.average([1,2,3,4],weights = [4,3,2,1], returned = True))
多维数组情况下,可指定用于计算的轴
import numpy as np
a = np.arange(6).reshape(3,2)
print (a)
print ('--------------------')
# 权值
wt = np.array([3,5])
# 沿行
print (np.average(a, axis = 1, weights = wt))
print ('--------------------')
# 沿行,并返回权重的和
print (np.average(a, axis = 1, weights = wt, returned = True))
标准差是一组数据平均值分散程度的度量。标准差的平方=方差。
import numpy as np
#标准差
print (np.std([1,2,3,4]))
print ('--------------------')
#方差
print (np.var([1,2,3,4]))
arr = np.arange(10).reshape(5,2)
print(arr)
print('--------------')
print(np.sum(arr))
print('--------------')
print(np.sum(arr, axis=0))
print('--------------')
print(np.sum(arr, axis=1))