** 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()
Age1. What does it give you? Is it in the form of a float?StringVar()must sit in some sort of loop. So my guess is that at some point it receives something else than string and this explicitfloat()casting does not work. As @TheLizzard suggested try printing those values to see what it will return.StringVardoesn't have any loops in its implementation (or at least that is why I think). Each time you change the contents of theEntryit sets the value of theStringVar. It's basically a wrapper for a string that exists in tcl's world.StringVarare just normal objects.