使用Pandas Data Frame运行OLS回归

Mic*_*ael 107 python regression pandas scikit-learn statsmodels

我有一个pandas数据框,我希望能够从B列和C列中的值预测A列的值.这是一个玩具示例:

import pandas as pd
df = pd.DataFrame({"A": [10,20,30,40,50], 
                   "B": [20, 30, 10, 40, 50], 
                   "C": [32, 234, 23, 23, 42523]})
Run Code Online (Sandbox Code Playgroud)

理想情况下,我会有类似的东西ols(A ~ B + C, data = df),但当我从算法库中查看示例时,scikit-learn它似乎将数据提供给模型,其中包含行而不是列.这将要求我将数据重新格式化为列表中的列表,这似乎首先打败了使用pandas的目的.在pandas数据框中对数据运行OLS回归(或更普遍的机器学习算法)的最pythonic方法是什么?

DSM*_*DSM 137

我认为你几乎可以完全按照你认为理想的做法,使用statsmodels包,它是'版本0.20.0 pandas之前pandas的可选依赖项之一(它用于一些事情)pandas.stats.

>>> import pandas as pd
>>> import statsmodels.formula.api as sm
>>> df = pd.DataFrame({"A": [10,20,30,40,50], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]})
>>> result = sm.ols(formula="A ~ B + C", data=df).fit()
>>> print(result.params)
Intercept    14.952480
B             0.401182
C             0.000352
dtype: float64
>>> print(result.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      A   R-squared:                       0.579
Model:                            OLS   Adj. R-squared:                  0.158
Method:                 Least Squares   F-statistic:                     1.375
Date:                Thu, 14 Nov 2013   Prob (F-statistic):              0.421
Time:                        20:04:30   Log-Likelihood:                -18.178
No. Observations:                   5   AIC:                             42.36
Df Residuals:                       2   BIC:                             41.19
Df Model:                           2                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept     14.9525     17.764      0.842      0.489       -61.481    91.386
B              0.4012      0.650      0.617      0.600        -2.394     3.197
C              0.0004      0.001      0.650      0.583        -0.002     0.003
==============================================================================
Omnibus:                          nan   Durbin-Watson:                   1.061
Prob(Omnibus):                    nan   Jarque-Bera (JB):                0.498
Skew:                          -0.123   Prob(JB):                        0.780
Kurtosis:                       1.474   Cond. No.                     5.21e+04
==============================================================================

Warnings:
[1] The condition number is large, 5.21e+04. This might indicate that there are
strong multicollinearity or other numerical problems.
Run Code Online (Sandbox Code Playgroud)

  • 请注意,正确的关键字是`formula`,我不小心输入了`formula`并得到了奇怪的错误:`TypeError:from_formula()至少需要3个参数(给定2个)` (2认同)
  • @ a.powell OP的代码适用于Python 2.我认为你需要做的唯一改变就是在括号中加上圆括号来打印:`print(result.params)`和`print(result.summary())` (2认同)

Rom*_*kar 67

注意: pandas.stats 已被删除 0.20.0


可以这样做pandas.stats.ols:

>>> from pandas.stats.api import ols
>>> df = pd.DataFrame({"A": [10,20,30,40,50], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]})
>>> res = ols(y=df['A'], x=df[['B','C']])
>>> res
-------------------------Summary of Regression Analysis-------------------------

Formula: Y ~ <B> + <C> + <intercept>

Number of Observations:         5
Number of Degrees of Freedom:   3

R-squared:         0.5789
Adj R-squared:     0.1577

Rmse:             14.5108

F-stat (2, 2):     1.3746, p-value:     0.4211

Degrees of Freedom: model 2, resid 2

-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
             B     0.4012     0.6497       0.62     0.5999    -0.8723     1.6746
             C     0.0004     0.0005       0.65     0.5826    -0.0007     0.0014
     intercept    14.9525    17.7643       0.84     0.4886   -19.8655    49.7705
---------------------------------End of Summary---------------------------------
Run Code Online (Sandbox Code Playgroud)

请注意,您需要statsmodels安装软件包,它由pandas.stats.ols函数内部使用.

  • 请注意,这将在未来的熊猫版本中弃用! (13认同)
  • 为什么这样做?我生动地希望这个功能能够存活下来!它非常实用且快捷! (4认同)
  • `pandas.stats.ols模块已弃用,将在以后的版本中删除.我们引用像statsmodels这样的外部包,请参阅这里的一些例子:http:// www.statsmodels.org/stable/regression.html` (2认同)
  • @DestaHaileselassieHagos.这可能是由于"缺少拦截"的问题.等效`R`包的设计者通过删除平均值的调整来调整:https://stats.stackexchange.com/a/36068/64552..其他建议:`你可以使用sm.add_constant为exog数组添加一个截距并使用一个dict:`reg = ols("y~x",data = dict(y = y,x = x)). ()` (2认同)
  • 他们删除了"pandas.stats"是一个悲伤的日子 (2认同)

3no*_*vak 28

我不知道这是否是新的sklearn还是pandas,但我能直接传递数据帧sklearn没有数据帧转换为numpy的阵列或任何其它数据类型.

from sklearn import linear_model

reg = linear_model.LinearRegression()
reg.fit(df[['B', 'C']], df['A'])

>>> reg.coef_
array([  4.01182386e-01,   3.51587361e-04])
Run Code Online (Sandbox Code Playgroud)

  • 来自 OP 的小改动 - 但在将 `.values.reshape(-1, 1)` 附加到数据框列之后,我发现这个特定的答案非常有帮助。例如:`x_data = df['x_data'].values.reshape(-1, 1)`并将`x_data`(以及类似创建的`y_data`)np数组传递到`.fit()`方法中。 (3认同)

Fre*_*Foo 16

这将要求我将数据重新格式化为列表中的列表,这似乎首先打败了使用pandas的目的.

不,它只是转换为NumPy数组:

>>> data = np.asarray(df)
Run Code Online (Sandbox Code Playgroud)

这需要花费一些时间,因为它只是为您的数据创建一个视图.然后把它喂给scikit-learn:

>>> from sklearn.linear_model import LinearRegression
>>> lr = LinearRegression()
>>> X, y = data[:, 1:], data[:, 0]
>>> lr.fit(X, y)
LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
>>> lr.coef_
array([  4.01182386e-01,   3.51587361e-04])
>>> lr.intercept_
14.952479503953672
Run Code Online (Sandbox Code Playgroud)

  • 我不得不做`np.matrix(np.asarray(df))`,因为sklearn期望一个垂直向量,而numpy数组,一旦你将它们从数组中切割出来,就像水平向量一样,这在大多数时候都是很好的. (3认同)
  • 有没有办法用Pandas DataFrame直接喂Scikit-Learn? (2认同)

ves*_*and 11

Statsmodels 可以构建一个OLS 模型,其中列引用直接指向Pandas 数据框

简短而甜蜜:

model = sm.OLS(df[y], df[x]).fit()


代码细节和回归总结:

# imports
import pandas as pd
import statsmodels.api as sm
import numpy as np

# data
np.random.seed(123)
df = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=list('ABC'))

# assign dependent and independent / explanatory variables
variables = list(df.columns)
y = 'A'
x = [var for var in variables if var not in y ]

# Ordinary least squares regression
model_Simple = sm.OLS(df[y], df[x]).fit()

# Add a constant term like so:
model = sm.OLS(df[y], sm.add_constant(df[x])).fit()

model.summary()
Run Code Online (Sandbox Code Playgroud)

输出:

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      A   R-squared:                       0.019
Model:                            OLS   Adj. R-squared:                 -0.001
Method:                 Least Squares   F-statistic:                    0.9409
Date:                Thu, 14 Feb 2019   Prob (F-statistic):              0.394
Time:                        08:35:04   Log-Likelihood:                -484.49
No. Observations:                 100   AIC:                             975.0
Df Residuals:                      97   BIC:                             982.8
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         43.4801      8.809      4.936      0.000      25.996      60.964
B              0.1241      0.105      1.188      0.238      -0.083       0.332
C             -0.0752      0.110     -0.681      0.497      -0.294       0.144
==============================================================================
Omnibus:                       50.990   Durbin-Watson:                   2.013
Prob(Omnibus):                  0.000   Jarque-Bera (JB):                6.905
Skew:                           0.032   Prob(JB):                       0.0317
Kurtosis:                       1.714   Cond. No.                         231.
==============================================================================
Run Code Online (Sandbox Code Playgroud)

如何直接得到 R 平方、系数和 p 值:

# commands:
model.params
model.pvalues
model.rsquared

# demo:
In[1]: 
model.params
Out[1]:
const    43.480106
B         0.124130
C        -0.075156
dtype: float64

In[2]: 
model.pvalues
Out[2]: 
const    0.000003
B        0.237924
C        0.497400
dtype: float64

Out[3]:
model.rsquared
Out[2]:
0.0190
Run Code Online (Sandbox Code Playgroud)