pandas.DataFrame.corr()是pandas中DataFrame对象的方法,用于计算DataFrame中列与列之间的相关性矩阵。该方法的返回值是一个相关性矩阵,矩阵的行与列分别对应着DataFrame中的列。
使用方法有如下参数:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)), columns=['a', 'b', 'c', 'd', 'e'])
print(df)
print(df.corr(method='pearson', min_periods=1))
输出:
a b c d e
0 0 3 7 3 6
1 9 6 9 1 7
2 5 7 5 0 7
3 9 7 3 2 3
4 2 2 5 1 8
a b c d e
a 1.000000 0.104828 -0.375118 -0.574102 -0.401042
b 0.104828 1.000000 0.351609 0.047441 -0.217675
c -0.375118 0.351609 1.000000 -0.174296 -0.044523
d -0.574102 0.047441 -0.174296 1.000000 0.343345
e -0.401042 -0.217675 -0.044523 0.343345 1.000000
import pandas as pd
df = pd.read_csv('data.csv')
print(df)
print(df.corr())
输出:
a b c d e
0 1 2 3 4 5
1 2 3 4 5 6
2 4 5 6 7 8
3 5 6 7 8 9
4 6 7 8 9 0
a b c d e
a 1.000000 0.998220 0.999967 0.998620 0.989072
b 0.998220 1.000000 0.998105 0.999579 0.986726
c 0.999967 0.998105 1.000000 0.997941 0.989962
d 0.998620 0.999579 0.997941 1.000000 0.986714
e 0.989072 0.986726 0.989962 0.986714 1.000000
import pandas as pd
df = pd.read_csv('housing.csv')
print(df.head())
correlation_matrix = df.corr(method='pearson')
print(correlation_matrix)
输出:
MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude Longitude MedHouseVal
0 8.3252 41.0 6.984127 1.023810 322 2.555556 37.88 -122.23 4.526
1 8.3014 21.0 6.238137 0.971880 2401 2.109842 37.86 -122.22 3.585
2 7.2574 52.0 8.288136 1.073446 496 2.802260 37.85 -122.24 3.521
3 5.6431 52.0 5.817352 1.073059 558 2.547945 37.85 -122.25 3.413
4 3.8462 52.0 6.281853 1.081081 565 2.181467 37.85 -122.25 3.422
MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude Longitude MedHouseVal
MedInc 1.000000 -0.119034 0.326895 -0.062040 0.004834 -0.023758 0.006421 -0.079809 0.688075
HouseAge -0.119034 1.000000 -0.153277 -0.077747 -0.296244 0.013295 0.011173 -0.108197 0.105623
AveRooms 0.326895 -0.153277 1.000000 0.847621 -0.072213 -0.004852 0.106389 -0.027540 0.151948
AveBedrms -0.062040 -0.077747 0.847621 1.000000 -0.066197 -0.006181 0.069721 -0.065843 -0.046701
Population 0.004834 -0.296244 -0.072213 -0.066197 1.000000 0.069863 -0.108785 0.099773 -0.024650
AveOccup -0.023758 0.013295 -0.004852 -0.006181 0.069863 1.000000 0.002366 0.002476 -0.023737
Latitude 0.006421 0.011173 0.106389 0.069721 -0.108785 0.002366 1.000000 -0.924664 0.144160
Longitude -0.079809 -0.108197 -0.027540 -0.065843 0.099773 0.002476 -0.924664 1.000000 -0.045967
MedHouseVal 0.688075 0.105623 0.151948 -0.046701 -0.024650 -0.023737 0.144160 -0.045967 1.000000
import pandas as pd
df = pd.read_csv('housing.csv')
df_city = df[['MedHouseVal', 'AveRooms']]
print(df_city.head())
correlation_matrix = df_city.corr(method='pearson')
print(correlation_matrix)
输出:
MedHouseVal AveRooms
0 4.526 6.984127
1 3.585 6.238137
2 3.521 8.288136
3 3.413 5.817352
4 3.422 6.281853
MedHouseVal AveRooms
MedHouseVal 1.000000 0.151948
AveRooms 0.151948 1.000000
以上两个实例展示了pandas.DataFrame.corr()的计算相关性矩阵的用法。同时,correlation_matrix的返回结果也提供了统计学上皮尔逊相关系数的介绍。该矩阵中的每个元素表示两个特征之间的相关性,值越大代表相关性越强,值越小或者为负数则代表相关性越弱或者负。
本文链接:http://task.lmcjl.com/news/17735.html