diff --git a/src/core/modeling/modeling_batch.py b/src/core/modeling/modeling_batch.py index 4f563eb..d84cee8 100644 --- a/src/core/modeling/modeling_batch.py +++ b/src/core/modeling/modeling_batch.py @@ -50,6 +50,22 @@ from src.preprocessing.spectral_Preprocessing import Preprocessing, get_preproce from src.core.utils.split_methods import spxy, ks +class _SafeFiniteTransformer(BaseEstimator, TransformerMixin): + """Pipeline 安全网:np.nan_to_num + clip,确保无 inf/NaN 进入下游模型。 + + 放在 Pipeline 的 preproc 与 model 之间,无论上游(MNF/比值除法) + 产生何种极端值,SVR 等严格校验的模型都不会因 inf 拒绝输入。 + """ + def fit(self, X, y=None): + return self + + def transform(self, X): + result = np.nan_to_num(np.asarray(X, dtype=np.float64), + nan=0.0, posinf=0.0, neginf=0.0) + result = np.clip(result, -1e15, 1e15) + return result + + class WaterQualityModelingBatch: """水质参数反演批量建模类""" @@ -619,9 +635,14 @@ class WaterQualityModelingBatch: pipeline = Pipeline([ ('imputer', SimpleImputer(strategy='median')), ('preproc', preproc), + ('cleaner', _SafeFiniteTransformer()), ('model', base_model), ]) + # ★ 入口清洗:inf → NaN,后续 SimpleImputer/cleaner 接力处理 + X_train = X_train.replace([np.inf, -np.inf], np.nan) + X_test = X_test.replace([np.inf, -np.inf], np.nan) + # RandomizedSearchCV 需要以「步骤名__参数名」的格式索引参数网格; # 我们原有的 config['params'] 是模型层的(无 __),统一加 model__ 前缀。 prefixed_params = {