大三课设-分类聚类预测系统
大三機器學習課設
下面介紹一下我們的課設系統
首先看一下我們的課設要求:
1.熟悉機器學習的完整流程,包括:問題建模,獲取數據,特征工程,模型訓練,模型調優,線上運行;或者分為三大塊:數據準備與預處理,模型選擇與訓練,模型驗證與參數調優。
2.繪制機器學習算法分類歸納思維導圖,按照有監督學習、無監督學習、半監督學習和強化學習進行繪制,對學過的算法進行歸納總結。
3.自行選擇學習任務,按照機器學習流程,分別設計分類、預測、聚類系統,每個系統務必選擇不同的算法進行訓練,采用多種方法進行模型驗證與參數調優,選擇適合的多個指標對模型進行評估,采用可視化方法對結果進行分析。
(1)分類算法:
k-近鄰算法、貝葉斯分類器、決策樹分類、BP神經網絡、AdaBoost、GBDT、隨機森林、邏輯回歸等
(2)預測:貝葉斯網絡、馬爾科夫模型、線性回歸、XGBoost、嶺回歸、多項式回歸、決策樹回歸、深度神經網絡預測
(3)聚類:K-means、層次聚類BIRCH、密度聚類DBSCAN算法、高斯混合聚類GMM、密度聚類的OPTICS算法、基于網格的聚類(STING、CLIQUE)、Mean Shift聚類算法
其中:藍色標注的算法要求必須在問題中使用,紅色標注的為選用(至少選一種,多選加分),黑色的可不用,如用則有加分
4.要求
(1)所選用算法可直接調用Python中的相關庫函數實現,但要對其源碼進行分析,厘清算法結構及各部分功能。也可自行編寫相關算法,并與庫函數進行對比實驗
(2)數據集的選擇要分為小數據集、中等規模數據集、大規模數據集,數據集類型應有結構化、半結構化以及非結構化數據集。
(3)同一類算法中要實現各個算法在不同數據集、不同指標的比較
(4)算法設計中要有較詳細的注釋說明,對每個模塊給出詳細解釋、功能注釋等
接下來先看一下我們的RGB系統的界面(因為界面很丑純色圖設計的 所以稱為RGB系統)
- 主界面設置了四個Button,前三個分別進入一個子系統,最后一個退出系統
代碼:
main.py
import os import tkinter as tk import matplotlib.pyplot as plt plt.title("")def run_classfiy():os.system(r'python UI_classfiy.py')def run_cluster():os.system(r'python UI_Cluster.py')def run_forecast():os.system(r'python UI_forecast.py')window = tk.Tk() window.title("machine learning") window.geometry("300x400") # 窗口大小 var = tk.StringVar() tk.Label(window, text="請選擇要進行的操作", font=("微軟雅黑", 12)).pack() tk.Button(window, text="分類", font=("微軟雅黑", 12), width=15, height=2, command=lambda: run_classfiy()).pack() tk.Button(window, text="聚類", font=("微軟雅黑", 12), width=15, height=2, command=lambda: run_cluster()).pack() tk.Button(window, text="預測", font=("微軟雅黑", 12), width=15, height=2, command=lambda: run_forecast()).pack() tk.Button(window, text="退出", font=("微軟雅黑", 12), width=15, height=2, command=lambda: quit()).pack() window.mainloop() # 點擊時循環更新數據classfiy.py
# k-近鄰算法、 # 貝葉斯分類器、 # 決策樹分類、 # AdaBoost、 # GBDT、 # 隨機森林、 # 邏輯回歸、 import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from sklearn import tree from sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier, GradientBoostingRegressor from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score from sklearn.naive_bayes import GaussianNB from sklearn.neighbors import KNeighborsClassifier import numpy as npclass Classfiy(object):def __init__(self, x_train, y_train, x_test, y_test):self.x_train = x_trainself.y_train = y_trainself.x_test = x_testself.y_test = y_testself.KNN_pred, self.beyes_pred, self.DT_pred, self.AdaBoost_pred, self.RF_pred, self.LR_pred, self.GBDT_pred \= 0, 0, 0, 0, 0, 0, 0def KNN(self, k=5, p=2):knn = KNeighborsClassifier(n_neighbors=k, p=p, metric='minkowski')knn.fit(self.x_train, self.y_train)self.KNN_pred = knn.predict(self.x_test)self.save_pic("KNN", self.KNN_pred)def beyes(self):beyes = GaussianNB()beyes.fit(self.x_train, self.y_train)self.beyes_pred = beyes.predict(self.x_test)self.save_pic("beyes", self.beyes_pred)def DT(self):dt = tree.DecisionTreeClassifier(criterion="entropy")dt.fit(self.x_train, self.y_train)self.DT_pred = dt.predict(self.x_test)self.save_pic("DT", self.DT_pred)def AdaBoost(self, n_estimators=100):AB = AdaBoostClassifier(n_estimators=n_estimators)AB.fit(self.x_train, self.y_train)self.AdaBoost_pred = AB.predict(self.x_test)self.save_pic("AdaBoost", self.AdaBoost_pred)def RF(self):RF = RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=1, n_jobs=2)RF.fit(self.x_train, self.y_train)self.RF_pred = RF.predict(self.x_test)self.save_pic("RF", self.RF_pred)def LR(self):LR = LogisticRegression(solver='liblinear')LR.fit(self.x_train, self.y_train)self.LR_pred = LR.predict(self.x_test)self.save_pic("LR", self.LR_pred)def GBDT(self):GBDT = GradientBoostingRegressor()GBDT.fit(self.x_train, self.y_train)self.GBDT_pred = GBDT.predict(self.x_test)self.GBDT_pred = np.asarray(self.GBDT_pred, dtype=int)self.save_pic("GBDT", self.GBDT_pred)def Evaluation_indicators(self, stri, y_pred):return [stri,round(accuracy_score(self.y_test, y_pred), 3),round(precision_score(self.y_test, y_pred, average="macro"), 3),round(recall_score(self.y_test, y_pred, average="micro"), 3),round(f1_score(self.y_test, y_pred, average="weighted"), 3)]def save_pic(self, stri, y_pred):plt.title(stri)plt.scatter(self.x_test[:, 0], self.x_test[:, 1], c=y_pred)plt.savefig("image/"+stri+".png", dpi=55)Clusterer.py
# K-means、 # 層次聚類BIRCH、 # 密度聚類DBSCAN算法、 # 高斯混合聚類GMM、 # 密度聚類的OPTICS算法、 # Mean Shift聚類算法 import pandas as pd from matplotlib import pyplot as plt from sklearn.cluster import KMeans, Birch, DBSCAN, OPTICS, MeanShift from sklearn.mixture import GaussianMixture import sklearn from sklearn import metrics from sklearn.metrics import accuracy_score from sklearn.metrics import homogeneity_completeness_v_measure import numpy as np# 計算純度 def purity_score(y_true, y_pred):y_voted_labels = np.zeros(y_true.shape)labels = np.unique(y_true)ordered_labels = np.arange(labels.shape[0])for k in range(labels.shape[0]):y_true[y_true == labels[k]] = ordered_labels[k]labels = np.unique(y_true)bins = np.concatenate((labels, [np.max(labels) + 1]), axis=0)for cluster in np.unique(y_pred):hist, _ = np.histogram(y_true[y_pred == cluster], bins=bins)winner = np.argmax(hist)y_voted_labels[y_pred == cluster] = winnerreturn accuracy_score(y_true, y_voted_labels)class Cluser:def __init__(self, k, data, y_true):self.K = kself.data = dataself.y_true = y_trueself.kmeams_pred, self.birch_pred, self.dbscan_pred, self.gmm_pred, self.optics_pred, self.MS_pred = \0, 0, 0, 0, 0, 0def K_means(self):kmeans = KMeans(n_clusters=self.K)self.kmeams_pred = kmeans.fit_predict(self.data)self.save_pic("K_means", self.kmeams_pred)def BIRCH(self):model = Birch(n_clusters=self.K)self.birch_pred = model.fit_predict(self.data)self.save_pic("BIRCH", self.birch_pred)def DBSCAN(self):model = DBSCAN()self.dbscan_pred = model.fit_predict(self.data)self.save_pic("DBSCAN", self.dbscan_pred)def GMM(self):model = GaussianMixture(n_init=3)self.gmm_pred = model.fit_predict(self.data)self.save_pic("GMM", self.gmm_pred)def OPTICS(self):model = OPTICS()self.optics_pred = model.fit_predict(self.data)self.save_pic("OPTICS", self.optics_pred)def Mean_Shift(self):model = MeanShift()self.MS_pred = model.fit_predict(self.data)self.save_pic("Mean_Shift", self.MS_pred)def Evaluation_indicators(self, stri, y_pred):h_c_v = homogeneity_completeness_v_measure(self.y_true, y_pred)return [stri,round(purity_score(self.y_true, y_pred), 3),round(metrics.adjusted_rand_score(self.y_true, y_pred), 3),round(sklearn.metrics.f1_score(self.y_true, y_pred, average='micro'), 3),round(metrics.mutual_info_score(self.y_true, y_pred), 3),round(h_c_v[0], 3),round(h_c_v[1], 3),round(h_c_v[2], 3)]def save_pic(self, stri, y_pred):plt.title(stri)plt.scatter(self.data[:, 0], self.data[:, 1], c=y_pred)plt.savefig("image/"+stri+".png", dpi=55)forecast.py
# 貝葉斯網絡、 # 馬爾科夫模型、 # 線性回歸、 # XGBoost、 # 嶺回歸、 # 多項式回歸、 # 決策樹回歸、import numpy as np import xgboost from hmmlearn.hmm import GaussianHMM from matplotlib import pyplot as plt from sklearn import linear_model, metrics import sklearn.pipeline as pl import sklearn.preprocessing as sp import sklearn.linear_model as lm from sklearn.linear_model import LinearRegression, BayesianRidge from sklearn.tree import DecisionTreeRegressor# 貝葉斯,線性 def mape(y_true, y_pred):return np.mean(np.abs((y_pred - y_true) / y_true)) * 100def smape(y_true, y_pred):return 2.0 * np.mean(np.abs(y_pred - y_true) / (np.abs(y_pred) + np.abs(y_true))) * 100class Forecast(object):def __init__(self, x_train, y_train, x_test, y_test):self.x_train = x_trainself.y_train = y_trainself.x_test = x_testself.y_test = y_testself.xgb_pred, self.LR_pred, self.DT_pred, self.polynomial_pred, self.RidgeCv_pred, self.byes_pred, \self.markov_pred = 0, 0, 0, 0, 0, 0, 0# XGBoost、def XGBoost(self):bst = xgboost.XGBClassifier()bst.fit(self.x_train, self.y_train)self.xgb_pred = bst.predict(self.x_test)self.save_pic("XGBoost", self.xgb_pred)# 線性回歸、def LR(self):model = LinearRegression()model.fit(self.x_train, self.y_train)model.score(self.x_test, self.y_test)self.LR_pred = model.predict(self.x_test)self.save_pic("LR", self.LR_pred)# 決策樹回歸def DT(self):model = DecisionTreeRegressor(max_depth=5)model.fit(self.x_train, self.y_train)self.DT_pred = model.predict(self.x_test)self.save_pic("DT", self.DT_pred)# 多項式回歸def polynomial(self):model = pl.make_pipeline(sp.PolynomialFeatures(10), # 多項式特征擴展器lm.LinearRegression()) # 線性回歸器model.fit(self.x_train, self.y_train)self.polynomial_pred = model.predict(self.x_test)self.save_pic("polynomial", self.polynomial_pred)# 嶺回歸def RidgeCv(self):model = linear_model.RidgeCV()model.fit(self.x_train, self.y_train)model.score(self.x_test, self.y_test)self.RidgeCv_pred = model.predict(self.x_test)self.save_pic("RidgeCv", self.RidgeCv_pred)# 貝葉斯網絡、def byes(self):mnb = BayesianRidge() # 使用默認配置初始化樸素貝葉斯mnb.fit(self.x_train, self.y_train)self.byes_pred = mnb.predict(self.x_test)self.save_pic("byes", self.byes_pred)# 馬爾科夫模型、def Markov(self):model = GaussianHMM(n_components=3, covariance_type='diag', n_iter=1000)model.fit(self.x_train)self.markov_pred = model.predict(self.x_test)self.save_pic("Markov", self.markov_pred)def Evaluation_indicators(self, stri, y_pred):return [stri,round(metrics.mean_squared_error(self.y_test, y_pred), 3),round(np.sqrt(metrics.mean_squared_error(self.y_test, y_pred)), 3),round(metrics.mean_absolute_error(self.y_test, y_pred), 3),round(mape(self.y_test, y_pred), 3),round(smape(self.y_test, y_pred), 3)]def save_pic(self, stri, y_pred):plt.title(stri)plt.plot(np.arange(len(y_pred)), self.y_test, 'go-', label='test value')plt.plot(np.arange(len(y_pred)), y_pred, 'ro-', label='predict value')plt.savefig("image/" + stri + ".png", dpi=55)Button_classfiy.py
from classfiy import * import pandas as pd from sklearn.datasets import * from sklearn.model_selection import train_test_splitclass ButtonClassfiy(object):def __init__(self, ifSelect, dataOption, e_value):self.ifSelect = ifSelectself.dataOption = dataOptionself.e_value = e_valueself.data = 0self.target = 0self.x_train = 0self.y_train = 0self.x_test = 0self.y_test = 0self.result = []def get_data(self):if self.dataOption == "iris":self.data = load_iris()["data"]self.target = load_iris()["target"]elif self.dataOption == "wine_data":self.data = load_wine()["data"]self.target = load_wine()["target"]elif self.dataOption == "breast_cancer":self.data = load_breast_cancer()["data"]self.target = load_breast_cancer()["target"]else:self.data = pd.read_csv(self.e_value)self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(self.data, self.target, test_size=0.30, random_state=42)def run(self):clf = Classfiy(self.x_train, self.y_train, self.x_test, self.y_test)if self.ifSelect[0]:clf.KNN()self.result.append(clf.Evaluation_indicators("{:<6}".format("KNN"), clf.KNN_pred))if self.ifSelect[1]:clf.beyes()self.result.append(clf.Evaluation_indicators("{:<6}".format("貝葉斯分類器"), clf.beyes_pred))if self.ifSelect[2]:clf.DT()self.result.append(clf.Evaluation_indicators("{:<6}".format("決策樹"), clf.DT_pred))if self.ifSelect[3]:clf.AdaBoost()self.result.append(clf.Evaluation_indicators("{:<6}".format("AdaBoost"), clf.AdaBoost_pred))if self.ifSelect[4]:clf.GBDT()self.result.append(clf.Evaluation_indicators("{:<6}".format("GBDT"), clf.GBDT_pred))if self.ifSelect[5]:clf.RF()self.result.append(clf.Evaluation_indicators("{:<6}".format("隨機森林"), clf.RF_pred))if self.ifSelect[6]:clf.LR()self.result.append(clf.Evaluation_indicators("{:<6}".format("邏輯回歸"), clf.LR_pred))return self.resultButton_cluster.py
from Clusterer import * from sklearn.datasets import * from sklearn.model_selection import train_test_splitclass ButtonCluster(object):def __init__(self, ifSelect, dataOption, e_value):self.ifSelect = ifSelectself.dataOption = dataOptionself.e_value = e_valueself.data = 0self.target = 0self.x_train = 0self.y_train = 0self.x_test = 0self.y_test = 0self.result = []def get_data(self):if self.dataOption == "iris":self.data = load_iris()["data"]self.target = load_iris()["target"]elif self.dataOption == "wine_data":self.data = load_wine()["data"]self.target = load_wine()["target"]elif self.dataOption == "breast_cancer":self.data = load_breast_cancer()["data"]self.target = load_breast_cancer()["target"]else:self.data = pd.read_csv(self.e_value)self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(self.data, self.target, test_size=0.30, random_state=42)def run(self):clf = Cluser(3, self.x_train, self.y_train)if self.ifSelect[0]:clf.K_means()self.result.append(clf.Evaluation_indicators("{:<6}".format("K-means"), clf.kmeams_pred))if self.ifSelect[1]:clf.BIRCH()self.result.append(clf.Evaluation_indicators("{:<6}".format("BIRCH"), clf.birch_pred))if self.ifSelect[2]:clf.DBSCAN()self.result.append(clf.Evaluation_indicators("{:<6}".format("DBSCAN"), clf.dbscan_pred))if self.ifSelect[3]:clf.GMM()self.result.append(clf.Evaluation_indicators("{:<6}".format("GMM"), clf.gmm_pred))if self.ifSelect[4]:clf.OPTICS()self.result.append(clf.Evaluation_indicators("{:<6}".format("OPTICS"), clf.optics_pred))if self.ifSelect[5]:clf.Mean_Shift()self.result.append(clf.Evaluation_indicators("{:<6}".format("Mean_Shift"), clf.MS_pred))return self.resultButton_forecast.py
from forecast import * from sklearn.datasets import * from sklearn.model_selection import train_test_splitclass ButtonForecast(object):def __init__(self, ifSelect, dataOption, e_value):self.ifSelect = ifSelectself.dataOption = dataOptionself.e_value = e_valueself.data = 0self.target = 0self.x_train = 0self.y_train = 0self.x_test = 0self.y_test = 0self.result = []def get_data(self):if self.dataOption == "iris":self.data = load_iris()["data"]self.target = load_iris()["target"]elif self.dataOption == "wine_data":self.data = load_boston()["data"]self.target = load_boston()["target"]elif self.dataOption == "breast_cancer":self.data = load_boston()["data"]self.target = load_boston()["target"]else:self.data = pd.read_csv(self.e_value)self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(self.data, self.target, test_size=0.30, random_state=42)# 貝葉斯網絡、# 馬爾科夫模型、# 線性回歸、# XGBoost、# 嶺回歸、# 多項式回歸、# 決策樹回歸、def run(self):clf = Forecast(self.x_train, self.y_train, self.x_test, self.y_test)if self.ifSelect[0]:clf.byes()self.result.append(clf.Evaluation_indicators("{:<6}".format("貝葉斯網絡"), clf.byes_pred))if self.ifSelect[1]:clf.Markov()self.result.append(clf.Evaluation_indicators("{:<6}".format("馬爾科夫模型"), clf.markov_pred))if self.ifSelect[2]:clf.LR()self.result.append(clf.Evaluation_indicators("{:<6}".format("線性回歸"), clf.LR()))if self.ifSelect[3]:clf.XGBoost()self.result.append(clf.Evaluation_indicators("{:<6}".format("XGBoost"), clf.xgb_pred))if self.ifSelect[4]:clf.RidgeCv()self.result.append(clf.Evaluation_indicators("{:<6}".format("嶺回歸"), clf.RidgeCv_pred))if self.ifSelect[5]:clf.polynomial()self.result.append(clf.Evaluation_indicators("{:<6}".format("多項式回歸"), clf.polynomial_pred))if self.ifSelect[6]:clf.DT()self.result.append(clf.Evaluation_indicators("{:<6}".format("決策樹回歸"), clf.DT_pred))return self.resultUI_classfiy.py
import tkinter as tk from tkinter import * from Button_classfiy import ButtonClassfiy import tkinter.messageboxwindow = tk.Tk() window.title("machine learning") window.geometry("800x800+50+50") # 窗口大小 data_option = StringVar() data_option.set("iris") ifSelect = {} diy_label = Label()# 窗口布局 select = tk.Frame(window, height=450, width=250, bg='green').place(x=10, y=50) data = tk.Frame(window, height=250, width=250, bg='red').place(x=10, y=530) text = Text(window, width=62, height=10) text.place(x=320, y=50) # 提示語 ChooseLabel = tk.Label(window, text="Please select a classification algorithm", font=('微軟雅黑', 12)).place(x=10, y=10) resultLabel = tk.Label(window, text="Results of the selected algorithm classification",font=('微軟雅黑', 12)).place(x=330, y=10) e = tk.Entry(data)canvas = tk.Canvas(window, height=260, width=440) image_flie = tk.PhotoImage(file="image/1.png") image = canvas.create_image(0, 0, anchor="nw", image=image_flie) canvas.place(x=320, y=230) pic_names = ["1.png"] pic_name = pic_names[0] pic_index = 0# 切換圖片 def swicth_pic(pic_name1):global image, image_flie, pic_index, pic_names, pic_nameimage_flie = tk.PhotoImage(file='image/'+pic_name1)image = canvas.create_image(0, 0, anchor="nw", image=image_flie)pic_index += 1if pic_index >= len(pic_names):pic_index = 0pic_name = pic_names[pic_index]Button(window, text="next pic", font=('微軟雅黑', 8), command=lambda: swicth_pic(pic_name), bg='gray').place(x=650, y=200)# 自定義文件路徑 def diy_data():global diy_label# 用戶自己輸入數據diy_label = Label(data, text="請輸入數據路徑:", font=('微軟雅黑', 10), bg="red")diy_label.place(x=10, y=700)e.place(x=110, y=703) # 若要顯示 則show=Nonedef delete_diy():global diy_labeldiy_label.place_forget()e.place_forget()def reflush():global ifSelect, data_option# 選擇算法復選框algorithm = {0: 'k-近鄰算法', 1: '貝葉斯分類器', 2: '決策樹分類', 3: 'AdaBoost', 4: 'GBDT', 5: '隨機森林', 6: '邏輯回歸'}# 判斷是否選擇for i in range(len(algorithm)):ifSelect[i] = BooleanVar()Checkbutton(select, text=algorithm[i], font=('微軟雅黑', 12), variable=ifSelect[i], bg='green') \.place(x=30, y=80 + i * 55, anchor="nw")# 數據層# 設置單選層,內置小數據、中數據、大數據tk.Radiobutton(window, text="小數據", variable=data_option, value="iris", bg='red', command=delete_diy) \.place(x=30, y=550)tk.Radiobutton(window, text="中數據", variable=data_option, value="wine_data", bg='red', command=delete_diy) \.place(x=30, y=580)tk.Radiobutton(window, text="大數據", variable=data_option, value="breast_cancer", bg="red", command=delete_diy) \.place(x=30, y=610)tk.Radiobutton(window, text="diy數據", variable=data_option, value="diy", command=diy_data, bg='red') \.place(x=30, y=640)reflush()# 到這里我們所需要的數據都可以拿到了 # 這部分我們加到運行命令下面def btn_f():global pic_names, pic_namefor i in range(7):ifSelect[i] = ifSelect[i].get()f = 1for i in range(7):if ifSelect[i]:f = 0if f:tkinter.messagebox.showerror("錯誤", "你沒有選擇任何算法")pic_names = ["image/1.png"]quit()e_value = e.get()pic_names = []for i in range(7):if ifSelect[i]:pic_names.append(["KNN.png", "beyes.png", "DT.png", "AdaBoost.png", "GBDT.png", "RF.png", "LR.png"][i])pic_name = pic_names[0]# 進行計算btnf = ButtonClassfiy(ifSelect, data_option.get(), e_value)btnf.get_data()result = btnf.run()show_ = "算法名稱 精確率 準確率 召回率 f1-score\n"for i in result:show_ = show_ + str(i) + '\n'text.delete('1.0', 'end')text.insert(INSERT, show_, "軟體雅黑")reflush()# 運行按鈕Button(window, text="run it", font=('微軟雅黑', 80), command=lambda: btn_f(), bg='gray').place(x=350, y=530) window.mainloop() # 點擊時循環更新數據UI_Cluster.py
import tkinter as tk from tkinter import * from Button_cluster import ButtonCluster import tkinter.messageboxwindow = tk.Tk() window.title("machine learning") window.geometry("800x800+50+50") # 窗口大小 data_option = StringVar() data_option.set("iris") ifSelect = {} diy_label = Label()# 窗口布局 select = tk.Frame(window, height=450, width=250, bg='green').place(x=10, y=50) data = tk.Frame(window, height=250, width=250, bg='red').place(x=10, y=530) text = Text(window, width=62, height=10) text.place(x=320, y=50) # 提示語 ChooseLabel = tk.Label(window, text="Please select a clustering algorithm", font=('微軟雅黑', 12)).place(x=10, y=10) resultLabel = tk.Label(window, text="The clustering results are as follows",font=('微軟雅黑', 12)).place(x=330, y=10) e = tk.Entry(data) # 畫布 canvas = tk.Canvas(window, height=260, width=440) image_flie = tk.PhotoImage(file="image/1.png") image = canvas.create_image(0, 0, anchor="nw", image=image_flie) canvas.place(x=320, y=230) pic_names = ["1.png"] pic_name = pic_names[0] pic_index = 0# 切換圖片 def swicth_pic(pic_name1):global image, image_flie, pic_index, pic_names, pic_nameimage_flie = tk.PhotoImage(file='image/'+pic_name1)image = canvas.create_image(0, 0, anchor="nw", image=image_flie)pic_index += 1if pic_index >= len(pic_names):pic_index = 0pic_name = pic_names[pic_index]Button(window, text="next pic", font=('微軟雅黑', 8), command=lambda: swicth_pic(pic_name), bg='gray').place(x=650, y=200)def diy_data():global diy_label# 用戶自己輸入數據diy_label = Label(data, text="請輸入數據路徑:", font=('微軟雅黑', 10), bg="red")diy_label.place(x=10, y=700)e.place(x=110, y=703) # 若要顯示 則show=Nonedef delete_diy():global diy_labeldiy_label.place_forget()e.place_forget()def reflush():global ifSelect, data_option# 選擇算法復選框algorithm = {0: 'K-means', 1: 'BIRCH', 2: 'DBSCAN', 3: 'GMM', 4: 'OPTICS', 5: 'Mean Shift'}# 判斷是否選擇for i in range(len(algorithm)):ifSelect[i] = BooleanVar()Checkbutton(select, text=algorithm[i], font=('微軟雅黑', 12), variable=ifSelect[i], bg='green') \.place(x=30, y=80 + i * 55, anchor="nw")# 數據層# 設置單選層,內置小數據、中數據、大數據tk.Radiobutton(window, text="小數據", variable=data_option, value="iris", bg='red', command=delete_diy) \.place(x=30, y=550)tk.Radiobutton(window, text="中數據", variable=data_option, value="wine_data", bg='red', command=delete_diy) \.place(x=30, y=580)tk.Radiobutton(window, text="大數據", variable=data_option, value="breast_cancer", bg="red", command=delete_diy) \.place(x=30, y=610)tk.Radiobutton(window, text="diy數據", variable=data_option, value="diy", command=diy_data, bg='red') \.place(x=30, y=640)reflush()# 到這里我們所需要的數據都可以拿到了 # 這部分我們加到運行命令下面def btn_f():global pic_names, pic_namefor i in range(6):ifSelect[i] = ifSelect[i].get()f = 1for i in range(6):if ifSelect[i]:f = 0if f:tkinter.messagebox.showerror("錯誤", "你沒有選擇任何算法")pic_names = ["image/1.png"]quit()e_value = e.get()pic_names = []for i in range(6):if ifSelect[i]:pic_names.append(["K_means.png", "BIRCH.png", "DBSCAN.png", "GMM.png", "OPTICS.png", "Mean_Shift.png"][i])pic_name = pic_names[0]# 進行計算btnf = ButtonCluster(ifSelect, data_option.get(), e_value)btnf.get_data()result = btnf.run()show_ = "算法名稱 純度 調整蘭德系數 f1-score 互信息 同質性 完整性 調和平均\n"for i in result:show_ = show_ + str(i) + '\n'text.delete('1.0', 'end')text.insert(INSERT, show_, "軟體雅黑")reflush()# 運行按鈕Button(window, text="run it", font=('微軟雅黑', 80), command=lambda: btn_f(), bg='gray').place(x=350, y=530) window.mainloop() # 點擊時循環更新數據UI_forecast.py
import tkinter as tk from tkinter import * from Button_forecast import ButtonForecast import tkinter.messageboxwindow = tk.Tk() window.title("machine learning") window.geometry("800x800+50+50") # 窗口大小 data_option = StringVar() data_option.set("iris") ifSelect = {} diy_label = Label()# 窗口布局 select = tk.Frame(window, height=450, width=250, bg='green').place(x=10, y=50) data = tk.Frame(window, height=250, width=250, bg='red').place(x=10, y=530) text = Text(window, width=62, height=10) text.place(x=320, y=50) # 提示語 ChooseLabel = tk.Label(window, text="Please select a prediction algorithm", font=('微軟雅黑', 12)).place(x=10, y=10) resultLabel = tk.Label(window, text="The predicted results are as follows", font=('微軟雅黑', 12)).place(x=330, y=10) e = tk.Entry(data)canvas = tk.Canvas(window, height=260, width=440) image_flie = tk.PhotoImage(file="image/1.png") image = canvas.create_image(0, 0, anchor="nw", image=image_flie) canvas.place(x=320, y=230) pic_names = ["image/1.png"] pic_name = pic_names[0] pic_index = 0# 切換圖片 def swicth_pic(pic_name1):global image, image_flie, pic_index, pic_names, pic_nameimage_flie = tk.PhotoImage(file='image/' + pic_name1)image = canvas.create_image(0, 0, anchor="nw", image=image_flie)pic_index += 1if pic_index >= len(pic_names):pic_index = 0pic_name = pic_names[pic_index]Button(window, text="next pic", font=('微軟雅黑', 8), command=lambda: swicth_pic(pic_name), bg='gray').place(x=650, y=200)def diy_data():global diy_label# 用戶自己輸入數據diy_label = Label(data, text="請輸入數據路徑:", font=('微軟雅黑', 10), bg="red")diy_label.place(x=10, y=700)e.place(x=110, y=703) # 若要顯示 則show=Nonedef delete_diy():global diy_labeldiy_label.place_forget()e.place_forget()def reflush():global ifSelect, data_option# 選擇算法復選框algorithm = {0: '貝葉斯網絡', 1: '馬爾科夫模型', 2: '線性回歸', 3: 'XGBoost', 4: '嶺回歸', 5: '多項式回歸', 6: '決策樹回歸'}# 判斷是否選擇for i in range(len(algorithm)):ifSelect[i] = BooleanVar()Checkbutton(select, text=algorithm[i], font=('微軟雅黑', 12), variable=ifSelect[i], bg='green') \.place(x=30, y=80 + i * 55, anchor="nw")# 數據層# 設置單選層,內置小數據、中數據、大數據tk.Radiobutton(window, text="小數據", variable=data_option, value="iris", bg='red', command=delete_diy) \.place(x=30, y=550)tk.Radiobutton(window, text="中數據", variable=data_option, value="wine_data", bg='red', command=delete_diy) \.place(x=30, y=580)tk.Radiobutton(window, text="大數據", variable=data_option, value="breast_cancer", bg="red", command=delete_diy) \.place(x=30, y=610)tk.Radiobutton(window, text="diy數據", variable=data_option, value="diy", command=diy_data, bg='red') \.place(x=30, y=640)reflush()# 到這里我們所需要的數據都可以拿到了 # 這部分我們加到運行命令下面def btn_f():global pic_names, pic_namefor i in range(7):ifSelect[i] = ifSelect[i].get()f = 1for i in range(7):if ifSelect[i]:f = 0if f:tkinter.messagebox.showerror("錯誤", "你沒有選擇任何算法")pic_names = ["image/1.png"]quit()e_value = e.get()pic_names = []for i in range(7):if ifSelect[i]:pic_names.append(["byes.png", "Markov.png", "LR.png", "XGBoost.png", "RidgeCv.png", "polynomial.png","DT.png"][i])pic_name = pic_names[0]# 進行計算btnf = ButtonForecast(ifSelect, data_option.get(), e_value)btnf.get_data()result = btnf.run()show_ = "算法名稱 MSE RMSE MAE MAPE SMAPE\n"for i in result:show_ = show_ + str(i) + '\n'text.delete('1.0', 'end')text.insert(INSERT, show_, "軟體雅黑")reflush()# 運行按鈕 Button(window, text="run it", font=('微軟雅黑', 80), command=lambda: btn_f(), bg='gray').place(x=350, y=530) window.mainloop() # 點擊時循環更新數據總結
以上是生活随笔為你收集整理的大三课设-分类聚类预测系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字库字符编码
- 下一篇: asp毕业设计——基于asp+sqlse