ValueError:X 有 5 个特征,但 DecisionTreeClassifier 期望 6 个特征作为输入

问题描述 投票:0回答:0

我对

DecisionTreeClassifier()
中的输入功能有疑问。实际上,管道本身的预测效果很好,但直接从模型中效果不佳。

这是图案:

loaded_model, name = open_model() # here I loaded a model from my machine

这里是管道的组成部分:

Pipeline(steps=[('one hot',
                 ColumnTransformer(remainder='passthrough',
                                   transformers=[('o',
                                                  OneHotEncoder(handle_unknown='ignore'),
                                                  Int64Index([3], dtype='int64'))])),
                ('classify',
                 DecisionTreeClassifier(max_depth=4, min_samples_leaf=2,
                                        min_samples_split=3))])

实际上,当我尝试用管道预测一个实例时,它工作得很好:

# instance to predict
vec = [3 1 0 True 2]
vec_pipe = vec.reshape(1, -1)
loaded_model.predict(vec_pipe)

但是以模型本身为例:

predict_class = loaded_model.named_steps['classify'].predict(vec_pipe)

我收到这个错误:

ValueError: X has 5 features, but DecisionTreeClassifier is expecting 6 features as input.

我应该检查和/或修改代码的哪一部分?

python scikit-learn pipeline decision-tree
© www.soinside.com 2019 - 2024. All rights reserved.