numpy中的常用统计函数

import numpy as np

# 统计函数

arr = np.arange(20).reshape(4,5)
print('arr:',arr)
# 求和函数sum
# axis不设置默认对整个数组进行求和计算
sum = np.sum(arr)
print(sum)
# axis=0  沿纵轴
sum_1 = np.sum(arr,axis=0)
print(sum_1)
# axis = 1 沿横轴
sum_2 = np.sum(arr,axis=1)
print(sum_2)
print(type(sum_2))

# cumsum()也是计算数组求和,不过是计算数组的累计和,包括每次累计的情况

cumsum_1 = np.cumsum(arr)
print(cumsum_1)
cumsum_2 = np.cumsum(arr,axis=0)
print(cumsum_2)
cumsum_3 = np.cumsum(arr,axis=1)
print(cumsum_3)

# mean()计算数组的均值
mean_1 = np.mean(arr)
print(mean_1)
mean_2 = np.mean(arr,axis=0)
print(mean_2)
mean_3 = np.mean(arr,axis=1)
print(mean_3)

# std() 计算数组的标准差
std_1 = np.std(arr)
print(std_1)
std_2 = np.std(arr,axis=0)
print(std_2)
std_3 = np.std(arr,axis=1)

# var()计算数组的方差
var_0 = np.var(arr)
var_1 = arr.var(axis=0)
var_2 = arr.var(axis=1)
print(var_0)
print(var_1)
print(var_2)

# min(),max()计算数组的最小值和最大值
print(np.min(arr))
print(np.min(arr,axis=0))
print(np.max(arr,axis=1))

# 重复和去重
arr2 = np.array([[22,33,22],[44,22,66]])
print('arr2',arr2)
print(np.unique(arr2))
print(np.tile(arr2,2))
print(np.repeat(arr2,2,axis=0))

# 转置和逆矩阵,视图,共轭转置
matr3 = np.mat('1,2,3,4;4,5,6,7;7,8,9,0')
print('arr3',matr3)
print(matr3.T)
print(matr3.I)
print(matr3.H)

# sort() argsort() 排序 排序下标

print(np.sort(arr2))
print(np.sort(arr2,axis=0))
print(np.argsort(arr2))

你可能感兴趣的:(numpy中的常用统计函数)