refactor: RandomizedSearchCV → GridSearchCV — SVR 超参全量穷举
- SVR 参数网格仅 216 种组合(4×6×3×3),全量搜索 ~2s 完成 - 之前 n_iter=10 只抽样 4.6%,靠运气撞最优参数 - 删除 n_iter 和 random_state 参数(GridSearchCV 不需要) - 保留 RandomizedSearchCV import 供其他模型(参数空间大的)使用
This commit is contained in:
@ -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,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user