From 495857913d0a1c727fa8aab51e11fbe43110251b Mon Sep 17 00:00:00 2001 From: duxin Date: Wed, 29 Jul 2026 10:28:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20RandomizedSearchCV=20=E2=86=92=20Gr?= =?UTF-8?q?idSearchCV=20=E2=80=94=20SVR=20=E8=B6=85=E5=8F=82=E5=85=A8?= =?UTF-8?q?=E9=87=8F=E7=A9=B7=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - SVR 参数网格仅 216 种组合(4×6×3×3),全量搜索 ~2s 完成 - 之前 n_iter=10 只抽样 4.6%,靠运气撞最优参数 - 删除 n_iter 和 random_state 参数(GridSearchCV 不需要) - 保留 RandomizedSearchCV import 供其他模型(参数空间大的)使用 --- src/core/modeling/modeling_batch.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/core/modeling/modeling_batch.py b/src/core/modeling/modeling_batch.py index 5e27447..83f4eda 100644 --- a/src/core/modeling/modeling_batch.py +++ b/src/core/modeling/modeling_batch.py @@ -14,7 +14,7 @@ from sklearn.svm import SVR from sklearn.ensemble import RandomForestRegressor from sklearn.neighbors import KNeighborsRegressor from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet -from sklearn.model_selection import RandomizedSearchCV, cross_val_score, KFold, train_test_split +from sklearn.model_selection import GridSearchCV, RandomizedSearchCV, cross_val_score, KFold, train_test_split from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score from sklearn.cross_decomposition import PLSRegression from sklearn.ensemble import GradientBoostingRegressor, AdaBoostRegressor, ExtraTreesRegressor @@ -646,23 +646,22 @@ class WaterQualityModelingBatch: X_train[~np.isfinite(X_train)] = np.nan X_test[~np.isfinite(X_test)] = np.nan - # RandomizedSearchCV 需要以「步骤名__参数名」的格式索引参数网格; - # 我们原有的 config['params'] 是模型层的(无 __),统一加 model__ 前缀。 + # 以「步骤名__参数名」的格式索引参数网格; + # config['params'] 是模型层的(无 __),统一加 model__ 前缀。 prefixed_params = { f"model__{k}": v for k, v in config['params'].items() } - # 随机搜索:直接对 Pipeline 调优(scaler 仅在 train fold 上 fit) + # 全量网格搜索:SVR 超参组合仅 216 种(4×6×3×3), + # 穷举远优于 RandomizedSearchCV(n_iter=10) 的随机抽样 cv_strategy = KFold(n_splits=cv_folds, shuffle=True, random_state=random_state) - grid_search = RandomizedSearchCV( + grid_search = GridSearchCV( pipeline, prefixed_params, - n_iter=10, cv=cv_strategy, scoring=scoring, n_jobs=safe_n_jobs, - random_state=random_state, verbose=1, )