为什么 .transform(test_X) 会出现此错误

Why does this error occur with .tranform(test_X)

我正在使用 MinMaxScaler 缩放我的数据,但出现此错误:

NotFittedError: This MinMaxScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

图片如下:

发生错误是因为您每次需要时都在实例化 MinMaxScalar() 的新对象。您只需要创建一个对象并根据您的要求在整个代码中使用它。

首先你需要创建一个 class MinMaxScalar() 的实例,像这样

my_scalar = MinMaxScalar()

然后到处使用同一个对象,而不是像您在代码中那样实例化一个新对象

train_X_scaled = my_scalar.fit_transform(train_X)

test_X_scaled = myscalar.transform(test_X)

有关如何使用不同标量的示例,请参阅 this blog post