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

Sklearn Decision Tree Classifier showing float error Python [not a duplicate]

我想用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])
  • 我认为你的程序中有一个变量tree
  • 程序混淆了使用导入语句 treetree 变量,因为您将 tree 覆盖为 float
  • 将变量名改为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=round(float(tree1.item(child,"values")[3]),2) 时将 tree 设为浮点数,因此出现错误:AttributeError: 'float' object has no attribute 'DecisionTreeClassifier'