import pandas as pd
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsRegressor
from catboost import CatBoostRegressor
from sklearn.dummy import DummyRegressor
from sklearn.ensemble import AdaBoostRegressor, ExtraTreesRegressor
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
# 加载CSV数据集
data = pd.read_csv('/kaggle/input/guloose/1-70.csv')
features = ['feature']
target = data['target']
X_train, X_test, y_train, y_test = train_test_split(data[features], target, test_size=0.25, random_state=4000)
# 创建回归器对象
knn_regressor = KNeighborsRegressor()
catboost_regressor = CatBoostRegressor()
# dummy_regressor = DummyRegressor()
adaboost_regressor = AdaBoostRegressor()
extra_trees_regressor = ExtraTreesRegressor()
# 定义超参数空间
param_grid = [
{
'model': [extra_trees_regressor],
'model__n_estimators': [50, 100, 200],
'model__max_depth': [None, 5, 10]
},
{
'model': [knn_regressor],
'model__n_neighbors': [3, 5, 7],
'model__weights': ['uniform', 'distance']
},
{
'model': [adaboost_regressor],
'model__n_estimators': [50, 100, 200],
'model__learning_rate': [0.1,0.2,0.5,0.8]
},
# {
# 'model': [dummy_regressor],
# 'model__base_estimator': [None]
# },
{
'model': [catboost_regressor],
'model__learning_rate': [0.1, 0.01, 0.001],
'model__depth': [6, 8],
'model__iterations': [100, 200]
}
]
# 创建管道
pipeline = Pipeline(steps=[('model', None)])
# 创建网格搜索对象
grid_search = GridSearchCV(pipeline, param_grid, scoring='neg_mean_squared_error', cv=3)
# 执行网格搜索
grid_search.fit(X_train, y_train)
# 输出最佳模型和超参数组合
best_model = grid_search.best_estimator_
best_params = grid_search.best_params_
best_score = grid_search.best_score_
print("Best Model:", best_model)
print("Best Parameters:", best_params)
print("Best Score:", best_score)
机器学习-网格搜索寻找合适的模型
于 2024-03-26 15:25:37 首次发布