0

** Im getting this Value error on age2 = float(Age1) gender2 = float(gender1) this code im not sure why this error is caused and need a solution**

The data of Age1 and gender1 is str but i want it to be in float so i added this code but the Value error shows up idk why

import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from tkinter import *

root = Tk()
root.geometry('400x400')
root.resizable(0,0)
root.title('Data Check')
root.config(bg ='seashell3')
Age = StringVar()
gender = StringVar()

def predicts():

    Age1 = Age.get()
    gender1 = gender.get()

    age2 = int(Age1)
    gender2 = int(gender1)

    music_data = pd.read_csv('music.csv')
    x = music_data.drop(columns=['genre'])
    y = music_data['genre']
    x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.2)

    model = DecisionTreeClassifier()
    model.fit(x_train,y_train)
    predictions = model.predict(x_test)

    model1 = DecisionTreeClassifier()
    model1.fit(x,y)
    predictions1 = model1.predict([[age2,gender2]])

    score = accuracy_score(y_test, predictions)
    print(score,predictions1)
    Label(root, text = score, font = 'arial 15 bold').place(x = 90, y = 220)
    Label(root, text = predictions1, font = 'arial 15 bold').place(x = 120, y = 220)

predicts()


Label(root, text = 'Enter Age and Gender(0,1)' , font='arial 15 bold', bg = 'seashell2').place(x = 20,y=70)
Entry(root, font = 'arial 15', textvariable = Age , bg = 'antiquewhite2').place(x=90 , y = 130)
Entry(root, font = 'arial 15', textvariable = gender , bg = 'antiquewhite2').place(x=90 , y = 160)
Button(root, font = 'arial 13 bold', text = 'Predict'  ,padx =5,bg ='seashell4' ,command = predicts). place(x = 90, y = 290)

def Exit():
    root.destroy()

Button(root, font = 'arial 13 bold', text = 'EXIT'  ,padx =5,bg ='seashell4' ,command = Exit).place(x=230,y=310)
root.mainloop()

6
  • Does int casting work, since it is in the code? Commented May 2, 2021 at 14:07
  • Try printing Age1. What does it give you? Is it in the form of a float? Commented May 2, 2021 at 14:07
  • Ok, so it is not string to float but StringVar. I am not very familiar with tkinter but I think StringVar() must sit in some sort of loop. So my guess is that at some point it receives something else than string and this explicit float() casting does not work. As @TheLizzard suggested try printing those values to see what it will return. Commented May 2, 2021 at 14:19
  • 1
    @kaktus_car StringVar doesn't have any loops in its implementation (or at least that is why I think). Each time you change the contents of the Entry it sets the value of the StringVar. It's basically a wrapper for a string that exists in tcl's world. Commented May 2, 2021 at 14:23
  • 1
    @kaktus_car: "I think StringVar() must sit in some sort of loop" - no, nothing at all like that. Instances of StringVar are just normal objects. Commented May 2, 2021 at 14:50

1 Answer 1

2

I think I found the problem: in your code you have:

...
Age = StringVar()
...
predicts()
...
Button(..., command=predicts).place(...)

The problem is that you call predicts() just after you create the StringVar so its contents are "". And if we do float("") we get:

>>> float("")
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    float("")
ValueError: could not convert string to float: 
>>>

So actually that is what python was telling you :D

If you remove the predicts() but keep the command=predicts, it should solve your problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.