AttributeError: 'numpy.random.mtrand.RandomState' object has no attribute 'integers'

机器学习,超参优化时报错

hp_space_reg = {
    'clf_type': hp.choice('clf_type', [
        {
            'type': "LinearRegression",
            'clf': { 
                "normalize" :  hp.choice("lr.normalize", [True, False])
            }
        },
        {
            'type': "SVR",
            'clf': {
                'C': hp.loguniform('svm.C', -4.0 * np.log(10.0), 4.0 * np.log(10.0)),
                'kernel': 'rbf',
                'gamma': hp.choice('svm.gamma', ['auto', 'scale'])
            }
        },

        {
            'type': "XGBRegressor",
            'clf': {
                "n_estimators": hp.choice("n_estimators_xgb", range(10, 100)),
                "max_depth": hp.choice("max_depth_xgb", range(3, 10)),
                "learning_rate" : hp.loguniform('learning_rate_xgb', -1.0 * np.log(3.0), 1.0 * np.log(3.0)),
                "gamma": hp.loguniform('gamma_xgb', -1.0 * np.log(10.0), 1.0 * np.log(10.0)),
                "subsample" :hp.choice("subsample_xgb",[1,0.9,0.8,0.7,0.6]), 
                "n_jobs":hp.choice("n_jobs_xgb",[-1]) 
            }

        },
        
        {                                  
            'type': "DecisionTreeRegressor", 
            'clf': {
                "max_depth": hp.choice("max_depth_dtr", range(3, 10)),
                "criterion": hp.choice("criterion_dtr",["mse", "friedman_mse", "mae"]),
                 
            }                                                                   
        },
        
        {
            'type': "RandomForestRegressor", 
            'clf': {
                "n_estimators": hp.choice("n_estimators_rf", range(10, 100)),
                "max_depth": hp.choice("max_depth_rf", range(3, 10)),
                "min_samples_split": hp.choice("min_samples_split_rf", range(2, 3)),
                "min_samples_leaf": hp.choice(" min_samples_leaf_rf", range(1, 3)),
                "min_weight_fraction_leaf": hp.choice("min_weight_fraction_leaf_rf", [0,0.1,0.2,0.3]),
                "max_features":hp.choice("max_features_rf",["auto", "sqrt", "log2"]), 
                "n_jobs":hp.choice("n_jobs_rf",[-1]) 
            }
        },
        
    ])
} 
random_state=0 
best_reg = opt(X_train_wrapper_reg, y_train_reg, space=hp_space_reg, max_evals=100, scoring="r2", random_state=random_state)
score = best_reg.score(X_test_wrapper_reg, y_test_reg) 
print("test accuracy:", score) 
print("best model", best_reg) 
运行结果及报错内容
AttributeError                            Traceback (most recent call last)
<ipython-input-39-c27a503ee0cf> in <module>
     54 } 
     55 random_state=0
---> 56 best_reg = opt(X_train_wrapper_reg, y_train_reg, space=hp_space_reg, max_evals=100, scoring="r2", random_state=random_state)
     57 score = best_reg.score(X_test_wrapper_reg, y_test_reg)
     58 print("test accuracy:", score)

~\syy\OSCs20210518\hyper_opt.py in opt(X, y, space, algo, max_evals, scoring, random_state)
     61 
     62     trials_clf2 = Trials()
---> 63     best_clf2 = fmin(partial(f_to_min2, X=X, y=y, scoring=scoring, random_state=random_state),
     64                      space=space, algo=algo, max_evals=max_evals,
     65                      trials=trials_clf2, rstate=np.random.RandomState(random_state))

~\anaconda3\lib\site-packages\hyperopt\fmin.py in fmin(fn, space, algo, max_evals, timeout, loss_threshold, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len, show_progressbar, early_stop_fn, trials_save_file)
    538 
    539     if allow_trials_fmin and hasattr(trials, "fmin"):
--> 540         return trials.fmin(
    541             fn,
    542             space,

~\anaconda3\lib\site-packages\hyperopt\base.py in fmin(self, fn, space, algo, max_evals, timeout, loss_threshold, max_queue_len, rstate, verbose, pass_expr_memo_ctrl, catch_eval_exceptions, return_argmin, show_progressbar, early_stop_fn, trials_save_file)
    669         from .fmin import fmin
    670 
--> 671         return fmin(
    672             fn,
    673             space,

~\anaconda3\lib\site-packages\hyperopt\fmin.py in fmin(fn, space, algo, max_evals, timeout, loss_threshold, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len, show_progressbar, early_stop_fn, trials_save_file)
    584 
    585     # next line is where the fmin is actually executed
--> 586     rval.exhaust()
    587 
    588     if return_argmin:

~\anaconda3\lib\site-packages\hyperopt\fmin.py in exhaust(self)
    362     def exhaust(self):
    363         n_done = len(self.trials)
--> 364         self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
    365         self.trials.refresh()
    366         return self

~\anaconda3\lib\site-packages\hyperopt\fmin.py in run(self, N, block_until_done)
    277                     # processes orchestration
    278                     new_trials = algo(
--> 279                         new_ids, self.domain, trials, self.rstate.integers(2 ** 31 - 1)
    280                     )
    281                     assert len(new_ids) >= len(new_trials)

AttributeError: 'numpy.random.mtrand.RandomState' object has no attribute 'integers'

本人代码初学者,不太清楚该从何入手解决。
希望可以正常运行输出结果,麻烦大家了!谢谢!

这个是你调用方法或属性不存在,可能是模块的版本更新去掉某些方法属性。可以降低版本试试

版本不匹配,检查一下hyperopt的numpy版本要求是多少,对应一下就可以了