python runtime_error ai_generated true

sklearn.exceptions.NotFittedError: 此 StandardScaler 实例尚未拟合。请先使用适当参数调用 'fit'。

sklearn.exceptions.NotFittedError: This StandardScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

ID: python/sklearn-notfitted-error

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-02-02首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

在 fit 之前调用 transform/predict,或在管道中使用了未拟合的新估计器。

English

Calling transform/predict on an estimator before fit, or using a fresh estimator inside a pipeline instead of the fitted one.

generic

解决方案

  1. 95% 成功率
    scaler = StandardScaler().fit(X_train); X_test_s = scaler.transform(X_test)
  2. 95% 成功率
    Wrap in Pipeline([('scaler', StandardScaler()), ('clf', LogisticRegression())]) so fit handles all steps

无效尝试

常见但无效的做法:

  1. 90% 失败

    Same error, no fitted state

  2. 60% 失败

    StandardScaler supports partial_fit but requires correct usage; often yields wrong stats