以流水线方式输出决策树

Posted

技术标签:

【中文标题】以流水线方式输出决策树【英文标题】:output decision tree in the pipeline manner 【发布时间】:2020-01-21 15:35:42 【问题描述】:

您好,由于我是使用 sklearn 库的机器学习方法的新手,我尝试将决策树合并到管道中,然后对模型进行预测和输出,但是当我运行以下代码时,我收到了警告:

“管道”对象没有属性“树_”

所以我想知道管道是否不支持树输出,我该如何解决这个问题?我也尝试过直接使用 decision_tree 类,但我收到了另一个警告: 使用序列设置数组元素。 我知道这似乎是因为我有不同维度的向量,但仍然不知道如何处理这种情况。

from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.pipeline import Pipeline

from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree.export import export_text
from sklearn import tree


# a function that reads the corpus, tokenizes it and returns the documents
# and their labels
def read_corpus(corpus_file, use_sentiment):
    documents = []
    labels = []
    with open(corpus_file, encoding='utf-8') as f:
        for line in f:
            tokens = line.strip().split()

            documents.append(tokens[3:])

            if use_sentiment:
                # 2-class problem: positive vs negative
                labels.append( tokens[1] )
            else:
                # 6-class problem: books, camera, dvd, health, music, software
                labels.append( tokens[0] )

    return documents, labels

# a dummy function that just returns its input
def identity(x):
    return x

# read the data and split i into train and test
X, Y = read_corpus('/Users/dengchenglong/Downloads/trainset', use_sentiment=False)
split_point = int(0.75*len(X))
Xtrain = X[:split_point]
Ytrain = Y[:split_point]
Xtest = X[split_point:]
Ytest = Y[split_point:]

# let's use the TF-IDF vectorizer
tfidf = False

# we use a dummy function as tokenizer and preprocessor,
# since the texts are already preprocessed and tokenized.
if tfidf:
    vec = TfidfVectorizer(preprocessor = identity,
                          tokenizer = identity)
else:
    vec = CountVectorizer(preprocessor = identity,
                          tokenizer = identity)


# combine the vectorizer with a Naive Bayes classifier
classifier = Pipeline( [('vec', vec),
                        ('cls', tree.DecisionTreeClassifier())])


# train the classifier on the train dataset
decision_tree = classifier.fit(Xtrain, Ytrain)


# predict the labels of the test data 
Yguess = classifier.predict(Xtest)
tree.plot_tree(classifier.fit(Xtest, Ytest)) 
# report performance of the classifier
print(accuracy_score(Ytest, Yguess))
print(classification_report(Ytest, Yguess))

【问题讨论】:

【参考方案1】:

如果你试试这个会怎样:

from sklearn.pipeline import make_pipeline

# combine the vectorizer with a Naive Bayes classifier
clf = DecisionTreeClassifier()
classifier = make_pipeline(vec,clf)

看起来,在使用管道之前,您必须启动您尝试应用的模型。让我知道这是否有效,如果没有,它返回的错误。 来自:Scikit-learn documentation 示例来自:Make pipeline example with trees

【讨论】:

不起作用...同样的警告成立,似乎管道不支持树中的功能? 已编辑答案...检查一下? 无论如何,我相信如果您点击我分享的第二个链接,您应该能够让这些树在您的管道中工作。如果这个新的 asnwer 有任何问题,请告诉我

以上是关于以流水线方式输出决策树的主要内容,如果未能解决你的问题,请参考以下文章

机器学习系列(三十六)——回归决策树与决策树总结

机器学习之决策树算法

决策树分类器,多标签输出

随机森林(分类与回归)

以 png 或 pdf 可视化 scikit-learn/sklearn 多输出决策树回归

具有概率目标的决策树