一、基本统计分析
1.Numpy数组
sum、prod 求和、求积
cumsum、cumprod 累加、累积
mean、median 均值、中位数
percentile 分位数
std、var 标准差、方差
min、max 极大值、极小值
corrcoel、cov 相关系数、协方差
unique 唯一值 #提取不重复的项
例:
import numpy as np
arr = np.random.randn(4, 5) #生成正态分布的数据
np.mean(arr) #求整体均值
np.mean(arr, axis=1) #求每一行的均值
np.mean(arr, axis=0) #求每一列的均值
np.median(arr) #求中位数,在一个等差数列或一个正态分布数列中,中位数就等于算术平均数。
np.median(arr, axis=1) #求每一行的中位数
np.median(arr, axis=0) #求每一列的中位数
np.cumsum(arr, axis=1) #求每一行的累加数列
np.cumsum(arr, axis=0) #求每一列的累加数列
np.unique(arr)
brr = np.random.randn(4,5)
np.corrcoef(arr, brr)#相关系数
np.cov(arr, brr)#协方差
2.Pandas
(1)值计数:出现几次
import pandas as pd
s = [3,3,1,2,4,3,4,6,5,6]
pd.value_counts(s)
运行结果:
3 3
4 2
6 2
1 1
2 1
5 1
dtype: int64
(2)在值计数的情况下求众数
import pandas as pd
s = [3,3,1,2,4,3,4,6,5,6]
ss = pd.value_counts(s)
ss
np.argmax(ss)#数组,索引值从0开始
运行结果:
3 3
4 2
6 2
1 1
2 1
5 1
dtype: int64
0
二、数据的标准化、离散化
1、数据的标准化

arr = np.random.randn(4, 5) #生成正态分布的数据
import pandas as pd
data = pd.DataFrame(arr)
data
运行结果:
0 1 2 3 4
0 0.589185 0.800277 -0.284719 0.001542 1.140371
1 0.326136 -0.524810 0.326337 -0.491992 0.699328
2 1.866762 0.135660 0.917257 1.051673 0.580966
3 1.265739 -0.052521 -1.403901 -0.297061 -1.299990
(1)Min-Max方法 用的Numpy包
data1 = (data - np.min(data)) / (np.max(data) - np.min(data)) #Max-Min的标准化 默认是每一行的最小值、最大值
data1
(2)Min-Max方法 用的Pandas包
data2 = (data - data.min()) / (data.max() - data.min())
运行结果:两个一样
(1)Z-score方法 没用无偏估计
data3 = (data - np.mean(data))/np.std(data)
data3
(2)用无偏估计,用这个方法计算
data4 = (data - data.mean())/data.std()
data4
运行结果:两者答案不同,选择用无偏估计,用Pandas这个方法计算正确
log函数转换:
j = np.log10(data.abs().max()) #abs()为取绝对值
data3 = data/10**j #log函数转换
data3
2.数据的离散化(分箱法)
(1)等宽离散法(等宽分箱)
等宽分箱:每个分箱中取值范围一致
sample = pd.DataFrame({'normal': np.random.randn(10)}) #产生10个符合正态分布的随机数
sample
运行结果:
normal
0 -0.401254
1 -0.805417
2 -1.538366
3 1.417620
4 -0.553882
5 0.797224
6 0.598533
7 1.430762
8 -2.503595
9 1.168223
pd.cut(sample.normal, 5) #分五份
运行结果:
0 (-0.93, -0.143]
1 (-0.93, -0.143]
2 (-1.717, -0.93]
3 (0.644, 1.431]
4 (-0.93, -0.143]
5 (0.644, 1.431]
6 (-0.143, 0.644]
7 (0.644, 1.431]
8 (-2.508, -1.717]
9 (0.644, 1.431]
Name: normal, dtype: category
Categories (5, interval[float64, right]): [(-2.508, -1.717] < (-1.717, -0.93] < (-0.93, -0.143] < (-0.143, 0.644] < (0.644, 1.431]]
pd.cut(sample.normal, bins=5, labels={1, 2, 3, 4, 5})
运行结果:
0 3
1 3
2 2
3 5
4 3
5 5
6 4
7 5
8 1
9 5
Name: normal, dtype: category
Categories (5, int64): [1 < 2 < 3 < 4 < 5]
(2)等频离散化(等深分箱)
等深分箱:每个分箱宽度可能不一致,但是频数几乎相同
ample.normal.quantile([0, 0.5, 1]) #分两箱,找出分位数
运行结果:
0.0 -2.503595
0.5 0.098640
1.0 1.430762
Name: normal, dtype: float64
pd.cut(sample.normal, bins=sample.normal.quantile([0,0.5,1]), include_lowest=True) #完成分箱
运算结果:
0 (-2.505, 0.0986]
1 (-2.505, 0.0986]
2 (-2.505, 0.0986]
3 (0.0986, 1.431]
4 (-2.505, 0.0986]
5 (0.0986, 1.431]
6 (0.0986, 1.431]
7 (0.0986, 1.431]
8 (-2.505, 0.0986]
9 (0.0986, 1.431]
Name: normal, dtype: category
Categories (2, interval[float64, right]): [(-2.505, 0.0986] < (0.0986, 1.431]]
pd.cut(sample.normal, bins=sample.normal.quantile([0, 0.5, 1]),include_lowest = True, labels = ['bad', 'good']) #也可以加入标签
运算结果:
0 bad
1 bad
2 bad
3 good
4 bad
5 good
6 good
7 good
8 bad
9 good
Name: normal, dtype: category
Categories (2, object): ['bad' < 'good']
三、金融中线性问题
1.

2.

3.

4.

5.

网友评论