How to Build a Machine Learning Model Performance Calculator App
Key Takeaways
This video demonstrates how to build a machine learning model performance calculator app using Streamlit, pandas, and scikit-learn, allowing users to calculate performance metrics such as accuracy, precision, and recall.
Full Transcript
do you happen to have a set of actual and predicted values lying around if you answered yes then this video is for you because today we're going to be building a machine learning model performance calculator app in python using the streamlit library so what the web app will do is it will take in the actual and predicted values and it will produce the model performance metrics such as accuracy math correlation coefficient and many others and so without further ado let's dive in and so before we continue with the explanation let me launch the application so that you can see so we're launching this performance metrics application and so let me fire up the terminal and then i'm going to activate my condy environment and so you could do the same don't activate data professor because this is my contact environment so depending on the name of your contact environment you want to activate that and now we're going to the folder which contains the app and then we're going to launch it by typing in streamlid run app.py enter and then this is the app let's have a look at the sidebar and so it will take in the input file here let me show you so the input file will be a b example let me show you what is the content of that and so it's going to be a simple classification model and in this example we have the actual column and the predicted column so you're going to see that the actual value is a and the predicted value is a so this is a correct prediction whereas a b here is a incorrect prediction a a is correct prediction bb are correct predictions ba are incorrect predictions and so this is our input so let me drag and drop it here and then you're going to see that it loads up the data in the input data section and it computes the confusion matrix for you and it is going to have the performance metrics for you and so all of the performance metrics indicated in here and the sidebar panel are displayed in the data frame and so you could feel free to customize take away some of the metrics and then the data frame will be updated in real time and so for this example we're going to build the model performance calculator app and it will be for classification models and so you could essentially do the same thing and create a version for the regression model and so i'll leave that up to you guys for your assignments and please feel free to share the links to your deployed application and so let's get into the line by line explanation of the code so let me launch the app.py file i'll have it open in atom and let's have a look okay so let's see how many lines of code are there so there are exactly 100 lines of code 100 lines of code exactly and so the first couple of lines will be importing the necessary libraries and we're using the streamlit library because this is based on the streamlet web application framework and we're going to use also the pandas library because we display the data frame here and we also make use of the pandas data frame we're going to use the base64 library because we're encoding the information which will be allowing us to download the csv file as the export file for the model performance metric and then we're going to use the image function from the pill library because we're going to display the logo here at the top and then we're going to use the scikit-learn metrics in order to import the confusion matrix function which will allow us to display the confusion matrix and we're also going to import all of the performance metrics function here as you can see from the stlearn.metrics module as well and then you're going to see that lines 9 through 30 we're defining a custom function which will calculate the performance metrics used in the data frame here and so these include the accuracy actually we took away the accuracy let me refresh it so it includes the accuracy the balance accuracy the position the recall the mcc f1 score and the coenz kappa and so they're defined here so each of the performance metrics function are used and then they're defined into the corresponding variable names and in the first two lines here line number 10 and 11 we're essentially going to be taking the input value the actual and the predicted columns which is columns one and columns two and then we're going to use it to calculate the accuracy and all of the other performance metrics finally we're going to take the values of the performance metrics and then we're going to convert it into a pandas series and then assign it to the corresponding variable names finally we're going to use the pd.contact in order to combine all of the series variable so that we have the data frame that we see at the bottom here the data frame that you see here and then we're going to return the data frame out okay and so we're going to use this function later on and lines number 33 until 38 we're going to calculate the confusion matrix which you see right here so as usual it takes in the first and second column of the input file it will assign it to the actual and predicted variables and then it's going to use it for calculating the confusion matrix and then finally we're going to create a data frame of it and then give it the column names of actual and predicted and then we're going to give it the index of actual and predicted so that we see the index name and also the column's name and then we're going to return it out and then the third function lines number 41 through 43 we're going to load the example data so this will be the y example.csv which we use upon clicking on the use example data button lines 46 through 50. it's going to be a custom function for us to download the csv file shown below here download csv file and then on lines 53 until 56 is going to be the sidebar and it's going to be the header of the sidebar so it is saying st.sidebar.header input panel so it's right here in the sidebar input panel and so make note that we're using the st.sidebar the sidebar here will specify the location of the header function and so if i take away the sidebar it's going to be moved to the main panel okay so we're going to leave it there because we want it in the sidebar and in line 54 through 56 is going to display the example csv file button right here example csv file so clicking on this will give you the example data okay and the example data is actually the one used here as well and then lines 59 is going to be the file uploader function and the import argument will allow it to type in or display upload your input csv file and then we specify that the input will only be accepting csv extension for the file name okay and then we assign the uploaded file to the uploaded file variable and actually if we delete a line here it's going to be 60 it's going to be 99 lines now so it's less than 100 now and then on line 62 and 63 is going to be the performance metrics let me show you it's right here so performance metrics will include the accuracy balance accuracy as i've mentioned and this is the list and then the performance metric list will be used as input here and so the performance metrics in the quotation mark it will be the name shown above the options that you see and then we're using the st.sidebar.multi-select function in order to display the multiple selections of the performance metrics so the two performance metrics variable used here will allow us to display all of the performance metrics in the possible options so let's see what if i modify this to be only accuracy let's see what happens and you're going to see that the accuracy is displayed here and so this let's see what if i put in position and we have position here so it's the selected value and what if i do the same thing but put it here instead save it it doesn't work okay so this is the full option so the full list should be placed in the second position okay so the entire list here should go here and then the third position here the input argument will be the one that we're going to select okay so i think i need a new keyboard because the built-in keyboard of the macbook pro sometimes it's not working all right so the third option here that you see will allow us to select the default parameters okay and then line 66 until 72 will be the header here it will display the image in the line number 66 until 67 so this will be responsible for displaying the logo 68 will display the web page title that you see here model performance calculator app and then the 69 until 72 will display the description of the app that you see right here let's have a look further line 74 until 99 okay so here you're going to see that we're using the if else logic okay and so in the ellis condition by default when you load the web page for the first time it will default to the else condition and so it will display the message awaiting the upload of the input file okay and so i'm uploading the input file csv file that you see here so we could just browse file click on it or we could drag and drop the input file into the box here then afterwards it will invoke the if condition okay because when a file is uploaded the if condition will be activated because the first line on line number 74 it will do a test or evaluation whether we have uploaded the file or not if we have uploaded the file it will run the following lines of code but if we haven't uploaded the file yet it will go to the else condition and then you're gonna see the awaiting the upload text message here along with the button use example data so let's take a look at the else condition so we have the if embedded inside the else and this is for the button and so we use if st button and so that what this means is that upon clicking if the stdot button is clicked perform the following statements right so if we click on it it will perform the calculation on the example data and so what does it do it will load the example data on line number 89 and then it will perform the calculation of the confusion matrix on line number 90 and it will calculate the performance metrics on line number 91 and then line number 92 will allow it to display the data frame for the selected metrics and so the selected metrics variable here is right here okay it is right here right here this is the selected metric so whatever is left here the data frame will display it okay okay so whatever performance metrics are selected here the data frame will be displaying only those selections and then lines 93 will display the input data right here input data 93 and 94 93 is the header 94 is the actual data frame lines number 95 will display the confusion matrix header and 96 will display the confusion matrix data frame line number 97 will display the performance metric header and line number 98 will display the data frame of the performance metric line number 99 will be the link to download the csv file okay and so the same exact explanation is also made for the if condition okay with the only exception is line number 75 and line number 89 okay so in the example line number 89 we'll be loading in the example data from the load example data function and in lines number 75 we'll be loading the csv file from the uploaded file so uploaded file right here from the uploaded box displayed here okay and so there you have it the explanation under the hood of the performance metrics application and congratulations you have built the model performance calculator app and if you would like to deploy this application to the web to the cloud then you want to check out the two videos that i have created prior to this and the links will be up in the top right hand corner feel free to check those out so that you'll be able to deploy the streamlet application to the cloud either using the streamlet sharing or using the heroku platform thank you for watching until the end of this video please let me know in the comments down below how you intend to use this application and i would greatly appreciate it if you support the channel by smashing the like button subscribing if you haven't already and also and also turning on the notification bell so that you will be notified of the next video and as always the best way to learn data science is to do data science and please enjoy the journey
Original Description
Do you have prediction results from machine learning models lying around? Too lazy to spin up a code to calculate the performance metrics? No worries we got you covered. In this video, you will learn how to build a machine learning model performance calculator app. Particularly, you feed the app with the actual and predicted values and the app produces the performance metrics.
🌟 Join as a Member to support this Channel:
https://www.youtube.com/channel/UCV8e2g4IWQqK71bbzGDEI4Q/join
🌟 Download Kite for FREE https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=dataprofessor&utm_content=description-only
⭕ Links for this video:
- Demo app https://share.streamlit.io/dataprofessor/model_performance_app/main/app.py
- Code https://github.com/dataprofessor/model_performance_app
🌟 Subscribe to this YouTube channel https://www.youtube.com/dataprofessor?sub_confirmation=1
🌟 Join the Newsletter of Data Professor http://newsletter.dataprofessor.org
🌟 Buy me a coffee https://www.buymeacoffee.com/dataprofessor
⭕ Playlist:
Check out our other videos in the following playlists.
✅ Data Science 101: https://bit.ly/dataprofessor-ds101
✅ Data Science YouTuber Podcast: https://bit.ly/datascience-youtuber-podcast
✅ Data Science Virtual Internship: https://bit.ly/dataprofessor-internship
✅ Bioinformatics: http://bit.ly/dataprofessor-bioinformatics
✅ Data Science Toolbox: https://bit.ly/dataprofessor-datasciencetoolbox
✅ Streamlit (Web App in Python): https://bit.ly/dataprofessor-streamlit
✅ Shiny (Web App in R): https://bit.ly/dataprofessor-shiny
✅ Google Colab Tips and Tricks: https://bit.ly/dataprofessor-google-colab
✅ Pandas Tips and Tricks: https://bit.ly/dataprofessor-pandas
✅ Python Data Science Project: https://bit.ly/dataprofessor-python-ds
✅ R Data Science Project: https://bit.ly/dataprofessor-r-ds
✅ Weka (No Code Machine Learning): http://bit.ly/dp-weka
⭕ Recommended Books:
🌟https://kit.co/dataprofessor
✅ Hands-On Machine Learning with Sci
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Data Professor · Data Professor · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How a Biologist became a Data Scientist
Data Professor
WEKA Tutorial #1.1 - How to Build a Data Mining Model from Scratch
Data Professor
WEKA Tutorial #1.2 - How to Build a Data Mining Model from Scratch
Data Professor
WEKA Tutorial #1.3 - How to Build a Data Mining Model from Scratch
Data Professor
Computational Drug Discovery: Machine Learning for Making Sense of Big Data in Drug Discovery
Data Professor
Quotes #1 on Big Data and Data Science
Data Professor
Quotes #2 on Big Data and Data Science
Data Professor
Quotes #3 on Big Data and Data Science
Data Professor
Quotes #4 on Big Data and Data Science
Data Professor
Quotes #5 on Big Data and Data Science
Data Professor
Data Science 101: Starting a Data Science / Data Mining Project
Data Professor
Data Science 101: CRISP-DM - Data Mining / Data Science in 6 Steps
Data Professor
R Programming 101: How to Define Variables
Data Professor
R Programming 101: Read and Write CSV files
Data Professor
Data Science 101: Basic Command-Line for Data Science
Data Professor
Strategies for Learning Data Science in 2020 (Data Science 101)
Data Professor
Building your Data Science Portfolio with GitHub (Data Science 101)
Data Professor
R Programming 101: Setting up R programming environment (R, RStudio and RStudio.cloud)
Data Professor
Exploratory Data Analysis in R: Towards Data Understanding
Data Professor
Exploratory Data Analysis in R: Quick Dive into Data Visualization
Data Professor
Machine Learning in R: Building a Classification Model
Data Professor
Machine Learning in R: Repurpose Machine Learning Code for New Data
Data Professor
Data Science 101: Deploying your Machine Learning Model
Data Professor
Machine Learning in R: Deploy Machine Learning Model using RDS
Data Professor
Data Pre-processing in R: Handling Missing Data
Data Professor
Machine Learning in R: Speed up Model Building with Parallel Computing
Data Professor
Data Science 101: Overview of Machine Learning Model Building Process
Data Professor
Web Apps in R: Building your First Web Application in R | Shiny Tutorial Ep 1
Data Professor
Web Apps in R: Build Interactive Histogram Web Application in R | Shiny Tutorial Ep 2
Data Professor
Web Apps in R: Building Data-Driven Web Application in R | Shiny Tutorial Ep 3
Data Professor
Web Apps in R: Building the Machine Learning Web Application in R | Shiny Tutorial Ep 4
Data Professor
Web Apps in R: Build BMI Calculator web application in R for health monitoring | Shiny Tutorial Ep 5
Data Professor
Machine Learning in R: Building a Linear Regression Model
Data Professor
What programming language to learn for Data Science? R versus Python
Data Professor
How to Become a Data Scientist (Learning Path and Skill Sets Needed)
Data Professor
Using Python in R
Data Professor
Interpretable Machine Learning Models
Data Professor
Making Scatter Plots in R [Data Visualisation in R series]
Data Professor
Machine Learning in Python: Building a Classification Model
Data Professor
Compare Machine Learning Classifiers in Python
Data Professor
Hyperparameter Tuning of Machine Learning Model in Python
Data Professor
Practical Introduction to Google Colab for Data Science
Data Professor
File Handling in Google Colab for Data Science
Data Professor
Pandas for Data Science: Create and Combine DataFrames / Rename Columns
Data Professor
Machine Learning in Python: Building a Linear Regression Model
Data Professor
Machine Learning in Python: Principal Component Analysis (PCA) for Handling High-Dimensional Data
Data Professor
How to Plot an ROC Curve in Python | Machine Learning in Python
Data Professor
Installing conda on Google Colab for Data Science
Data Professor
Use native R on Google Colab for Data Science
Data Professor
How to Save and Download files from Google Colab
Data Professor
Easy Web Scraping in Python using Pandas for Data Science
Data Professor
Data Science for Computational Drug Discovery using Python (Part 1)
Data Professor
Pandas Profiling for Data Science (Quick and Easy Exploratory Data Analysis)
Data Professor
Exploratory Data Analysis in Python using pandas
Data Professor
Quick tour of PyCaret (a low-code machine learning library in Python)
Data Professor
How to Upload Files to Google Colab
Data Professor
How to Install and Use Pandas Profiling on Google Colab
Data Professor
How to Adjust the Style of Pandas DataFrame
Data Professor
How to use Bamboolib for Data Wrangling in Data Science
Data Professor
How to use Pandas Profiling on Kaggle
Data Professor
More on: ML Pipelines
View skill →Related Reads
📰
📰
📰
📰
Introduction Data Science and Machine Learning
Medium · Data Science
AgriScore: An Explainable AI Credit Scoring System for Smallholder Farmers
Medium · Machine Learning
AgriScore: An Explainable AI Credit Scoring System for Smallholder Farmers
Medium · Data Science
The Sophistication Trap: Why the Smarter AI Technique Keeps Losing
Medium · Machine Learning
🎓
Tutor Explanation
DeepCamp AI