Sklearn决策树分类器显示浮动错误Python [不重复]

时间:2018-03-06 09:22:47

标签: python sklearn-pandas

我想使用sklearn DecisionTreeClassifier.

制作预测程序

我比较两个列表,ListOnePar有浮点值,timelist只有字符串。我总是得到同样的错误。我搜索了网络,但我找不到任何可以帮助我的东西。我只看到可以在两个列表之间进行比较(一个带有浮点数,另一个带有字符串。) 这不是另一个问题的重复,在另一个问题中,错误是完全不同的,整个程序是不同的。

这是错误:

Pred1=tree.DecisionTreeClassifier()
AttributeError: 'float' object has no attribute 'DecisionTreeClassifier'

这是代码:

from sklearn import tree

    ListOnePar=[]

    for child in tree1.get_children(id1):
        ListTwoPar=[]

        one=round(float(tree1.item(child,"values")[1]),2)
        two=round(float(tree1.item(child,"values")[2]),2)
        tree=round(float(tree1.item(child,"values")[3]),2)
        four=round(float(tree1.item(child,"values")[5]),1)
        five=round(float(tree1.item(child,"values")[6]),1)

        ListTwoPar.append(one)
        ListTwoPar.append(two)
        ListTwoPar.append(tree)
        ListTwoPar.append(four)
        ListTwoPar.append(five)

        ListOnePar.append(ListTwoPar)

    timelist=[]

    for child in tree1.get_children(id1):
        time=tree1.item(child,"values")[7]
        timelist.append(time)

    Pred1=tree.DecisionTreeClassifier()
    Pred1=Pred1.fit(ListOnePar,time)

    size=float(PredSizeEntry.get())
    time=float(PredTimeEntry.get())
    cost=float(PredCostEntry.get())
    level=float(PredLevelEntry.get())
    subcontractors=float(PredSubcontractorsEntry.get())

    ListForPrediction1=[]
    ListForPrediction2=[]

    ListForPrediction2.insert(0,size)
    ListForPrediction2.insert(1,time)
    ListForPrediction2.insert(2,cost)
    ListForPrediction2.insert(3,level)
    ListForPrediction2.insert(4,subcontractors)

    ListForPrediction1.append(ListForPrediction2)

    prediction1=Pred1.predict(ListForPrediction1) 
    print(prediction1[0])

1 个答案:

答案 0 :(得分:4)

  • 我认为您的计划tree
  • 中有一个变量
  • 程序很难使用import statement treetree变量,因为你要覆盖tree浮动
  • 将变量名称更改为three

    for child in tree1.get_children(id1):
        ListTwoPar=[]
    
        one=round(float(tree1.item(child,"values")[1]),2)
        two=round(float(tree1.item(child,"values")[2]),2)
        tree=round(float(tree1.item(child,"values")[3]),2)   # <===== variable to be changed from tree to three
        four=round(float(tree1.item(child,"values")[5]),1)
        five=round(float(tree1.item(child,"values")[6]),1)
    
  • 您在计算tree时将tree=round(float(tree1.item(child,"values")[3]),2)设为浮点数,因此您收到错误:AttributeError: 'float' object has no attribute 'DecisionTreeClassifier'