Complete Tutorial on Tkinter To Deploy Machine Learning Model
In machine learning, while building a predictive model we follow several different steps. We first do exploratory data analysis to understand the data well and do the required preprocessing. After the data gets ready we do modelling and develop a predictive model. This model is then used to compute prediction on the testing data and the results are evaluated using different error metrics. But what to do next? Do you know how you can use this model and check real-time predictions? It is said you can validate the model performance when you compute prediction in real-time. As we have already seen how we can do model deployment using flask. In this article, we will be exploring Tkinter – python GUI programming tool. We will explore how we can deploy a machine learning model and check real-time predictions using Tkinter. For this experiment, we will be using the Pima Indians Diabetes Data set that is available on Kaggle. We will first build a classification model that will classify whether a patient is diabetic or not. Then we will make a GUI using Tkinter and will check predictions on new data points. What you will learn from this? What is Tkinter? How is it used to make GUI? How to build machine learning models? How to Compute Predictions using the Tkinter GUI in real-time? What is Tkinter? How is it used to make GUI? Tkinter is a library written in Python that is widely used to create GUI applications. It is very easy to build GUI using Tkinter and the process is even faster. Tkinter has several widgets that can be used while developing GUI. These include buttons, radio buttons, checkboxes, etc. We will see how we can make a GUI Tkinter after we build the machine learning model later in the article. How to Build Machine Learning Models? Now we will build the classification model for classifying the patient as diabetic or not. We will quickly import all the libraries that are required and the data set. Use the below code for the same. import pandas as pd df = pd.read_csv('pima.csv') print(df) There are a total of 768 rows and 9 columns in the data set. There were no missing values found in the data set. Now we will create independent and dependent variables. After creating these we will split the data into training and testing sets. We will fit the training data over the model and will compute prediction over the testing data. Refer to the below code for the same. X = df.drop('class', axis = 1) y = df[['class']] from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) print(X_train.shape) print(X_test.shape) There are a total of 514 rows in the training and 254 are present in the testing set. We will now build the classification model. We are using Logistic regression for the same.  Once we have built the model we will feed the training data and will compute predictions for testing data. Use the below code for the same. from sklearn.linear_model import LogisticRegression lr = LogisticRegression() model = lr.fit(X_train,y_train) y_pred = lr.predict(X_test) We have stored the prediction on the testing data in y_pred variable. Let us check the predictions. print(y_pred) Now will evaluate the predictions using metrics accuracy score. Check the below code for the same. print(accuracy_score(y_pred,y_test)) We will now pickle this model that would be used to compute predictions for new data points. There are several other methods also available to pickle the model like Joblib. Refer to the below code for pickling the model. import pickle Model = pickle.dumps(model) How to Compute Predictions Using the Tkinter GUI in Real-Time? Since we are now done with pickling the file. Now we will create a GUI using Tkinter that will be used to capture new data points. Use the below code for the same. We will first define the library and then will make the GUI. import tkinter as tk from tkinter import ttk win = tk.Tk() win.title('Diabetes Predictions') We have first created a tkinter window and given the title as “Diabetic Predictions”. Now we will create the labels (features). Use the below code for the same. #Column 1 Preg=ttk.Label(win,text="Preg") Preg.grid(row=0,column=0,sticky=tk.W) Preg_var=tk.StringVar() Preg_entrybox=ttk.Entry(win,width=16,textvariable=Preg_var) Preg_entrybox.grid(row=0,column=1) #Column 2 Plas=ttk.Label(win,text="Plas") Plas.grid(row=1,column=0,sticky=tk.W) Plas_var=tk.StringVar() Plas_entrybox=ttk.Entry(win,width=16,textvariable=Plas_var) Plas_entrybox.grid(row=1,column=1) #Column 3 Pres=ttk.Label(win,text="Pres") Pres.grid(row=2,column=0,sticky=tk.W) Pres_var=tk.StringVar() Pres_entrybox=ttk.Entry(win,width=16,textvariable=Pres_var) Pres_entrybox.grid(row=2,column=1) #Column 4 skin=ttk.Label(win,text="skin") skin.grid(row=3,column=0,sticky=tk.W) skin_var=tk.StringVar() skin_entrybox=ttk.Entry(win,width=16,textvariable=skin_var) skin_entrybox.grid(row=3,column=1) #Column 5 test=ttk.Label(win,text="test") test.grid(row=4,column=0,sticky=tk.W) test_var=tk.StringVar() test_entrybox=ttk.Entry(win,width=16,textvariable=test_var) test_entrybox.grid(row=4,column=1) #Column 6 mass=ttk.Label(win,text="mass") mass.grid(row=5,column=0,sticky=tk.W) mass_var=tk.StringVar() mass_entrybox=ttk.Entry(win,width=16,textvariable=mass_var) mass_entrybox.grid(row=5,column=1) #Column 7 pedi=ttk.Label(win,text="pedi") pedi.grid(row=6,column=0,sticky=tk.W) pedi_var=tk.StringVar() pedi_entrybox=ttk.Entry(win,width=16,textvariable=pedi_var) pedi_entrybox.grid(row=6,column=1) #Column 8 age=ttk.Label(win,text="age") age.grid(row=7,column=0,sticky=tk.W) age_var=tk.StringVar() age_entrybox=ttk.Entry(win,width=16,textvariable=age_var) age_entrybox.grid(row=7,column=1) We have now created all the buttons that are mainly the features that will store the new data point values. When we have to compute prediction using any model we need a data frame on which we have to make the prediction. Once a user enters a different set of values we then have to create a data frame of it. Use the below code for the same. import pandas as pd DF = pd.DataFrame() def action(): global DB import pandas as pd DF = pd.DataFrame(columns=['Preg','Plas','Pres','skin','test','mass','pedi','age']) PREG=Preg_var.get() DF.loc[0,'Preg']=PREG PLAS=Plas_var.get() DF.loc[0,'Plas']=PLAS PRES=Pres_var.get() DF.loc[0,'Pres']=PRES SKIN=skin_var.get() DF.loc[0,'skin']=SKIN TEST=test_var.get() DF.loc[0,'test']=TEST MASS=mass_var.get() DF.loc[0,'mass']=MASS PEDI=pedi_var.get() DF.loc[0,'pedi']=PEDI AGE=age_var.get() DF.loc[0,'age']=AGE print(DF.shape) DB=DF Now we will create a submit button. Once this button is clicked the above action of data frame creation is done. We will once again convert every value that is inputted by the user to numerics. Use the below code for the same. def Output(): DB["Preg"] = pd.to_numeric(DB["Preg"]) DB["Plas"] = pd.to_numeric(DB["Plas"]) DB["Pres"] = pd.to_numeric(DB["Pres"]) DB["skin"] = pd.to_numeric(DB["skin"]) DB["test"] = pd.to_numeric(DB["test"]) DB["mass"] = pd.to_numeric(DB["mass"]) DB["pedi"] = pd.to_numeric(DB["pedi"]) DB["age"] = pd.to_numeric(DB["age"]) Now once we are done with this we will make use of the pickle file and compute prediction over this data frame. If the prediction comes out to be 1 then it will revert “Diabetic” and if it’s 0 then it will revert “Non-Diabetic”. Use the below code for the same. output=model.predict(DB) if output==1: result='Diabetic' elif output==0: result='Non-Diabetic' Now we will create a box that will display the output. (Diabetic/ Non-Diabetic). Once the predict button is clicked the model will predict the class and this prediction will be displayed in this box. Predict_entrybox=ttk.Entry(win,width=16) Predict_entrybox.grid(row=20,column=1) Predict_entrybox.insert(1,str(result)) Predict_button=ttk.Button(win,text="Predict",command=Output) Predict_button.grid(row=20,column=0) win.mainloop() Now we are ready to execute the GUI. Try running the entire code in one cell to get rid of errors. Once you run the cell a new window will open that will show you the GUI. Now we will enter the random values and check the prediction. We have entered the values for the features now we will click on submit to create the data frame and right after that we will click on the Predict button to check the prediction. Conclusion In this article, we discussed how to make a GUI using Tkinter. We explored by first building a classification model over Pima Diabetic Data then and pickling the model weights. We then designed a GUI and then computed prediction for randomly chosen data. The model that was built only gave 75% accuracy. We did not make any efforts to improve the accuracy since we wanted to learn more about predictions in real-time whereas the approach is to finalize the best performing model and pickling it. If you want to know more about how you can deploy a machine learning model using Flask, check this article title as “Hands-On-Guide To Machine Learning Model Deployment Using Flask”.
In this article, we will be exploring Tkinter – python GUI programming tool. We will explore how we can deploy a machine learning model and check real-time predictions using Tkinter.
In this article, we will explore how we can deploy a machine learning model and check real-time predictions using Tkinter.
["Machine Learning", "machine learning methods", "model deployment"]
["data_tools:Pandas", "machine learning", "TPU", "programming_languages:R", "AI", "ML", "model deployment", "Machine Learning", "Python", "programming_languages:Python", "machine learning methods", "R", "Pandas"]
["AI", "machine learning", "ML", "Pandas", "TPU", "Python", "R", "programming_languages:Python", "programming_languages:R", "data_tools:Pandas"]
I am currently enrolled in a Post Graduate Program In Artificial Intelligence and Machine learning. Data Science Enthusiast who likes to draw insights from the data. Always amazed with the intelligence of AI. It's really fascinating teaching a machine to see and understand images. Also, the interest gets doubled when the machine can tell you what it just saw. This is where I say I am highly interested in Computer Vision and Natural Language Processing. I love exploring different use cases that can be build with the power of AI. I am the person who first develops something and then explains it to the whole community with my writings.