gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:16:55 25 4
gpt4 key购买 nike

我创建了 sklearn 自定义估计器(statsmodels.regression.linear_model.WLS with Lasso)以使用交叉验证。 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 是 float 的,G1 看起来也是 float 的):

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

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

谢谢。

最佳答案

我相信您收到此错误消息是由于 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")

对比:

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)

关于python - 来自 statsmodels 的自定义估算器 WLS 的 sklearn check_estimator 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64161389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com