如何从statsmodels WLS回归2D参数测试得到的prediction [英] How to get the prediction of test from 2D parameters of WLS regression in statsmodels

查看:3223
本文介绍了如何从statsmodels WLS回归2D参数测试得到的prediction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我逐步向上<一个参数href=\"http://statsmodels.sourceforge.net/devel/generated/statsmodels.regression.linear_model.WLS.html\"相对=nofollow> WLS回归函数使用statsmodels。

我有我声明了一个10x3数据点¯x是这样的:

  X = np.array([1,2,3],[1,2,3],[4,5,6],[1,2,3] [4,5,6],[1,2,3],[1,2,3],[4,5,6],[4,5,6],[1,2,3]])

这是我的数据集,我有一个10X2 endog 载体,看起来像这样:

  Z =
[3.90311860e-322 2.00000000e + 000]
 [0.00000000e + 000 + 2.00000000e 000]
 [0.00000000e + 000 + -2.00000000e 000]
 [0.00000000e + 000 + 2.00000000e 000]
 [0.00000000e + 000 + -2.00000000e 000]
 [0.00000000e + 000 + 2.00000000e 000]
 [0.00000000e + 000 + 2.00000000e 000]
 [0.00000000e + 000 + -2.00000000e 000]
 [0.00000000e + 000 + -2.00000000e 000]
 [0.00000000e + 000 2.00000000e + 000]

现在进口进口statsmodels.api为SM后我这样做:

  G = np.zeros([3,2])#克(x)是将存储回归参数的函数
mod_wls = sm.WLS(Z,X)
temp_g = mod_wls.fit()
打印temp_g.params

和我得到这样的输出:

  [-5.92878775e-323 -2.77777778e + 000]
 [-4.94065646e-324 -4.44444444e-001]
 [4.94065646e-323 1.88888889e + 000]

早些时候,<一个href=\"http://stackoverflow.com/questions/23248583/how-to-add-regression-functions-in-python-or-create-a-new-regression-function-f\">the这个问题的答案,我能predict测试数据的值 X_test 使用 numpy.dot ,就像这样:

  np.dot(X_test,temp_g.params)

我明白了,因为它容易endog矢量,是一维数组。但它是如何工作的时候我endog载体,在这种情况下,以Z ,是2D的?
当我尝试上述行作为一维版本时,我收到以下错误:

  self._check_integrity()
  文件C:\\用户\\程序\\蟒蛇\\ LIB \\站点包\\ statsmodels \\基地\\ data.py,247线,在_check_integrity
    提高ValueError错误(endog和exog矩阵大小不同)
ValueError错误:endog和exog矩阵具有不同的尺寸


解决方案

np.dot(X_test,temp_g.params)应该仍然工作。

在某些情况下,你需要检查一下矩阵的方位,有时候有必要转。

predict ,结果大多数其他方法都不行,因为模型假定因变量,Z,是一维。

问题再次你正在尝试做的?

如果你想要到z的独立适合列,然后遍历它,以便每个y 1D。

在z.T Y:RES = WLS(Y,X).fit()

z.T 允许遍历所有列。

在其他情况下,我们通常堆叠模式,使y为1D和它的第一部分是 Z [:,0] 和列的第二部分是 Z [:1] 。解释变量的设计矩阵或矩阵,必须相应地扩大。

有关多元因变量的支持是在酝酿了statsmodels但仍需要一些时间来准备。

I'm incrementally up the parameters of WLS regression functions using statsmodels.

I have a 10x3 dataset X that I declared like this:

X = np.array([[1,2,3],[1,2,3],[4,5,6],[1,2,3],[4,5,6],[1,2,3],[1,2,3],[4,5,6],[4,5,6],[1,2,3]])

This is my dataset, and I have a 10x2 endog vector that looks like this:

z =
[[  3.90311860e-322   2.00000000e+000]
 [  0.00000000e+000   2.00000000e+000]
 [  0.00000000e+000  -2.00000000e+000]
 [  0.00000000e+000   2.00000000e+000]
 [  0.00000000e+000  -2.00000000e+000]
 [  0.00000000e+000   2.00000000e+000]
 [  0.00000000e+000   2.00000000e+000]
 [  0.00000000e+000  -2.00000000e+000]
 [  0.00000000e+000  -2.00000000e+000]
 [  0.00000000e+000   2.00000000e+000]]

Now after importing import statsmodels.api as sm I do this:

g = np.zeros([3, 2]) # g(x) is a function that will store the regression parameters
mod_wls = sm.WLS(z, X)
temp_g = mod_wls.fit()
print temp_g.params

And I get this output:

[[ -5.92878775e-323  -2.77777778e+000]
 [ -4.94065646e-324  -4.44444444e-001]
 [  4.94065646e-323   1.88888889e+000]]

Earlier, from the answer to this question, I was able to predict the value of test data X_test using numpy.dot, like this:

np.dot(X_test, temp_g.params)

I understood that easily since it the endog vector, y was a 1D array. But how does it work when my endog vector, in this case, z, is 2D? When I try the above line as was used in the 1D version, I get the following error:

   self._check_integrity()
  File "C:\Users\app\Anaconda\lib\site-packages\statsmodels\base\data.py", line 247, in _check_integrity
    raise ValueError("endog and exog matrices are different sizes")
ValueError: endog and exog matrices are different sizes

解决方案

np.dot(X_test, temp_g.params) should still work.

In some cases you need to check what the orientation of the matrices are, sometimes it's necessary to transpose

However predict and most other methods of the results will not work, because the model assumes that dependent variable, z, is 1D.

The question is again what you are trying to do?

If you want to independently fit columns of z, then iterate over it so each y is 1D.

for y in z.T: res = WLS(y, X).fit()

z.T allows iteration over columns.

In other cases, we usually stack the model so that y is 1D and first part of it is z[:,0] and the second part of the column is z[:,1]. The design matrix or matrix of explanatory variables has to be expanded correspondingly.

Support for multivariate dependent variables is in the making for statsmodels but will still take some time to be ready.

这篇关于如何从statsmodels WLS回归2D参数测试得到的prediction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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