如何将决策树中使用的特征及其值与模型一起保存在 pickle 文件中

how to save features and their values used in decision trees in pickle file along with model

假设我有一个用 sklearn 库构建的决策树分类器。 我将模型文件保存到 pickle 中, 现在,我再次加载它以查看模型描述符,即所有特征及其值,我只得到模型定义。

这是代码:

import pickle
with open("./decision_treee.pkl", 'rb') as f:
    pickle_model = pickle.load(f)
print(pickle_model)

这是我得到的输出:

`DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='gini',
                   max_depth=7, max_features=None, max_leaf_nodes=None,
                   min_impurity_decrease=0.0, min_impurity_split=None,
                   min_samples_leaf=2, min_samples_split=2,
                   min_weight_fraction_leaf=0.0, presort='deprecated',
                   random_state=50, splitter='best')`

如何获得做出决策的特征及其值,并最终对给定的数据输入进行分类? 像这样。

|--- feature1<= 1.50

|   |--- feature2<= 0.88
|   |   |--- feature3<= 0.05
|   |   |   |--- feat4<= 0.58
|   |   |   |   |--- class: 1.0
|   |   |   |--- feat4>  0.58
|   |   |   |   |--- class: 1.0
|   |   |--- feat3>  0.05
|   |   |   |--- class: 1.0
|   |--- feat2>  0.88
|   |   |--- class: 0.0
|--- feat1>  1.50
|   |--- feat5<= 0.22
|   |   |--- feat6<= 0.90
|   |   |   |--- feat7<= 0.1
|   |   |   |   |   |--- other_chars >  0.14
|   |   |   |   |   |   |--- class: 0.0
|   |   |--- feat3>  0.90
|   |   |   |--- feat5<= 0.13
|   |   |   |   |--- feat6<= 0.25
|   |   |   |   |   |--- class: 1.0
|   |   |   |   |--- feat6>  0.25
|   |   |   |   |   |--- class: 0.0

是否可以将这些特征及其值保存在 pickle 文件中,而不仅仅是模型定义?

如果您对训练好的模型进行 pickle,加载后它应该包含所有特征和值。使用 sklearn.tree.export_text() 检查您的模型是否正确加载。