首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Keras Sklearn调谐器模块'sklearn‘没有属性'pipeline’

Keras Sklearn调谐器模块'sklearn‘没有属性'pipeline’
EN

Stack Overflow用户
提问于 2021-09-10 16:54:31
回答 1查看 93关注 0票数 1
代码语言:javascript
复制
from sklearn import ensemble
from sklearn import linear_model

def build_model(hp):
    model_type = hp.Choice('model_type', ['random_forest', 'ridge'])
    if model_type == 'random_forest':
        with hp.conditional_scope('model_type', 'random_forest'):
            model = ensemble.RandomForestClassifier(
                n_estimators=hp.Int('n_estimators', 10, 50, step=10),
                max_depth=hp.Int('max_depth', 3, 10))
    elif model_type == 'ridge':
        with hp.conditional_scope('model_type', 'ridge'):
            model = linear_model.RidgeClassifier(
                alpha=hp.Float('alpha', 1e-3, 1, sampling='log'))
    else:
        raise ValueError('Unrecognized model_type')
    return model

tuner = kt.tuners.Sklearn(
        oracle=kt.oracles.BayesianOptimization(
            objective=kt.Objective('score', 'max'),
            max_trials=10),
        hypermodel=build_model,
        directory=".")

X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(
    X, y, test_size=0.2)

tuner.search(X_train, y_train)

best_model = tuner.get_best_models(num_models=1)[0]

在keras-tuner https://keras.io/api/keras_tuner/tuners/sklearn/上执行示例中的代码

我得到了如下所示的错误。应该怎么解决呢?

代码语言:javascript
复制
c:\users\99ans\appdata\local\programs\python\python39\lib\site-packages\keras_tuner\tuners\sklearn_tuner.py in run_trial(self, trial, X, y, sample_weight, groups)
    161                 sample_weight[train_indices] if sample_weight is not None else None
    162             )
--> 163 
    164             model = self.hypermodel.build(trial.hyperparameters)
    165             #if isinstance(model, Pipeline):

AttributeError: module 'sklearn' has no attribute 'pipeline'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-10 17:18:23

添加import sklearn.pipeline可以暂时解决这个问题。

这是一个非常新的问题,将在下一个版本中修复。

你可以在这里找到更多关于它的信息https://github.com/keras-team/keras-tuner/issues/600

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69135574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档