fix: Pipeline 安全网 — preproc 与 model 间插入 _SafeFiniteTransformer
- 新增 _SafeFiniteTransformer: np.nan_to_num + clip(±1e15) - 放在 Pipeline preproc→model 之间,无论 MNF 数值溢出还是 PhysicalFeatureExtractor 除零,SVR 收不到 inf/NaN - X_train/X_test 入口先 replace(inf→NaN),SimpleImputer 接力填充
This commit is contained in:
@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user