Ad

Sklearn Decision Tree Classifier Showing Float Error Python [not A Duplicate]

- 1 answer

I want to make a program for prediction using sklearn DecisionTreeClassifier.

Im comparing two lists, ListOnePar that has float values, and timelist that has only strings. I always get the same error. I searched the web and I didnt find anything that can help me. All I saw that comparison can be done between two lists (one with floats and other with strings.) This is not a duplicate of the other question, in the other question the error is totally different, and the whole program is different.

This is the error :

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

This is the code :

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])
Ad

Answer

  • I think there is a variable in your program tree
  • The program is confused to use import statement tree or tree variable because you are overwriting the tree to float
  • Change the variable name to 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)
    
  • You are making tree as float while calculating the tree=round(float(tree1.item(child,"values")[3]),2) hence you are getting the error : AttributeError: 'float' object has no attribute 'DecisionTreeClassifier'

Ad
source: stackoverflow.com
Ad