fix: DualStream Pipeline 三项修复 — StandardScaler + 部署导出 + 推理拆弹
任务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 维度不匹配崩溃
This commit is contained in:
@ -646,6 +646,7 @@ class WaterQualityModelingBatch:
|
|||||||
('imputer', SimpleImputer(strategy='median')),
|
('imputer', SimpleImputer(strategy='median')),
|
||||||
('preproc', preproc),
|
('preproc', preproc),
|
||||||
('cleaner', _SafeFiniteTransformer()),
|
('cleaner', _SafeFiniteTransformer()),
|
||||||
|
('scaler', StandardScaler()),
|
||||||
('model', base_model),
|
('model', base_model),
|
||||||
])
|
])
|
||||||
|
|
||||||
@ -785,6 +786,11 @@ class WaterQualityModelingBatch:
|
|||||||
'mnf_W': np.asarray(_mnf.W_mnf_),
|
'mnf_W': np.asarray(_mnf.W_mnf_),
|
||||||
'mnf_n_components': int(getattr(_mnf, 'n_components_', _mnf.n_components)),
|
'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_)
|
# SVR 部署矩阵(rbf kernel 需要 support_vectors_)
|
||||||
_deploy['svr_dual_coef'] = np.asarray(_svr.dual_coef_)
|
_deploy['svr_dual_coef'] = np.asarray(_svr.dual_coef_)
|
||||||
_deploy['svr_intercept'] = np.asarray(_svr.intercept_)
|
_deploy['svr_intercept'] = np.asarray(_svr.intercept_)
|
||||||
|
|||||||
@ -446,8 +446,11 @@ class WaterQualityInference:
|
|||||||
"""
|
"""
|
||||||
train_wl = metadata.get('train_wavelengths', None)
|
train_wl = metadata.get('train_wavelengths', None)
|
||||||
if train_wl is None or len(train_wl) == 0:
|
if train_wl is None or len(train_wl) == 0:
|
||||||
print("[DualStream_MNF] ⚠ 模型无 train_wavelengths,fallback 原样输入")
|
raise ValueError(
|
||||||
return spectra.values
|
"推理失败:metadata 中缺失 train_wavelengths。"
|
||||||
|
"DualStream_MNF 必须对齐训练波段以完成光谱重采样。"
|
||||||
|
"请使用包含 train_wavelengths 元数据的模型文件。"
|
||||||
|
)
|
||||||
|
|
||||||
# 提取纯光谱列
|
# 提取纯光谱列
|
||||||
spec_cols = []
|
spec_cols = []
|
||||||
|
|||||||
Reference in New Issue
Block a user