【发布时间】:2021-07-25 04:12:58
【问题描述】:
** 我收到此值错误 年龄2 =浮动(年龄1) 性别2 =浮动(性别1) 此代码我不确定为什么会导致此错误并需要解决方案**
Age1 和 gender1 的数据是 str,但我希望它处于浮动状态,所以我添加了此代码,但 Value 错误显示 idk 为什么
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()
【问题讨论】:
-
int 转换是否有效,因为它在代码中?
-
尝试打印
Age1。它给了你什么?是浮动的形式吗? -
好的,所以它不是要浮动的字符串,而是 StringVar。我对 tkinter 不是很熟悉,但我认为
StringVar()必须处于某种循环中。所以我的猜测是,在某些时候它接收到的不是字符串,而这个显式的float()转换不起作用。正如@TheLizzard 建议的那样,尝试打印这些值以查看它会返回什么。 -
@kaktus_car
StringVar在其实现中没有任何循环(或者至少这就是我认为的原因)。每次更改Entry的内容时,它都会设置StringVar的值。它基本上是一个存在于 tcl 世界中的字符串的包装器。 -
@kaktus_car:“我认为 StringVar() 必须处于某种循环中” - 不,根本不是那样的。
StringVar的实例只是普通对象。
标签: python tkinter scikit-learn valueerror