用两个列表Python(始终为0%)的用户输入的Sklearn准确度得分

时间:2018-03-08 19:45:32

标签: python

我有两个列表,有两种类型的值(int值列在名为projekti的列表中,字符串值在vreme中)。 我想制作一个程序,它将根据用户输入预测字符串值。用户输入是int值。我还想检查预测的准确性。 程序给了我一个错误,并且总是给我0%的准确度,无论用户输入是什么。 我做错了什么?

from sklearn import tree
from sklearn.metrics import accuracy_score

projekti=[[1000], [1300], [1400], [1100], [1200], [1100], [1500]] #velicina objekta, broj nivoa, vreme

vreme=[1, 0, 0, 1, 1, 1, 0] #uspesnost vremena

clf=tree.DecisionTreeClassifier()
clf=clf.fit(projekti,vreme)

unos=input("unesi velicinu: ")
List1=[]

List1.append([unos])

prediction=clf.predict(List1)

accuracy=accuracy_score(List1, prediction)
print(prediction[0]," ", accuracy*100,"%")

1 个答案:

答案 0 :(得分:0)

您的prediction输出为["no"]["yes"]的单个元素列表,因此当您调用accuracy = accuracy_score(List1, prediction)时,您正在比较包含字符串值的列表( str)具有整数值(int),该值始终为0.

这对我有用:

from sklearn import tree
from sklearn.metrics import accuracy_score
import numpy as np

projekti = [1000, 1300, 1400, 1100, 1200, 1100, 900]
vreme = [1, 0, 0, 1, 1, 1, 0]

clf = tree.DecisionTreeClassifier()
clf = clf.fit([[i] for i in projekti],vreme)

valuetotest = [900]
expectedresult = 0

prediction = clf.predict([valuetotest])
print(prediction)

accuracy=accuracy_score([expectedresult], prediction)
print(accuracy*100," %")

给出:

[0]
100.0  %

测试多个数据点:

from sklearn import tree
from sklearn.metrics import accuracy_score
import numpy as np

projekti = [1000, 1300, 1400, 1100, 1200, 1100, 900]
vreme = [1, 0, 0, 1, 1, 1, 0]

clf = tree.DecisionTreeClassifier()
clf = clf.fit([[i] for i in projekti],vreme)

valuetotest = [900, 1100, 1500, 2200]
expectedresult = [0, 1, 0, 0]

prediction = clf.predict([[i] for i in valuetotest])
print(prediction)

accuracy=accuracy_score([[i] for i in expectedresult], prediction)
print(accuracy*100," %")

给出:

[0 1 0 0]
100.0  %