Travel Tips
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
利用Python进行相关性分析
Python特征分析- 相关性分析
# 相关性分析
介绍:分析连续变量之间的线性相关程度的强弱
方法:图示初判 / Pearson相关系数(皮尔逊相关系数) / Sperman秩相关系数(斯皮尔曼相关系数)
## 引入库
```python import numpy as np import pandas as pd import matplotlib.pyplot as plt from scipy import stats %matplotlib inline ```
## 图示初判
### 变量之间的线性相关性
```python data1 = pd.Series(np.random.rand(50)*100).sort_values() data2 = pd.Series(np.random.rand(50)*50).sort_values() data3 = pd.Series(np.random.rand(50)*500).sort_values(ascending = False) # 创建三个数据:data1为0-100的随机数并从小到大排列,data2为0-50的随机数并从小到大排列,data3为0-500的随机数并从大到小排列, fig = plt.figure(figsize = (10,4)) ax1 = fig.add_subplot(1,2,1) ax1.scatter(data1, data2) plt.grid() # 正线性相关 ax2 = fig.add_subplot(1,2,2) ax2.scatter(data1, data3) plt.grid() # 负线性相关 ```
### 散点图矩阵初判多变量间关系
```python data = pd.DataFrame(np.random.randn(200,4)*100, columns = ['A','B','C','D']) pd.plotting.scatter_matrix(data,figsize=(8,8), c = 'k', marker = '+', diagonal='hist', alpha = 0.8, range_padding=0.1) data.head() ```
## Pearson相关系数
### 创建样本数据
```python data1 = pd.Series(np.random.rand(100)*100).sort_values() data2 = pd.Series(np.random.rand(100)*50).sort_values() data = pd.DataFrame({'value1':data1.values, 'value2':data2.values}) print(data.head()) print('------') # 创建样本数据 ```
### 正态性检验 → pvalue >0.05
```python u1,u2 = data['value1'].mean(),data['value2'].mean() # 计算均值 std1,std2 = data['value1'].std(),data['value2'].std() # 计算标准差 print('value1正态性检验:\n',stats.kstest(data['value1'], 'norm', (u1, std1))) print('value2正态性检验:\n',stats.kstest(data['value2'], 'norm', (u2, std2))) print('------') # 正态性检验 → pvalue >0.05 ```
### 制作Pearson相关系数求值表
```python data['(x-u1)*(y-u2)'] = (data['value1'] - u1) * (data['value2'] - u2) data['(x-u1)**2'] = (data['value1'] - u1)**2 data['(y-u2)**2'] = (data['value2'] - u2)**2 print(data.head()) print('------') # 制作Pearson相关系数求值表 ```
### 求出r
```python r = data['(x-u1)*(y-u2)'].sum() / (np.sqrt(data['(x-u1)**2'].sum() * data['(y-u2)**2'].sum())) print('Pearson相关系数为:%.4f' % r) # 求出r # |r| > 0.8 → 高度线性相关 ```
```python Pearson相关系数为:0.9969 ```
## Pearson相关系数 - 算法
```python data1 = pd.Series(np.random.rand(100)*100).sort_values() data2 = pd.Series(np.random.rand(100)*50).sort_values() data = pd.DataFrame({'value1':data1.values, 'value2':data2.values}) print(data.head()) print('------') # 创建样本数据 data.corr() # pandas相关性方法:data.corr(method='pearson', min_periods=1) → 直接给出数据字段的相关系数矩阵 # method默认pearson ```
## Sperman秩相关系数
### 创建样本数据
```python data = pd.DataFrame({'智商':[106,86,100,101,99,103,97,113,112,110], '每周看电视小时数':[7,0,27,50,28,29,20,12,6,17]}) print(data) print('------') # 创建样本数据 ```
### 重新排序、index
```python data.sort_values('智商', inplace=True) data['range1'] = np.arange(1,len(data)+1) data.sort_values('每周看电视小时数', inplace=True) data['range2'] = np.arange(1,len(data)+1) print(data) print('------') # “智商”、“每周看电视小时数”重新按照从小到大排序,并设定秩次index ```
### 求出rs
```python data['d'] = data['range1'] - data['range2'] data['d2'] = data['d']**2 print(data) print('------') # 求出di,di2 n = len(data) rs = 1 - 6 * (data['d2'].sum()) / (n * (n**2 - 1)) print('Pearson相关系数为:%.4f' % rs) # 求出rs ```
## Pearson相关系数 - 算法
```python data = pd.DataFrame({'智商':[106,86,100,101,99,103,97,113,112,110], '每周看电视小时数':[7,0,27,50,28,29,20,12,6,17]}) print(data) print('------') # 创建样本数据 data.corr(method='spearman') # pandas相关性方法:data.corr(method='pearson', min_periods=1) → 直接给出数据字段的相关系数矩阵 # method默认pearson ```
Sed ac lorem felis. Ut in odio lorem. Quisque magna dui, maximus ut commodo sed, vestibulum ac nibh. Aenean a tortor in sem tempus auctor
December 4, 2020 at 3:12 pm
Sed ac lorem felis. Ut in odio lorem. Quisque magna dui, maximus ut commodo sed, vestibulum ac nibh. Aenean a tortor in sem tempus auctor
December 4, 2020 at 3:12 pm
Donec in ullamcorper quam. Aenean vel nibh eu magna gravida fermentum. Praesent eget nisi pulvinar, sollicitudin eros vitae, tristique odio.
December 4, 2020 at 3:12 pm
我是 s enim interduante quis metus. Duis porta ornare nulla ut bibendum
Rosie
6 minutes ago