简体   繁体   English

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

[英]sklearn check_estimator error for a custom estimator WLS from statsmodels

I created sklearn custom estimator (statsmodels.regression.linear_model.WLS with Lasso) to use cross validation.我创建了 sklearn 自定义估计器(statsmodels.regression.linear_model.WLS with Lasso)来使用交叉验证。 check_estimator() reports error, yet I can't anything wrong, and it seems to run. 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)

I do:我愿意:

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

It gives error:它给出了错误:

---------------------------------------------------------------------------
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

Debug says shape of G1 and self.exog are same (self.exog is float, G1 also looks to be float): Debug 说 G1 和 self.exog 的形状是一样的(self.exog 是浮动的,G1 看起来也是浮动的):

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

What might be wrong with my code?我的代码可能有什么问题? I am checking whether the result is correct, which may takes a little while.我正在检查结果是否正确,这可能需要一点时间。

Thanks.谢谢。

I believe you're getting this error message due to a bug (type mismatch) in WLS.我相信您收到此错误消息是由于 WLS 中的错误(类型不匹配)。 Compare:相比:

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

This [particular] error may be corrected by:这个[特定]错误可以通过以下方式纠正:

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])

but then you will run into another error: check_complex_data (you may see definition here )但随后您会遇到另一个错误: check_complex_data (您可能会在此处看到定义)

The problem you're facing check_estimator is way too strict on what checks an estimator (WLS in your case) should pass.您面临的问题check_estimator对估算器(在您的情况下为 WLS)应该通过的检查过于严格。 You may see the list of all checks with:您可能会看到所有检查的列表:

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

WLS is not passing all the checks, like for complex numbers (we moved from line 2 to line 11), but works with your data in practice, so you can live with it (or recode it from scratch). WLS 并没有通过所有检查,比如复数(我们从第 2 行移到第 11 行),而是在实践中处理您的数据,因此您可以接受它(或从头开始重新编码)。

EDIT编辑

A valid question could be "which test run and which fail".一个有效的问题可能是“哪个测试运行,哪个失败”。 To do this you may wish to check https://scikit-learn.org/stable/auto_examples/release_highlights/plot_release_highlights_0_22_0.html#checking-scikit-learn-compatibility-of-an-estimator and sklearn.utils.estimator_checks._yield_checks for a generator of available checks for your estimator为此,您可能希望检查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为您的估算器生成可用检查的生成器

EDIT 2编辑 2

An alternative for v0.24 might be: v0.24 的替代方案可能是:

check_estimator(SMWrapper(), strict_mode=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM