如何使用python在df中找到2列之间的依赖关系

时间:2016-06-20 22:31:01

标签: python pandas matplotlib

我有数据

city    inc     pop
New-York  29343,00  8683,00
Moscow  25896,00    17496,00
Boston   21785,00   15063,00
Berlin  20000,00    70453,00
London  44057,00    57398,00
Rome  24000,00  104831,00

我需要了解incpop的依赖程度。 我尝试绘制图df.plot(x='inc', y='pop'),但我的图表很糟糕,因为我有200个值。 我怎样才能做得更好?

3 个答案:

答案 0 :(得分:1)

默认情况下,绘图kind参数为line。对于探索性数据分析,通常最好从散点图开始。

df.plot(x='inc', y='pop', kind='scatter')

答案 1 :(得分:1)

如上所述,您可以通过以下网址获取correlation

df['inc'].corr(df['pop'])
-0.0279628856838

如果您想要线性回归,可以使用statsmodels.ols

import statsmodels.api as sm
df['const'] = 1
model = sm.OLS(df['inc'], df[['const', 'pop']])
results = model.fit()
results.summary()

产生:

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                    inc   R-squared:                       0.001
Model:                            OLS   Adj. R-squared:                 -0.249
Method:                 Least Squares   F-statistic:                  0.003130
Date:                Tue, 21 Jun 2016   Prob (F-statistic):              0.958
Time:                        07:29:55   Log-Likelihood:                -62.413
No. Observations:                   6   AIC:                             128.8
Df Residuals:                       4   BIC:                             128.4
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
const        2.78e+04   6548.318      4.246      0.013      9623.205   4.6e+04
pop           -0.0064      0.114     -0.056      0.958        -0.322     0.310
==============================================================================
Omnibus:                          nan   Durbin-Watson:                   2.613
Prob(Omnibus):                    nan   Jarque-Bera (JB):                1.721
Skew:                           1.302   Prob(JB):                        0.423
Kurtosis:                       3.330   Cond. No.                     9.46e+04
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

最后,您可以将趋势线添加到散点图中:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
ax = df.plot.scatter('inc', 'pop')
z = np.polyfit(df['inc'], df['pop'], 1)
p = np.poly1d(z)
df['trend'] = p(df.inc)
df.plot(x='inc', y='trend', ax=ax)
plt.show()

得到(看起来很奇怪,因为我只使用你的5个数据点):

enter image description here

并得到最终的线方程:

"y=%.6fx+(%.6f)" % (z[0], z[1])
y=-0.122779x+(49032.076720)

答案 2 :(得分:0)

您可以做各种事情以使其更具可读性。我想只用这一行就可以得到一个线条图。您可以先将其更改为分散。

如果您尝试显示某些相关性,则可以叠加回归线。

如果这太乱了,你可以玩颜色,例如让点浅灰色,但回归线为红色。

查看http://pandas.pydata.org/pandas-docs/stable/visualization.html获取灵感。使用GeomScatter详细查看示例 - 账单/提示似乎与您可以做的很接近。