Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多指标df中的线性回归_Python_Pandas_Linear Regression_Scipy.stats - Fatal编程技术网

Python 多指标df中的线性回归

Python 多指标df中的线性回归,python,pandas,linear-regression,scipy.stats,Python,Pandas,Linear Regression,Scipy.stats,给定下面的多索引多列数据帧,我想对该数据帧的每个块应用线性回归,例如,对于每个站号,我想在LST和Value之间运行回归。df如下所示: Latitude Longitude LST Elevation Value Station_Number Date RSM00025356 2019-0

给定下面的多索引多列数据帧,我想对该数据帧的每个块应用线性回归,例如,对于每个站号,我想在LST和Value之间运行回归。df如下所示:

                           Latitude  Longitude        LST  Elevation      Value
Station_Number Date                                                            
RSM00025356    2019-01-01   66.3797     173.33 -31.655008       78.0 -28.733333
               2019-02-02   66.3797     173.33 -17.215009       78.0 -17.900000
               2019-02-10   66.3797     173.33 -31.180006       78.0 -19.500000
               2019-02-26   66.3797     173.33 -19.275007       78.0  -6.266667
               2019-04-23   66.3797     173.33 -12.905004       78.0  -4.916667

还有很多电视台要找。理想情况下,输出将只是每个站号的回归结果

您可以使用
groupby
分割数据帧,然后在组内运行每个回归。您可以将结果存储在字典中,其中键为“Station_Number”。我将使用statsmodels进行回归,但是有许多可能的库,这取决于您对标准错误和推断的关注程度

import statsmodels.formula.api as smf

d = {}
for station,gp in df.groupby('Station_Number'):
    mod = smf.ols(formula='LST ~ Value', data=gp)
    d[station] = mod.fit()

回归结果:

d['RSM00025356'].params
#Intercept   -11.676331
#Value         0.696465
#dtype: float64

d['RSM00025356'].summary()

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                    LST   R-squared:                       0.660
Model:                            OLS   Adj. R-squared:                  0.547
Method:                 Least Squares   F-statistic:                     5.831
Date:                Fri, 28 May 2021   Prob (F-statistic):             0.0946
Time:                        11:17:51   Log-Likelihood:                -14.543
No. Observations:                   5   AIC:                             33.09
Df Residuals:                       3   BIC:                             32.30
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept    -11.6763      5.143     -2.270      0.108     -28.043       4.690
Value          0.6965      0.288      2.415      0.095      -0.221       1.614
==============================================================================
Omnibus:                          nan   Durbin-Watson:                   2.536
Prob(Omnibus):                    nan   Jarque-Bera (JB):                0.299
Skew:                           0.233   Prob(JB):                        0.861
Kurtosis:                       1.895   Cond. No.                         35.9
==============================================================================

工作了,非常感谢!