From 12866f12b07f280d722c287224af21b2cc89a901 Mon Sep 17 00:00:00 2001 From: duxin Date: Wed, 29 Jul 2026 10:20:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Pipeline=20=E5=AE=89=E5=85=A8=E7=BD=91?= =?UTF-8?q?=20=E2=80=94=20preproc=20=E4=B8=8E=20model=20=E9=97=B4=E6=8F=92?= =?UTF-8?q?=E5=85=A5=20=5FSafeFiniteTransformer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 _SafeFiniteTransformer: np.nan_to_num + clip(±1e15) - 放在 Pipeline preproc→model 之间,无论 MNF 数值溢出还是 PhysicalFeatureExtractor 除零,SVR 收不到 inf/NaN - X_train/X_test 入口先 replace(inf→NaN),SimpleImputer 接力填充 --- src/core/modeling/modeling_batch.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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 = {