在运行随机森林分类器的任何BayesSearchCV函数时出现错误

我尝试使用RF分类器,但每次尝试运行bayessearchCV函数时都会返回错误。附件中是我具体的示例和一个您可以运行并复现的示例。我怀疑这可能是由于train_test_split函数引起的,但我并不完全确定如何排查。请告诉我我的代码中是否有明显的错误…

我目前使用的是最新的sklearn/skopt/numpy等版本

import numpy as npimport pandas as pdfrom sklearn import preprocessingfrom matplotlib import pyplot as pltimport xgboost as xgbimport sklearnfrom sklearn.linear_model import LogisticRegressionfrom sklearn.linear_model import ElasticNetfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_score from sklearn.metrics import roc_auc_scorefrom skopt import BayesSearchCVfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.model_selection import GridSearchCVopt = BayesSearchCV(    RandomForestClassifier(random_state=42),    {        'n_estimators': (5,5000),        'max_features': ['auto','sqrt'],        'max_depth': (2,90),        'min_samples_split': (2,10),        'min_samples_leaf': (1,7),        'bootstrap': ["True","False"]    },    n_iter=32,    cv=3,    scoring='roc_auc')opt.fit(full_train, full_y_train)print("val. score: %s" % opt.best_score_)print("test score: %s" % opt.score(X_test_red, y_test))

错误

/Users/user/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/deprecation.py:67: FutureWarning: Class MaskedArray is deprecated; MaskedArray is deprecated in version 0.23 and will be removed in version 0.25. Use numpy.ma.MaskedArray instead.  warnings.warn(msg, category=FutureWarning)---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-20-8b1596e90c35> in <module>----> 1 opt.fit(full_train, full_y_train)      2       3 print("val. score: %s" % opt.best_score_)      4 print("test score: %s" % opt.score(X_test_red, y_test))~/opt/anaconda3/lib/python3.8/site-packages/skopt/searchcv.py in fit(self, X, y, groups, callback)~/opt/anaconda3/lib/python3.8/site-packages/skopt/searchcv.py in _step(self, X, y, search_space, optimizer, groups, n_points)~/opt/anaconda3/lib/python3.8/site-packages/skopt/searchcv.py in _fit(self, X, y, groups, parameter_iterable)~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/deprecation.py in wrapped(*args, **kwargs)     66         def wrapped(*args, **kwargs):     67             warnings.warn(msg, category=FutureWarning)---> 68             return init(*args, **kwargs)     69         cls.__init__ = wrapped     70 TypeError: object.__init__() takes exactly one argument (the instance to initialize)

供您复现的示例

from skopt import BayesSearchCVfrom sklearn.datasets import load_digitsfrom sklearn.svm import SVCfrom sklearn.model_selection import train_test_splitX, y = load_digits(10, True)X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.75, test_size=.25, random_state=0)# log-uniform: understand as search over p = exp(x) by varying xopt = BayesSearchCV(    SVC(),    {        'C': (1e-6, 1e+6, 'log-uniform'),        'gamma': (1e-6, 1e+1, 'log-uniform'),        'degree': (1, 8),  # integer valued parameter        'kernel': ['linear', 'poly', 'rbf'],  # categorical parameter    },    n_iter=32,    cv=3)opt.fit(X_train, y_train)print("val. score: %s" % opt.best_score_)print("test score: %s" % opt.score(X_test, y_test))

在我的机器上,这会产生与第一个示例相同的错误。


回答:

sklearn >= 0.23.0 的问题已在skopt版本0.8.1中修复。https://pypi.org/project/scikit-optimize/0.8.1/

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注