Sklearn Decision Tree Classifier Showing Float Error Python [not A Duplicate]
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])
Answer
- I think there is a variable in your program
tree
- The program is confused to use import statement
tree
ortree
variable because you are overwriting thetree
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 thetree=round(float(tree1.item(child,"values")[3]),2)
hence you are getting the error :AttributeError: 'float' object has no attribute 'DecisionTreeClassifier'
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module