From 3edb73b6521bd3dced67b9fc379147258b96cc9f Mon Sep 17 00:00:00 2001 From: duxin Date: Wed, 29 Jul 2026 15:41:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20DualStream=20Pipeline=20=E4=B8=89?= =?UTF-8?q?=E9=A1=B9=E4=BF=AE=E5=A4=8D=20=E2=80=94=20StandardScaler=20+=20?= =?UTF-8?q?=E9=83=A8=E7=BD=B2=E5=AF=BC=E5=87=BA=20+=20=E6=8E=A8=E7=90=86?= =?UTF-8?q?=E6=8B=86=E5=BC=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 任务1: Pipeline 插入 StandardScaler imputer → FeatureUnion → cleaner → scaler(StandardScaler) → SVR MNF(≈1e4量级)与物理指数(≈1e0量级)统一尺度后入SVR 任务2: .npz 导出 scaler_mean / scaler_scale C++端可复现 (X - mean) / scale 任务3: 推理端 train_wavelengths 缺失时直接抛异常 不再静默裸退308列导致 SimpleImputer 维度不匹配崩溃 --- src/core/modeling/modeling_batch.py | 8 +++++++- src/core/prediction/inference_batch.py | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/core/modeling/modeling_batch.py b/src/core/modeling/modeling_batch.py index 0b9b5d0..8f7f79a 100644 --- a/src/core/modeling/modeling_batch.py +++ b/src/core/modeling/modeling_batch.py @@ -646,7 +646,8 @@ class WaterQualityModelingBatch: ('imputer', SimpleImputer(strategy='median')), ('preproc', preproc), ('cleaner', _SafeFiniteTransformer()), - ('model', base_model), + ('scaler', StandardScaler()), + ('model', base_model), ]) # ★ 入口清洗:inf → NaN,后续 SimpleImputer/cleaner 接力处理 @@ -785,6 +786,11 @@ class WaterQualityModelingBatch: 'mnf_W': np.asarray(_mnf.W_mnf_), 'mnf_n_components': int(getattr(_mnf, 'n_components_', _mnf.n_components)), } + # StandardScaler 参数(C++ 端需复现 (X - mean) / scale) + _scaler = model.named_steps.get('scaler') + if _scaler is not None and hasattr(_scaler, 'mean_'): + _deploy['scaler_mean'] = np.asarray(_scaler.mean_, dtype=np.float64) + _deploy['scaler_scale'] = np.asarray(_scaler.scale_, dtype=np.float64) # SVR 部署矩阵(rbf kernel 需要 support_vectors_) _deploy['svr_dual_coef'] = np.asarray(_svr.dual_coef_) _deploy['svr_intercept'] = np.asarray(_svr.intercept_) diff --git a/src/core/prediction/inference_batch.py b/src/core/prediction/inference_batch.py index e778178..9489a2d 100644 --- a/src/core/prediction/inference_batch.py +++ b/src/core/prediction/inference_batch.py @@ -446,8 +446,11 @@ class WaterQualityInference: """ train_wl = metadata.get('train_wavelengths', None) if train_wl is None or len(train_wl) == 0: - print("[DualStream_MNF] ⚠ 模型无 train_wavelengths,fallback 原样输入") - return spectra.values + raise ValueError( + "推理失败:metadata 中缺失 train_wavelengths。" + "DualStream_MNF 必须对齐训练波段以完成光谱重采样。" + "请使用包含 train_wavelengths 元数据的模型文件。" + ) # 提取纯光谱列 spec_cols = []