【问题标题】:Python error: len() of unsized object while using statsmodels with one row of dataPython错误:使用带有一行数据的statsmodels时未调整大小的对象的len()
【发布时间】:2014-06-14 06:32:34
【问题描述】:

当我有很多数据点时,我可以很好地使用 statsmodel 的 WLS (weighted least squares regression)。但是,当我尝试将 WLS 用于数据集中的单个样本时,我似乎遇到了 numpy 数组的问题。

我的意思是,如果我有一个数据集 X,它是一个二维数组,有很多行,WLS 可以正常工作。但如果我尝试在单行上工作,则不会。你会在下面的代码中明白我的意思:

import sys
from sklearn.externals.six.moves import xrange
from sklearn.metrics import accuracy_score
import pylab as pl
from sklearn.externals.six.moves import zip
import numpy as np
import statsmodels.api as sm
from statsmodels.sandbox.regression.predstd import wls_prediction_std

# this is my dataset X, with 10 rows
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 response vector, y, also with 10 rows
y = np.array([1, 1, 0, 1, 0, 1, 1, 0, 0, 1])
# weights, 10 rows
weights = np.array([ 0.1 , 0.1, 0.1 , 0.1, 0.1 , 0.1, 0.1 , 0.1, 0.1 , 0.1 ])

# the line below, using all 10 rows of X, gives no errors but is commented out
# mod_wls = sm.WLS(y, X, weights)
# and this is the line I need, which is giving errors:
mod_wls = sm.WLS(np.array(y[0]), np.array([X[0]]),np.array([weights[0]]))

上面的最后一行最初只是mod_wls = sm.WLS(y[0], X[0], weights[0])

但这给了我像object of type 'numpy.float64' has no len() 这样的错误,因此我把它们变成了数组。 但现在我不断收到此错误:

Traceback (most recent call last):
  File "C:\Users\app\Documents\Python Scripts\test.py", line 53, in <module>
    mod_wls = sm.WLS(np.array(y[0]), np.array([X[0]]),np.array([weights[0]]))
  File "C:\Users\app\Anaconda\lib\site-packages\statsmodels\regression\linear_model.py", line 383, in __init__
    weights=weights, hasconst=hasconst)
  File "C:\Users\app\Anaconda\lib\site-packages\statsmodels\regression\linear_model.py", line 79, in __init__
    super(RegressionModel, self).__init__(endog, exog, **kwargs)
  File "C:\Users\app\Anaconda\lib\site-packages\statsmodels\base\model.py", line 136, in __init__
    super(LikelihoodModel, self).__init__(endog, exog, **kwargs)
  File "C:\Users\app\Anaconda\lib\site-packages\statsmodels\base\model.py", line 52, in __init__
    self.data = handle_data(endog, exog, missing, hasconst, **kwargs)
  File "C:\Users\app\Anaconda\lib\site-packages\statsmodels\base\data.py", line 401, in handle_data
    return klass(endog, exog=exog, missing=missing, hasconst=hasconst, **kwargs)
  File "C:\Users\app\Anaconda\lib\site-packages\statsmodels\base\data.py", line 78, in __init__
    self._check_integrity()
  File "C:\Users\app\Anaconda\lib\site-packages\statsmodels\base\data.py", line 249, in _check_integrity
    print len(self.endog)
TypeError: len() of unsized object

所以为了看看长度有什么问题,我这样做了:

print "y size: "
print len(np.array([y[0]]))
print "X size"
print len (np.array([X[0]]))
print "weights size"
print len(np.array([weights[0]]))

得到了这个输出:

y size: 
1
X size
1
weights size
1

然后我尝试了这个:

print "x shape"
print X[0].shape
print "y shape"
print y[0].shape

输出是:

x shape
(3L,)
y shape
()

data.py 中的第 249 行,即错误所指,有这个功能,我在其中添加了一堆“打印尺寸”以查看发生了什么:

def _check_integrity(self):
    if self.exog is not None:
        print "exog size: " 
        print len(self.exog)            
        print "endog size"
        print len(self.endog) # <-- this, and the line below are causing the error
        if len(self.exog) != len(self.endog):
            raise ValueError("endog and exog matrices are different sizes")

len(self.endog) 似乎有问题。虽然当我尝试打印出len(np.array([y[0]])) 时,它只是给出了输出1。但不知何故,当y 进入 check_integrity 函数并变为endog 时,它的行为就不一样了....还是发生了其他事情?

我该怎么办?我正在使用一种算法,我确实需要为X 的每一行分别运行 WLS。

【问题讨论】:

  • 您为什么要这样做?单一观察的回归是我们(statsmodels 开发人员)从未考虑过的。在您的示例中,您尝试通过 1 个观察值估计 3 个参数。
  • 我正在尝试在此处实施步骤 (2)(ii):stats.stackexchange.com/questions/93691/…。如果 $X$ 是完整的观察集,并且 $x_i$ 是其中的一个观察值,那么对单个观察值的估计不是这意味着什么?
  • step(2)(ii) 我很确定这意味着使用所有 i,即使用权重向量 (w_i) 在 $(x_i)$ 矩阵上回归 g 向量。听起来类似于迭代重新加权最小二乘法,这是一种常见的优化方法。我没有试图了解细节。

标签: python arrays numpy statsmodels


【解决方案1】:

对于一次观察,没有 WLS 这样的东西。当它们归一化为 1 时,单个权重将简单地变为 1。如果你想这样做,虽然我猜你不这样做,只需使用 OLS。解决方案将是 SVD 的结果,但数据中没有任何实际关系。

使用 pinv/svd 的 OLS 解决方案

np.dot(np.linalg.pinv(X[[0]]), y[0])

尽管您可以编造任何有效的答案并获得相同的结果。我不确定 SVD 解决方案与其他非唯一解决方案的具体属性是什么。

[~/]
[26]: beta = [-.5, .25, 1/3.]

[~/]
[27]: np.dot(beta, X[0])
[27]: 1.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 2018-08-31
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    相关资源
    最近更新 更多