如何使用贝叶斯优化(在Python中)在超参数空间上优化超参数?

如何使用贝叶斯优化(在Python中)在超参数空间上优化超参数?,python,scikit-learn,hyperparameters,hyperopt,Python,Scikit Learn,Hyperparameters,Hyperopt,我正试图使用下面的代码在超参数空间上使用贝叶斯优化对超参数进行优化,以实现随机森林回归,但我得到一个错误,即 TypeError:init()获得意外的关键字参数“min_samples” 我在尝试以下代码时遇到此错误: # Import packages import numpy as np from sklearn.metrics import mean_squared_error from sklearn import datasets from sklearn.ensemble impo

我正试图使用下面的代码在超参数空间上使用贝叶斯优化对超参数进行优化,以实现随机森林回归,但我得到一个错误,即

TypeError:init()获得意外的关键字参数“min_samples”

我在尝试以下代码时遇到此错误:

# Import packages
import numpy as np
from sklearn.metrics import mean_squared_error
from sklearn import datasets
from sklearn.ensemble import RandomForestRegressor
from hyperopt import hp, tpe, fmin, Trials, STATUS_OK

# Create datasets
reg_prob = datasets.make_friedman1(n_samples=100, n_features=10, noise=1.0, random_state=None)
x_train = reg_prob[0][0:50]
y_train = reg_prob[1][0:50]
x_test = reg_prob[0][50:100]
y_test = reg_prob[1][50:100]


#Create Hyperparameter space
space= {'n_estimators':hp.choice('n_estimators', range(2, 150, 1)),
        'min_samples':hp.choice('min_samples', range(2, 100, 1)),
        'max_features':hp.choice('max_features', range(2, 100, 1)),
        'max_samples':hp.choice('max_samples', range(2, 100, 1)),
       }


#Define Objective Function
def objective(space):
    
    rf = RandomForestRegressor(**space)

    
    # fit Training model
    rf.fit(x_train, y_train)
    
    # Making predictions and find RMSE
    y_pred = rf.predict(x_test)
    mse = mean_squared_error(y_test,y_pred)
    rmse = np.sqrt(mse)
    
    
    # Return RMSE
    return rmse


#Surrogate Fn
trials = Trials()
best = fmin(objective,
    space=space,
    algo=tpe.suggest,
    max_evals=100,
    trials=trials)

print(best)
print(trials.results)
我还尝试使用下面的代码列出目标函数中的超参数,但我得到了以下错误

TypeError:objective()缺少3个必需的位置参数:“最小样本数”、“最大特征数”和“最大样本数”

你能告诉我如何修复我的代码吗

我能够使用以下代码调整单个超参数:

# Import packages
import numpy as np
import time
from sklearn.metrics import mean_squared_error
from sklearn import datasets
from sklearn.ensemble import RandomForestRegressor

from hyperopt import hp, tpe, fmin, Trials, STATUS_OK
from collections import OrderedDict

reg_prob = datasets.make_friedman1(n_samples=100, n_features=10, noise=1.0, random_state=None)
x_train = reg_prob[0][0:50]
y_train = reg_prob[1][0:50]
x_test = reg_prob[0][50:100]
y_test = reg_prob[1][50:100]

space= hp.choice('num_leaves', range(2, 100, 1))


def objective(num_leaves):
    
    rf = RandomForestRegressor(num_leaves)
    

    rf.fit(x_train, y_train)
    

    y_pred = rf.predict(x_test)
    mse = mean_squared_error(y_test,y_pred)
    rmse = np.sqrt(mse)
    

    
    # Return RMSE
    return rmse

trials = Trials()
best = fmin(objective,
    space=space,
    algo=tpe.suggest,
    max_evals=100,
    trials=trials)

print(best)
print(trials.results)

问题在于
RandomForestClassifier
中没有名为
minu samples
的参数。看见可能你的意思是
min\u samples\u leaf

只要将
min\u sample\u leaf
的上限保持在数据集中样本数的范围内即可

否则,代码就没有其他问题了

import matplotlib.pyplot as plt

# Import packages
import numpy as np
from sklearn.metrics import mean_squared_error
from sklearn import datasets
from sklearn.ensemble import RandomForestRegressor
from hyperopt import hp, tpe, fmin, Trials, STATUS_OK

# Create datasets
reg_prob = datasets.make_friedman1(n_samples=100, n_features=10, noise=1.0, random_state=None)
x_train = reg_prob[0][0:50]
y_train = reg_prob[1][0:50]
x_test = reg_prob[0][50:100]
y_test = reg_prob[1][50:100]


#Create Hyperparameter space
space= {'n_estimators':hp.choice('n_estimators', range(2, 150, 1)),
        'min_samples_leaf':hp.choice('min_samples', range(2, 50, 1)),
        'max_features':hp.choice('max_features', range(2, 10, 1)),
        'max_samples':hp.choice('max_samples', range(2, 50, 1)),
       }


#Define Objective Function
def objective(space):
    
    rf = RandomForestRegressor(**space)

    
    # fit Training model
    rf.fit(x_train, y_train)
    
    # Making predictions and find RMSE
    y_pred = rf.predict(x_test)
    mse = mean_squared_error(y_test,y_pred)
    rmse = np.sqrt(mse)
    
    
    # Return RMSE
    return rmse


#Surrogate Fn
trials = Trials()
best = fmin(objective,
    space=space,
    algo=tpe.suggest,
    max_evals=2,
    trials=trials)

print(best)
print(trials.results)
import matplotlib.pyplot as plt

# Import packages
import numpy as np
from sklearn.metrics import mean_squared_error
from sklearn import datasets
from sklearn.ensemble import RandomForestRegressor
from hyperopt import hp, tpe, fmin, Trials, STATUS_OK

# Create datasets
reg_prob = datasets.make_friedman1(n_samples=100, n_features=10, noise=1.0, random_state=None)
x_train = reg_prob[0][0:50]
y_train = reg_prob[1][0:50]
x_test = reg_prob[0][50:100]
y_test = reg_prob[1][50:100]


#Create Hyperparameter space
space= {'n_estimators':hp.choice('n_estimators', range(2, 150, 1)),
        'min_samples_leaf':hp.choice('min_samples', range(2, 50, 1)),
        'max_features':hp.choice('max_features', range(2, 10, 1)),
        'max_samples':hp.choice('max_samples', range(2, 50, 1)),
       }


#Define Objective Function
def objective(space):
    
    rf = RandomForestRegressor(**space)

    
    # fit Training model
    rf.fit(x_train, y_train)
    
    # Making predictions and find RMSE
    y_pred = rf.predict(x_test)
    mse = mean_squared_error(y_test,y_pred)
    rmse = np.sqrt(mse)
    
    
    # Return RMSE
    return rmse


#Surrogate Fn
trials = Trials()
best = fmin(objective,
    space=space,
    algo=tpe.suggest,
    max_evals=2,
    trials=trials)

print(best)
print(trials.results)