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 = []