fix: 新模型路径推理时缺失 WQI 特征导致 Pipeline 维度不匹配
- 新模型路径(有 train_wavelengths)重采样后仅产出 50 列光谱, 未计算模型训练时包含的 WQI 指数特征(期望 113 列) - 旧模型路径(else 分支)有此逻辑但被隔离,新路径无法受益 - 将 WQI 特征补全逻辑提至 if/else 之后统一执行, 通过 n_features_in_ 属性自动检测特征缺口并补齐
This commit is contained in:
@ -662,6 +662,28 @@ class WaterQualityInference:
|
||||
print(f"[兼容填充] 特征不足,补零 "
|
||||
f"{n_current} → {expected_features} 列")
|
||||
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
# ★ 特征补全:模型训练时可能包含 WQI 指数等衍生特征,
|
||||
# 推理端需自动计算补齐(适用于新旧模型两条路径)
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
expected_features = getattr(model, 'n_features_in_', None)
|
||||
if expected_features is not None and spectra.shape[1] < expected_features:
|
||||
print(f"[特征补全] 检测到特征缺口:当前 {spectra.shape[1]} 列 "
|
||||
f"< 模型期望 {expected_features} 列,正在计算 WQI 指数...")
|
||||
try:
|
||||
from src.utils.water_index import WaterQualityIndexCalculator
|
||||
calc = WaterQualityIndexCalculator()
|
||||
formulas = calc.list_available()
|
||||
if formulas:
|
||||
results_df = calc.calculate_many(formulas, spectra, fast=True)
|
||||
if isinstance(results_df, pd.DataFrame) and not results_df.empty:
|
||||
original_col_count = spectra.shape[1]
|
||||
spectra = pd.concat([spectra, results_df], axis=1)
|
||||
print(f"[特征补全] 完成!扩充至 {spectra.shape[1]} 列 "
|
||||
f"(+{spectra.shape[1] - original_col_count} WQI)")
|
||||
except Exception as e:
|
||||
print(f"[特征补全] 失败: {e}")
|
||||
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
# 通用清洗
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
|
||||
Reference in New Issue
Block a user