sklearn "Pipeline instance is not fitted yet." 错误,即使它是
sklearn "Pipeline instance is not fitted yet." error, even though it is
已经有人问过类似的问题,但答案并没有帮助我解决问题:
我正在尝试使用多个管道通过 One Hot Encoder 预处理我的数据以处理分类和数值数据(如 this blog 中所建议)。
这是我的代码,尽管我的分类器产生了 78% 的准确率,但我无法弄清楚为什么我不能绘制我正在训练的决策树以及什么可以帮助我解决问题。这是代码片段:
import pandas as pd
import sklearn.tree as tree
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.compose import ColumnTransformer
X = pd.DataFrame(data=data)
Y = pd.DataFrame(data=prediction)
categoricalFeatures = ["race", "gender"]
numericalFeatures = ["age", "number_of_actions"]
categoricalTransformer = Pipeline(steps=[
('onehot', OneHotEncoder(handle_unknown='ignore')),
])
numericTransformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler()),
])
preprocessor = ColumnTransformer(transformers=[
('num', numericTransformer, numericalFeatures),
('cat', categoricalTransformer, categoricalFeatures)
])
classifier = Pipeline(steps=[
('preprocessor', preprocessor),
('classifier', tree.DecisionTreeClassifier(max_depth=3))
])
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=12, stratify=Y)
classifier.fit(X_train, y_train)
print("model score: %.3f" % classifier.score(X_test, y_test)) # Prints accuracy of 0.78
text_representation = tree.export_text(classifier)
最后一个命令产生了这个错误,尽管模型正在拟合(我假设这是同步情况,但不知道如何解决):
sklearn.exceptions.NotFittedError: This Pipeline instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
您不能在整个管道上使用 export_text
函数,因为它只接受决策树对象,即 DecisionTreeClassifier
或 DecisionTreeRegressor
。只通过管道的拟合估计器,它将起作用:
text_representation = tree.export_text(classifier['classifier'])
提示Pipeline
对象未拟合的错误信息是由于scikit-learn
的check_is_fitted
函数引起的。它通过检查估计器上是否存在拟合属性(以尾随下划线结尾)来工作。由于 Pipeline
对象不公开此类属性,因此检查失败并引发错误,尽管它确实适合。但这不是问题,因为 Pipeline
对象不应该以这种方式使用。
已经有人问过类似的问题,但答案并没有帮助我解决问题:
我正在尝试使用多个管道通过 One Hot Encoder 预处理我的数据以处理分类和数值数据(如 this blog 中所建议)。
这是我的代码,尽管我的分类器产生了 78% 的准确率,但我无法弄清楚为什么我不能绘制我正在训练的决策树以及什么可以帮助我解决问题。这是代码片段:
import pandas as pd
import sklearn.tree as tree
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.compose import ColumnTransformer
X = pd.DataFrame(data=data)
Y = pd.DataFrame(data=prediction)
categoricalFeatures = ["race", "gender"]
numericalFeatures = ["age", "number_of_actions"]
categoricalTransformer = Pipeline(steps=[
('onehot', OneHotEncoder(handle_unknown='ignore')),
])
numericTransformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler()),
])
preprocessor = ColumnTransformer(transformers=[
('num', numericTransformer, numericalFeatures),
('cat', categoricalTransformer, categoricalFeatures)
])
classifier = Pipeline(steps=[
('preprocessor', preprocessor),
('classifier', tree.DecisionTreeClassifier(max_depth=3))
])
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=12, stratify=Y)
classifier.fit(X_train, y_train)
print("model score: %.3f" % classifier.score(X_test, y_test)) # Prints accuracy of 0.78
text_representation = tree.export_text(classifier)
最后一个命令产生了这个错误,尽管模型正在拟合(我假设这是同步情况,但不知道如何解决):
sklearn.exceptions.NotFittedError: This Pipeline instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
您不能在整个管道上使用 export_text
函数,因为它只接受决策树对象,即 DecisionTreeClassifier
或 DecisionTreeRegressor
。只通过管道的拟合估计器,它将起作用:
text_representation = tree.export_text(classifier['classifier'])
提示Pipeline
对象未拟合的错误信息是由于scikit-learn
的check_is_fitted
函数引起的。它通过检查估计器上是否存在拟合属性(以尾随下划线结尾)来工作。由于 Pipeline
对象不公开此类属性,因此检查失败并引发错误,尽管它确实适合。但这不是问题,因为 Pipeline
对象不应该以这种方式使用。