import tkinter as tk
import tkinter.font as font
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
s_data = pd.read_csv("https://raw.githubusercontent.com/AdiPersonalWorks/Random/master/student_scores%20-%20student_scores.csv")
def predicted_score(hour :float):
X = s_data.iloc[:, :-1].values
y = s_data.iloc[:, 1].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
linear_regressor = LinearRegression()
linear_regressor.fit(X_train, y_train)
y_pred = linear_regressor.predict(X_test)
marks = linear_regressor.predict(hour)
return marks
window = tk.Tk()
window.geometry("600x300")
tk.Label(window, text="Marks Predictor", justify= tk.CENTER, font = font.Font(size = 40,weight='bold')).pack()
tk.Label(window, text="Predict Percentage", justify= tk.LEFT, font = font.Font(size = 20,weight='bold')).pack()
hour_input = tk.Entry(window)
hour_input.bind("<Return>",predicted_score)
result_predicted_score = tk.Label(window).pack()
hour_input.pack()
window.mainloop()
上面的代码根据学生的学习小时数来预测学生的分数。预测是使用简单的线性回归完成的。我已经将 Tkinter 用于 GUI。运行此程序时出现“TypeError:float() 参数必须是字符串或数字,而不是“事件””错误。