以流水线方式输出决策树

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

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

“Pipeline”对象没有属性“tree_”

所以我想知道管道是否不支持树输出,我该如何解决这个问题?我也尝试过直接使用 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))
python pipeline decision-tree
2个回答
0
投票

如果你尝试这个会怎么样:

from sklearn.pipeline import make_pipeline

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

看起来,在使用管道之前,您必须启动您要应用的模型。让我知道这是否有效,如果无效,它返回的错误。 来自:Scikit-learn 文档 示例:用树制作管道示例


0
投票

我认为你可以在这里更改你的代码 tree.plot_tree(classifier.fit(Xtest, Ytest)) 进入 tree.plot_tree(decision_tree.named_steps['cls']) 因为named_steps是您安装的管道的一个属性https://scikit-learn.org/stable/modules/ generated/sklearn.pipeline.Pipeline.html。 它对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.