tkinter显示错误:ValueError:无法将字符串转换为浮点值:



当我运行此代码时,我会收到错误。我正在尝试创建一个小应用程序,预测一种疾病是否是疾病。我在这里实现了RandomForest。该代码在Jupyter Notebook中执行时运行。然而,当我制作一个.py文件时,我在clf.predict((中遇到了一个类型转换错误,我不明白为什么。这是我在.py文件中使用的代码,该文件显示错误。

import tkinter as tk
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 400, height = 600,  relief = 'raised')
canvas1.pack()
label1 = tk.Label(root, text='Predict the shit')
label1.config(font=('helvetica', 14))
canvas1.create_window(200, 25, window=label1)
label2 = tk.Label(root, text='Type G Value:')
label2.config(font=('helvetica', 10))
canvas1.create_window(200, 50, window=label2)
entry1 = tk.Entry (root) 
canvas1.create_window(200, 100, window=entry1)

label5 = tk.Label(root, text='Type H value:')
label5.config(font=('helvetica', 10))
canvas1.create_window(200, 140, window=label5)
entry2 = tk.Entry (root) 
canvas1.create_window(200, 160, window=entry2)
a1 = tk.IntVar()
a2 = tk.IntVar()
a1 = entry1.get()
a2 = entry2.get()
global lst1, lst2
lst1 = [a1]
lst2 = [a2]

def c1 ():
import numpy as np
import pandas as pd
import os
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier

data = pd.read_csv("C:/Users/kumar/Downloads/try/train_data.csv")
df1 = data[['Blood_Pressure_Abnormality','Level_of_Hemoglobin','Genetic_Pedigree_Coefficient']].copy()
x1= df1.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x1)
df1 = pd.DataFrame(x_scaled,columns=df1.columns)
median_genetic_pedigree_coeff = df1[['Genetic_Pedigree_Coefficient']].median()
df1['Genetic_Pedigree_Coefficient'].fillna(median_genetic_pedigree_coeff[0],inplace = True)
# Seprating the Data frame into input(x) and target(y)
list1 = list(df1.columns)
list1.remove('Blood_Pressure_Abnormality')
x = df1[list1]
y = df1[["Blood_Pressure_Abnormality"]]
y = np.array(y)
y = np.ravel(y)

# Splitting the Data into train and Test 
X_train, X_test, Y_train, Y_test= train_test_split(x, y, test_size=0.2,random_state=42)

clf = RandomForestClassifier(bootstrap=True, class_weight=None,
criterion='gini', max_depth=11, max_features='auto',
max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=100,
n_jobs=None, oob_score=False, random_state=42, verbose=0,
warm_start=False)

clf.fit(X_train,Y_train)
y_pred = clf.predict(X_test)
print(type(X_test))
print("With accuracy of ",accuracy_score(Y_test, y_pred))

df_new = pd.DataFrame(list(zip(lst1, lst2)),
columns =['H_value', 'G_Coeff'])
print(type(df_new))
y_pred = clf.predict(df_new)
print(y_pred)
hh = y_pred[0]
print(hh)
if hh == 1:
label4 = tk.Label(root, text= "Benign",font=('helvetica', 10, 'bold'))
canvas1.create_window(200, 240, window=label4)
else:
label4 = tk.Label(root, text= "Malignant",font=('helvetica', 10, 'bold'))
canvas1.create_window(200, 240, window=label4)

button1 = tk.Button(text='The entered case is', command=c1, bg='brown', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 200, window=button1)
root.mainloop()

我看到一个错误如下:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:UserskumarAnaconda3libtkinter__init__.py", line 1705, in __call__
return self.func(*args)
File "<ipython-input-4-1bb627d3a044>", line 93, in c1
y_pred = clf.predict(df_new)
File "C:UserskumarAnaconda3libsite-packagessklearnensembleforest.py", line 545, in predict
proba = self.predict_proba(X)
File "C:UserskumarAnaconda3libsite-packagessklearnensembleforest.py", line 588, in predict_proba
X = self._validate_X_predict(X)
File "C:UserskumarAnaconda3libsite-packagessklearnensembleforest.py", line 359, in _validate_X_predict
return self.estimators_[0]._validate_X_predict(X, check_input=True)
File "C:UserskumarAnaconda3libsite-packagessklearntreetree.py", line 391, in _validate_X_predict
X = check_array(X, dtype=DTYPE, accept_sparse="csr")
File "C:UserskumarAnaconda3libsite-packagessklearnutilsvalidation.py", line 496, in check_array
array = np.asarray(array, dtype=dtype, order=order)
File "C:UserskumarAnaconda3libsite-packagesnumpycorenumeric.py", line 538, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: 

我是编码新手。你能帮我修一下吗。当我在笔记本中运行与下面相同的代码时,它运行良好,没有任何错误。只有当我在tkinter中运行它时,我才看到错误。

import numpy as np
import pandas as pd
import os
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier

lst1 = [0.8]
lst2 = [0.5]
data = pd.read_csv("C:/Users/kumar/Downloads/try/train_data.csv")
df1 = data[['Blood_Pressure_Abnormality','Level_of_Hemoglobin','Genetic_Pedigree_Coefficient']].copy()
x1= df1.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x1)
df1 = pd.DataFrame(x_scaled,columns=df1.columns)
median_genetic_pedigree_coeff = df1[['Genetic_Pedigree_Coefficient']].median()
df1['Genetic_Pedigree_Coefficient'].fillna(median_genetic_pedigree_coeff[0],inplace = True)
# Seprating the Data frame into input(x) and target(y)
list1 = list(df1.columns)
list1.remove('Blood_Pressure_Abnormality')
x = df1[list1]
y = df1[["Blood_Pressure_Abnormality"]]
y = np.array(y)
y = np.ravel(y)

# Splitting the Data into train and Test 
X_train, X_test, Y_train, Y_test= train_test_split(x, y, test_size=0.2,random_state=42)

clf = RandomForestClassifier(bootstrap=True, class_weight=None,
criterion='gini', max_depth=11, max_features='auto',
max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=100,
n_jobs=None, oob_score=False, random_state=42, verbose=0,
warm_start=False)

clf.fit(X_train,Y_train)
y_pred = clf.predict(X_test)
print(type(X_test))
print("With accuracy of ",accuracy_score(Y_test, y_pred))

df_new = pd.DataFrame(list(zip(lst1, lst2)),
columns =['H_value', 'G_Coeff'])
print(type(df_new))
y_pred = clf.predict(df_new)
print(y_pred)
hh = y_pred[0]
print(hh)
if hh == 1:
print(type(hh))
else:
print(hh)

您先调用entry1 = tk.Entry(root),然后调用a1 = entry1.get(),而不给用户在条目中输入数据的时间。所以a1就是""——一个空字符串。要解决此问题,请尝试在c1函数中创建lst1lst2。–TheLizzard的回答。我感谢你的帮助TheLizzard

最新更新