python pandas中的分组回归 [英] Regression by group in python pandas

查看:1456
本文介绍了python pandas中的分组回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个有关python pandas回归分析的快速问题. 因此,假设我具有以下数据集:

I want to ask a quick question related to regression analysis in python pandas. So, assume that I have the following datasets:

 Group      Y        X
  1         10       6
  1         5        4
  1         3        1
  2         4        6
  2         2        4
  2         3        9

我的目标是进行回归; Y是因变量,X是自变量.问题是我想按组运行此回归并在新数据集中打印系数.因此,结果应类似于:

My aim is to run regression; Y is dependent and X is independent variable. The issue is I want to run this regression by Group and print the coefficients in a new data set. So, the results should be like:

 Group   Coefficient
   1        0.25 (lets assume that coefficient is 0.25)
   2        0.30

我希望我能解释我的问题. 预先非常感谢您的帮助.

I hope I can explain my question. Many thanks in advance for your help.

推荐答案

我不确定所需的回归类型,但这是您执行OLS(普通最小二乘)的方法:

I am not sure about the type of regression you need, but this is how you do an OLS (Ordinary least squares):

import pandas as pd
import statsmodels.api as sm 

def regress(data, yvar, xvars):
    Y = data[yvar]
    X = data[xvars]
    X['intercept'] = 1.
    result = sm.OLS(Y, X).fit()
    return result.params


#This is what you need
df.groupby('Group').apply(regress, 'Y', ['X'])

您可以定义回归函数并将参数传递给它.

You can define your regression function and pass parameters to it as mentioned.

这篇关于python pandas中的分组回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆