为什么需要浮动,我该如何解决?

时间:2018-02-19 02:39:37

标签: python machine-learning floating-point

我有这个决策树算法:

import sys
from class_vis import prettyPicture
from prep_terrain_data import makeTerrainData

import numpy as np
import pylab as pl

features_train, labels_train, features_test, labels_test = makeTerrainData()

from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

clf = DecisionTreeClassifier()
clf = clf.fit(features_train, labels_train)
prediction = clf.predict(features_test, labels_test)
acc = accuracy_score(labels_test, prediction)

def submitAccuracies():
  return {"acc":round(acc,3)} 

当我运行它时,我收到以下错误:

Traceback (most recent call last):
  File "vm_main.py", line 33, in <module>
    import main
  File "/tmp/vmuser_yafetikvhw/main.py", line 2, in <module>
    import studentMain
  File "/tmp/vmuser_yafetikvhw/studentMain.py", line 10, in <module>
    student_output = student_code.submitAccuracies()
  File "/tmp/vmuser_yafetikvhw/decisionTreeAccuracyQuiz.py", line 34, in submitAccuracies
    return float({"acc":round(acc,3)})
TypeError: a float is required

我被困在这里。我已经尝试将输入转换为浮动但仍然会出现相同的错误。例如:acc = acc / 1.0或acc = float(acc)

感谢。

1 个答案:

答案 0 :(得分:0)

得分

  

返回正确分类的样本数(int)。

从这里: http://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html

试试这个:

return {"acc":round(float(acc), 3)}