用python结合tkinter库实现def standard_X(X): X_copy = X.copy() # 拿数据 for col_name in X_copy.columns: # 取列名 col_data = X_copy[[col_name]] # 根据列名拿列数据,两个方括号是因为要二维数组 # fit_transform stand_data = StandardScaler().fit_transform(col_data.values) # 标准化 X_copy[col_name] = stand_data # 将数据替换成标准化后的数据 return X_copy standard_X(X).describe([0.01,0.25,0.5,0.75,0.99]).T stand_X = standard_X(X) for col_name in stand_X.columns: sns.distplot(stand_X[col_name]) plt.title(col_name) plt.show() X[0] = pd.cut(X[0],bins = 5,labels = [0,1,2,3,4]) sns.countplot(X[0]) for col_name in X.columns: X[col_name] = pd.cut(X[col_name],bins = 5,label knn = KNeighborsClassifier() X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2,random_state = 1) knn.fit(X_train,y_train) KNeighborsClassifier() knn.score(X_train,y_train) knn.score(X_test,y_test)成一个界面
时间: 2024-03-10 07:49:25 浏览: 135
简单python界面设计(tkinter库)
好的,下面是一个简单的示例,展示了如何使用Tkinter创建一个界面,其中包含您的代码。请注意,由于您未提供数据和标签,我将使用随机数据进行演示。您需要根据自己的需求调整代码。
```
import tkinter as tk
from tkinter import ttk
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
class MyGUI:
def __init__(self, master):
self.master = master
self.master.title("Data Standardization and Classification")
# Create labels and entry boxes for user input
self.label_1 = ttk.Label(self.master, text="Enter number of rows:")
self.label_1.grid(row=0, column=0, padx=10, pady=10)
self.entry_1 = ttk.Entry(self.master)
self.entry_1.grid(row=0, column=1, padx=10, pady=10)
self.label_2 = ttk.Label(self.master, text="Enter number of columns:")
self.label_2.grid(row=1, column=0, padx=10, pady=10)
self.entry_2 = ttk.Entry(self.master)
self.entry_2.grid(row=1, column=1, padx=10, pady=10)
self.button_1 = ttk.Button(self.master, text="Submit", command=self.submit)
self.button_1.grid(row=2, column=1, padx=10, pady=10)
# Create output text box
self.output = tk.Text(self.master, height=10, width=50)
self.output.grid(row=3, column=0, columnspan=2, padx=10, pady=10)
def submit(self):
# Get user input
num_rows = int(self.entry_1.get())
num_cols = int(self.entry_2.get())
# Generate random data
X = pd.DataFrame(np.random.randn(num_rows, num_cols))
# Standardize data
X_copy = X.copy()
for col_name in X_copy.columns:
col_data = X_copy[[col_name]]
stand_data = StandardScaler().fit_transform(col_data.values)
X_copy[col_name] = stand_data
output_1 = X_copy.describe([0.01, 0.25, 0.5, 0.75, 0.99]).T
# Display distribution plots
for col_name in X_copy.columns:
sns.distplot(X_copy[col_name])
plt.title(col_name)
plt.show()
# Categorize data and display count plot
X[0] = pd.cut(X[0], bins=5, labels=[0, 1, 2, 3, 4])
sns.countplot(X[0])
plt.show()
# Classify data using KNN
knn = KNeighborsClassifier()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
knn.fit(X_train, y_train)
output_2 = "Train score: {:.2f}\nTest score: {:.2f}".format(knn.score(X_train, y_train), knn.score(X_test, y_test))
# Display output
self.output.delete('1.0', tk.END)
self.output.insert(tk.END, "Data Description:\n{}\n\n".format(output_1))
self.output.insert(tk.END, "KNN Classification:\n{}".format(output_2))
root = tk.Tk()
my_gui = MyGUI(root)
root.mainloop()
```
在这个界面中,用户需要输入他们想要的随机数据的行数和列数,并单击“提交”按钮。然后,程序将生成数据,对其进行标准化,显示分布图,将其分类并显示计数图,并在最后使用KNN对其进行分类。程序将输出数据描述和分类结果到文本框中。
阅读全文