Machine Learning Tool with GUI - Linear Regression, Logistic Regression, Gaussian Naive Bayes
#Creating the GUI root = tk.Tk() root.geometry('700x500') root.title('Machine Learning Tool')
#Creating the file upload function def upload_file(): global df file_path = filedialog.askopenfilename() df = pd.read_csv(file_path) st.insert(tk.INSERT, 'File uploaded successfully! ') st.insert(tk.INSERT, 'Please select the type of model you want to build ')
#Creating the function for linear regression def linear_reg(): global X_train, X_test, y_train, y_test st.delete('1.0', END) st.insert(tk.INSERT, 'Linear Regression Model
') X = df.drop('target_variable', axis=1) y = df['target_variable'] X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=42) lr = LinearRegression() lr.fit(X_train, y_train) y_pred = lr.predict(X_test) mse = mean_squared_error(y_test, y_pred) st.insert(tk.INSERT, 'Mean Squared Error: {} '.format(mse)) st.insert(tk.INSERT, 'Model Accuracy: {} '.format(lr.score(X_test, y_test))) st.insert(tk.INSERT, 'Model Coefficients: {} '.format(lr.coef_))
#Creating the function for logistic regression def logistic_reg(): global X_train, X_test, y_train, y_test st.delete('1.0', END) st.insert(tk.INSERT, 'Logistic Regression Model
') X = df.drop('target_variable', axis=1) y = df['target_variable'] le = LabelEncoder() y = le.fit_transform(y) X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=42) sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) lr = LogisticRegression() lr.fit(X_train, y_train) y_pred = lr.predict(X_test) acc = accuracy_score(y_test, y_pred) st.insert(tk.INSERT, 'Model Accuracy: {} '.format(acc))
#Creating the function for Gaussian Naive Bayes def gaussian_nb(): global X_train, X_test, y_train, y_test st.delete('1.0', END) st.insert(tk.INSERT, 'Gaussian Naive Bayes Model
') X = df.drop('target_variable', axis=1) y = df['target_variable'] le = LabelEncoder() y = le.fit_transform(y) X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=42) sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) nb = GaussianNB() nb.fit(X_train, y_train) y_pred = nb.predict(X_test) acc = accuracy_score(y_test, y_pred) st.insert(tk.INSERT, 'Model Accuracy: {} '.format(acc))
#Creating the function for plotting the linear regression model def plot_linear_reg(): global X_train, X_test, y_train, y_test st.delete('1.0', END) st.insert(tk.INSERT, 'Linear Regression Model Plot
') X = df.drop('target_variable', axis=1) y = df['target_variable'] X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=42) lr = LinearRegression() lr.fit(X_train, y_train) y_pred = lr.predict(X_test) plt.scatter(X_test, y_test, color='blue') plt.plot(X_test, y_pred, color='red') plt.title('Linear Regression Model') plt.xlabel('Independent Variable') plt.ylabel('Target Variable') plt.show()
#Creating the function for plotting the logistic regression model def plot_logistic_reg(): global X_train, X_test, y_train, y_test st.delete('1.0', END) st.insert(tk.INSERT, 'Logistic Regression Model Plot
') X = df.drop('target_variable', axis=1) y = df['target_variable'] le = LabelEncoder() y = le.fit_transform(y) X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=42) sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) lr = LogisticRegression() lr.fit(X_train, y_train) y_pred = lr.predict(X_test) plt.scatter(X_test[:,0], X_test[:,1], c=y_test, cmap='winter') plt.xlabel('Independent Variable 1') plt.ylabel('Independent Variable 2') plt.title('Logistic Regression Model') plt.show()
#Creating the function for plotting the Gaussian Naive Bayes model def plot_gaussian_nb(): global X_train, X_test, y_train, y_test st.delete('1.0', END) st.insert(tk.INSERT, 'Gaussian Naive Bayes Model Plot
') X = df.drop('target_variable', axis=1) y = df['target_variable'] le = LabelEncoder() y = le.fit_transform(y) X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=42) sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) nb = GaussianNB() nb.fit(X_train, y_train) y_pred = nb.predict(X_test) plt.scatter(X_test[:,0], X_test[:,1], c=y_test, cmap='winter') plt.xlabel('Independent Variable 1') plt.ylabel('Independent Variable 2') plt.title('Gaussian Naive Bayes Model') plt.show()
#Creating the file upload button upload_button = tk.Button(root, text='Upload File', command=upload_file) upload_button.pack(pady=10)
#Creating the button for linear regression lr_button = tk.Button(root, text='Linear Regression', command=linear_reg) lr_button.pack(pady=10)
#Creating the button for logistic regression logistic_button = tk.Button(root, text='Logistic Regression', command=logistic_reg) logistic_button.pack(pady=10)
#Creating the button for Gaussian Naive Bayes nb_button = tk.Button(root, text='Gaussian Naive Bayes', command=gaussian_nb) nb_button.pack(pady=10)
#Creating the button for plotting the linear regression model plot_lr_button = tk.Button(root, text='Plot Linear Regression Model', command=plot_linear_reg) plot_lr_button.pack(pady=10)
#Creating the button for plotting the logistic regression model plot_logistic_button = tk.Button(root, text='Plot Logistic Regression Model', command=plot_logistic_reg) plot_logistic_button.pack(pady=10)
#Creating the button for plotting the Gaussian Naive Bayes model plot_nb_button = tk.Button(root, text='Plot Gaussian Naive Bayes Model', command=plot_gaussian_nb) plot_nb_button.pack(pady=10)
#Creating the text area for displaying the results st = scrolledtext.ScrolledText(root, width=60, height=10) st.pack(pady=10)
root.mainloop()
原文地址: https://www.cveoy.top/t/topic/oIgG 著作权归作者所有。请勿转载和采集!