import tkinter as tk
from tkinter import filedialog
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# GUI界面
root = tk.Tk()
root.title('数据预处理和模型训练')
root.geometry('400x300')

# 文件选择函数
def select_file():
    filepath = filedialog.askopenfilename()
    file_entry.delete(0, tk.END)
    file_entry.insert(0, filepath)

# 数据读取和预处理函数
def process_data():
    # 读入数据集
    filepath = file_entry.get()
    df = pd.read_csv(filepath)
    
    # 数据清洗
    df.dropna(inplace=True)
    
    # 数据标准化
    scaler = StandardScaler()
    scaled_data = scaler.fit_transform(df)
    
    # 数据分割
    X = scaled_data[:, :-1]
    y = scaled_data[:, -1]
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
    
    # 模型训练和测试
    clf = LogisticRegression()
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    
    # 输出预处理后的数据和模型评估结果
    output_text.delete('1.0', tk.END)
    output_text.insert(tk.END, '预处理后的数据:\n')
    output_text.insert(tk.END, str(scaled_data))
    output_text.insert(tk.END, '\n\n模型评估结果:\n')
    output_text.insert(tk.END, '准确率:%.2f' % accuracy)

# 文件选择按钮和输入框
file_label = tk.Label(root, text='请选择数据文件:')
file_label.pack()
file_entry = tk.Entry(root, width=30)
file_entry.pack()
file_button = tk.Button(root, text='选择', command=select_file)
file_button.pack()

# 数据处理按钮和输出框
process_button = tk.Button(root, text='处理数据并训练模型', command=process_data)
process_button.pack()
output_label = tk.Label(root, text='预处理后的数据和模型评估结果:')
output_label.pack()
output_text = tk.Text(root, width=40, height=15)
output_text.pack()

root.mainloop()
Python 数据预处理和模型训练 GUI 工具

原文地址: https://www.cveoy.top/t/topic/ojOs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录