SkLearn中的混淆矩阵显示错误

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

给定虹膜数据集,我试图为我的分类模型绘制一个混淆矩阵。但是,我不断收到错误消息。希望有人可以指导。谢谢

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn import tree
from sklearn import metrics
from sklearn.metrics import confusion_matrix

def train_and_predict(train_input_features, train_outputs, prediction_features):
    classifier=tree.DecisionTreeClassifier()
    classifier.fit(train_input_features,train_outputs)
    predictions=classifier.predict(prediction_features)


iris = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target,test_size=0.3, random_state=0)

y_pred = train_and_predict(X_train, y_train, X_test)
print(confusion_matrix(y_test, predictions))

OUT:NameError:未定义名称'预测'

python-3.x machine-learning scikit-learn confusion-matrix
1个回答
0
投票

我发现我需要将代码粘贴到函数中,即:

def train_and_predict(train_input_features, train_outputs, prediction_features):
    classifier=tree.DecisionTreeClassifier()
    classifier.fit(train_input_features,train_outputs)
    predictions=classifier.predict(prediction_features)
    print(predictions)
    print('Confusion matrix\n',confusion_matrix(y_test,classifier.predict(X_test)))
© www.soinside.com 2019 - 2024. All rights reserved.