【问题标题】:how to save features and their values used in decision trees in pickle file along with model如何将决策树中使用的特征及其值与模型一起保存在pickle文件中
【发布时间】:2020-08-17 02:18:25
【问题描述】:

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

这是代码:

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 文件中,而不仅仅是模型定义?

【问题讨论】:

    标签: python-3.x scikit-learn pickle


    【解决方案1】:

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

    【讨论】:

    • 很高兴它有帮助!如果您的问题已完全回答,您可以将答案标记为正确。
    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 2016-11-15
    • 2023-03-12
    • 2017-10-22
    • 2019-09-17
    • 2018-04-16
    • 1970-01-01
    • 2017-11-09
    相关资源
    最近更新 更多