当我有很多数据点时,我可以很好地使用 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。
请您参考如下方法:
对于一次观察,没有 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
