statsmodels中的自定义估算器WLS的sklearn check_estimator错误

时间:2020-10-01 18:38:23

标签: python scikit-learn cross-validation

我创建了sklearn自定义估算器(带有套索的statsmodels.regression.linear_model.WLS)以使用交叉验证。 check_estimator()报告错误,但是我没什么大不了,它似乎正在运行。

class SMWrapper(BaseEstimator, RegressorMixin):
    def __init__(self, alpha=0, lasso=True):
        self.alpha = alpha
        self.lasso = lasso
    def fit(self, X, y):
        # unpack weight from X
        self.model_ = WLS(y, X[:,:-1], weights=X[:,-1])
        if self.lasso:
            L1_wt = 1
        else:
            L1_wt = 0
        self.results_ = self.model_.fit_regularized(alpha=self.alpha, L1_wt=L1_wt, method='sqrt_lasso')
        return self
    def predict(self, X):
        return self.results_.predict(X[:,:-1])

    # yy shape is (nb_obs), xx shape is (nb_xvar, nb_obs), weight shape is (nb_obs)
    # pack weight as one more xvar, so that train/validation split will be done properly on weight.
    lenx = len(xx)
    xxx = np.full((yy.shape[0], lenx+1), 0.0)
    for i in range(lenx):
        xxx[:,i] = xx[i]
    xxx[:,lenx] = weight
    lassoReg = SMWrapper(lasso=lasso)
    param_grid = {'alpha': alpha}
    grid_search = GridSearchCV(lassoReg, param_grid, cv=10, scoring='neg_mean_squared_error',return_train_score=True)
    grid_search.fit(xxx, yy)

我愿意:

from sklearn.utils.estimator_checks import check_estimator
check_estimator(SMWrapper())

出现错误:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-518-6da3cc5b584c> in <module>()
----> 1 check_estimator(SMWrapper())

/nfs/geardata/anaconda2/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in check_estimator(Estimator)
    302     for check in _yield_all_checks(name, estimator):
    303         try:
--> 304             check(name, estimator)
    305         except SkipTest as exception:
    306             # the only SkipTest thrown currently results from not

/nfs/geardata/anaconda2/lib/python2.7/site-packages/sklearn/utils/testing.pyc in wrapper(*args, **kwargs)
    346             with warnings.catch_warnings():
    347                 warnings.simplefilter("ignore", self.category)
--> 348                 return fn(*args, **kwargs)
    349 
    350         return wrapper

/nfs/geardata/anaconda2/lib/python2.7/site-packages/sklearn/utils/estimator_checks.pyc in check_estimators_dtypes(name, estimator_orig)
   1100         estimator = clone(estimator_orig)
   1101         set_random_state(estimator, 1)
-> 1102         estimator.fit(X_train, y)
   1103 
   1104         for method in methods:

<ipython-input-516-613d1ce7615e> in fit(self, X, y)
     10         else:
     11             L1_wt = 0
---> 12         self.results_ = self.model_.fit_regularized(alpha=self.alpha, L1_wt=L1_wt, method='sqrt_lasso')
     13         return self
     14     def predict(self, X):

/nfs/geardata/anaconda2/lib/python2.7/site-packages/statsmodels/regression/linear_model.pyc in fit_regularized(self, method, alpha, L1_wt, start_params, profile_scale, refit, **kwargs)
    779             start_params=start_params,
    780             profile_scale=profile_scale,
--> 781             refit=refit, **kwargs)
    782 
    783         from statsmodels.base.elastic_net import (

/nfs/geardata/anaconda2/lib/python2.7/site-packages/statsmodels/regression/linear_model.pyc in fit_regularized(self, method, alpha, L1_wt, start_params, profile_scale, refit, **kwargs)
    998                 RegularizedResults, RegularizedResultsWrapper
    999             )
-> 1000             params = self._sqrt_lasso(alpha, refit, defaults["zero_tol"])
   1001             results = RegularizedResults(self, params)
   1002             return RegularizedResultsWrapper(results)

/nfs/geardata/anaconda2/lib/python2.7/site-packages/statsmodels/regression/linear_model.pyc in _sqrt_lasso(self, alpha, refit, zero_tol)
   1052         G1 = cvxopt.matrix(0., (n+1, 2*p+1))
   1053         G1[0, 0] = -1
-> 1054         G1[1:, 1:p+1] = self.exog
   1055         G1[1:, p+1:] = -self.exog
   1056 

NotImplementedError: invalid type in assignment

调试表明G1和self.exog的形状相同(self.exog是浮点的,G1也看起来是浮点的):

ipdb> self.exog.shape
(20, 4)
ipdb> G1[1:, 1:p+1]
<20x4 matrix, tc='d'>

我的代码可能有什么问题?我正在检查结果是否正确,这可能需要一些时间。

谢谢。

1 个答案:

答案 0 :(得分:1)

我相信您是由于WLS中的错误(类型不匹配)而收到此错误消息。比较:

import cvxopt
n =10
p = 5
G1 = cvxopt.matrix(0., (n+1, 2*p+1))

G1[0, 0] = -1
x = np.zeros((10,5))
G1[1:, 1:p+1] = x.astype("float64")

vs:

G1[1:, 1:p+1] = x.astype("float32")
NotImplementedError                       Traceback (most recent call last)
<ipython-input-82-d517da814a22> in <module>
      6 G1[0, 0] = -1
      7 x = np.zeros((10,5))
----> 8 G1[1:, 1:p+1] = x.astype("float32")

NotImplementedError: invalid type in assignment

可以通过以下方式纠正此[特殊]错误:

class SMWrapper(BaseEstimator, RegressorMixin):
    def __init__(self, alpha=0, lasso=True):
        self.alpha = alpha
        self.lasso = lasso
    def fit(self, X, y):
        # unpack weight from X
        self.model_ = WLS(y, X[:,:-1].astype("float64"), weights=X[:,-1]) # note astype
        if self.lasso:
            L1_wt = 1
        else:
            L1_wt = 0
        self.results_ = self.model_.fit_regularized(alpha=self.alpha, L1_wt=L1_wt, method='sqrt_lasso')
        return self
    def predict(self, X):
        return self.results_.predict(X[:,:-1])

但随后您将遇到另一个错误:check_complex_data(您可能会看到定义here

check_estimator面临的问题对于估算器(在您的情况下为WLS)应通过的检查过于严格。您可能会看到以下所有支票的清单:

from sklearn.utils.estimator_checks import check_estimator
gen = check_estimator(SMWrapper(), generate_only=True)
for x in iter(gen):
    print(x)

WLS不会通过所有检查,就像复数一样(我们从第2行移至第11行),但实际上可以使用您的数据,因此您可以使用它(或从头开始对其进行重新编码)。

编辑

一个有效的问题可能是“哪个测试运行,哪个失败”。为此,您可能希望检查https://scikit-learn.org/stable/auto_examples/release_highlights/plot_release_highlights_0_22_0.html#checking-scikit-learn-compatibility-of-an-estimatorsklearn.utils.estimator_checks._yield_checks来为估算器生成可用支票的生成器

编辑2

v0.24的替代方法可能是:

check_estimator(SMWrapper(), strict_mode=False)