如何在python中添加回归函数,或根据给定的系数创建新的回归函数? [英] How to add regression functions in python, or create a new regression function from given coefficients?

查看:66
本文介绍了如何在python中添加回归函数,或根据给定的系数创建新的回归函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回归函数,一个数据点的 g1(x)= 5x-1 .我还有另一个回归函数, g2(x)= 3x + 4 .

I have one regression function, g1(x) = 5x - 1 for one data point. I have another regression function, g2(x) = 3x + 4.

我想添加这两个模型以创建最终的回归模型 G(x).

I want to add these two models to create a final regression model, G(x).

这意味着:

G(x) = g1(x) + g2(x) 
=> 5x - 1 + 3x + 4
=> 8x +3 

我的问题是,如何在python中完成此操作?如果我的数据集是 X ,则我使用的是这样的statsmodels:

My question is, how can this be done in python? If my dataset is X, I'm using statsmodels like this:

import statsmodels.api as sm
from statsmodels.sandbox.regression.predstd import wls_prediction_std
import numpy as np

mod_wls = sm.WLS(y, X)
res_wls = mod_wls.fit()
print res_wls.params

这给了我拟合数据 X 的回归函数的系数.要添加这些函数,我可以轻松地获取每个系数,并对它们求和以得出新回归函数(例如G(x))的系数.但是,既然我有了自己的系数,如何将它们转换成回归函数,并用它们来预测新数据?因为据我所知,必须先将模型拟合"到数据中,然后才能将其用于预测.

And that gives me the coefficients for the regression function that fits the data X. To add the functions, I can easily grab the coefficients for each, and sum them up to get the coefficients for a new regression function such as G(x). But now that I've got my own coefficients, how can I convert them into a regression function and use them to predict a new data? Because as far as I know, models have to be "fitted" to data before they can be used for prediction.

或者有什么方法可以直接添加回归函数?我将在算法中迭代添加函数.

Or is there any way to directly add regression functions? I'm going to be adding functions iteratively in my algorithm.

推荐答案

此模型生成的预测应该准确

The prediction generated by this model should be exactly

np.dot(X_test, res_wls.params)

因此,如果您想对多个模型求和,例如

Thus, if you want to sum several models, e.g.

summed_params = np.array([res_wls.params for res_wls in all_my_res_wls]).sum(axis=0)

您的预测应该是

np.dot(X_test, summed_params)

在这种情况下,无需使用估算器的内置函数.

In this case there would be no need to use the built-in functions of the estimator.

这篇关于如何在python中添加回归函数,或根据给定的系数创建新的回归函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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