Wine Quality Prediction App Using Streamlit

Stuti Singh
Analytics Vidhya
Published in
4 min readJun 29, 2020

What makes a wine better quality than another. It is very difficult for us to assess the quality of wine just by reading the label. Quality is assessed best by tasting. Very often we cannot taste a wine before buying, but information is available on wine labels to help you decide, although you will need a certain amount of knowledge about producers and vintages. Of course, any wine seller can offer you this advice. In this article we are going to take help from machine learning and let it predict wine quality by reading the label.

This blog is mainly focused on application deployment. Although deployment is the last step in data science project’s life cycle but it’s very important aspect. It provides access to technical and non-technical users. In data science, project life cycle is as depicted below in picture.

Image Reference-https://www.quora.com/What-is-the-life-cycle-of-a-data-science-project

Sometimes it’s very tiresome to spend time on a web framework after investing so many hours on a data science project. Project Deployment can be tricky. Here comes Streamlit. Streamlit takes care of deployment and which can be published as a working web application.

Streamlit

Streamlit is an open-source Python library that makes it easy to build beautiful custom web-apps for machine learning and data science. It is very easy to use. Streamlit watches for changes on each save and updates the app live while you’re coding. Code runs from top to bottom, always from a clean state, and with no need for callbacks.

In this blog, we will lean on the resourcefulness of Streamlit to help us deploy our own model. You’ll love working with Streamlit!

Here I am going to make a simple machine learning with the help of Streamlit to predict wine quality from 3 to 8(as in the dataset). I used red wine dataset from kaggle(link). For simplicity, I have selected Random Forest Model as a classifier.

Step 1- Install required Libraries

In this project Streamlit, pandas and scikit-learn are required libraries.

At first, you need to install Streamlit on your system using pip. Type “$pip install streamlit” in your terminal. In a similar manner we can install pandas and scikit-learn. After installation we can import it.

Let’s have a look on instance of our dataset.

Step 2-Code of Web Application

Lines(7–10) st.write()function to print out the title of app in markdown format (making use of the # symbol to signify header text (line 8) whereas subsequent line (line 9) provides normal descriptive text for web app.

Lines(11–34) In app we are creating a side bar for user input parameters. There will be sliders for different wine characteristics as fixed_acidity, volatile_acidity etc.

st.write() function is used to display the contents of the df dataframe.

Lines(35–38) Line 36 load dataset from csv file. Line 38 and 39 to divide dataset as predictor(X) and target data(Y).

Line 40 create an instance of Random Forest Classifier. In subsequent lines we train the model via the clf.fit function by using X and Y variables as input arguments.

Step 3- Running the web app

streamlit run app1.py

After a few seconds, an internet browser window should pop-up and directs you to the created web app by taking you to http://localhost:8501 as shown below.

Congratulations, you have created a machine learning powered app in just 50 lines of code. The most fascinating fact is that we have deployed a machine learning web application without any need to learn Flask or Django. In addition, Streamlit is easy to use. Please share it with your friends and colleagues if you found it helpful.

References:

  1. https://towardsdatascience.com/how-to-build-a-simple-machine-learning-web-app-in-python-68a45a0e0291
  2. https://docs.streamlit.io/en

--

--