我正在使用决策树算法从测试文件中预测标签。但是,我需要打印包含该标签的整行或单个单元格。下面提到了我正在处理的代码。
import numpy as np
import csv
from sklearn.metrics import accuracy_score,confusion_matrix
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
path = "train_names.csv"
file=open(path)
reader = csv.reader(file)
data = np.asarray(list(reader))
#train data
names_train=data[1:,[0,1,2,3,4]]
label_train=data[1:,[5]]
#test data
names_test=data1[1:,[0,1,2,3,4]]
label_test=data1[1:,[5]]
decisionTreeClassifier = DecisionTreeClassifier()
decisionTreeClassifier.fit(names_train,label_train)
predictions = decisionTreeClassifier.predict(names_test)
print("Accuracy: ",accuracy_score(label_test,predictions))
for i in range(0,len(names_test)):
print (predictions[i])
答案 0 :(得分:0)
您是说要打印预测结果吗?如果是这样,您可以直接致电print(predictions)
或通过导入
sklearn
的{{1}}
confusion_matrix
您还可以使用此方法打印模型的得分
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(label_test, predictions)
print(cm)
# your X-axis is the predicted while Y-axis is the truth
答案 1 :(得分:0)
只需要迭代输入值(names_test
)和预测输出(predictions
)。试试吧!
predictions = decisionTreeClassifier.predict(names_test)
for x,y_pred in zip(names_test, predictions):
print(x,y_pred)