Prediction of Wine type using Deep Learning
Last Updated :
27 May, 2025
Deep learning is commonly used to analyze large datasets but to understand its core concepts it’s helpful to start with smaller, more manageable datasets. One such dataset is the Wine Quality dataset which includes information about the chemical properties of wines and their quality ratings.
In this article, we’ll explore how deep learning can be applied to predict wine quality based on its chemical composition. But before loading the data it is important to understand its features. The dataset consists of 12 variables. Here are a few of them:
- Fixed Acidity: This refers to the non-volatile acids in the wine, which contribute to the wine's tartness.
- Volatile Acidity: This refers to acetic acid content which can contribute to a vinegar-like taste in wine.
- Citric Acid: Citric acid is one of the fixed acids in wine.
- Residual Sugar: This is the sugar that remains after fermentation stops.
- Chlorides: Chlorides can contribute to saltiness in wine.
- Free Sulfur Dioxide: This is the sulfur dioxide that is added to wine.
- Total Sulfur Dioxide: This is the sum of bound and free sulfur dioxide.
1. Importing Required Libraries
We will be importing numpy, pandas, matplotlib, scikit learn and keras for model building.
Python
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
2. Loading and Preprocessing Data
We will be loading dataset from provided URL. After that we will preprocess it so that model can use this cleaned dataset for training and making prediction.
- type : is added to distinguish between red and white wine: 1 for red wine and 0 for white wine.
- pd.concat(): using this two datasets (red and white) are concatenated into a single data frame wine.
- dropna(): Any rows with missing values are dropped using this to ensure clean data for training the model.
Python
red = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv", sep=';')
white = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv", sep=';')
red['type'] = 1
white['type'] = 0
wines = pd.concat([red, white], ignore_index=True)
wines.dropna(inplace=True)
3. Plotting Distribution of Alcohol
We will use matplotlib
to create a visual representation of the distribution of alcohol content for red and white wines. We will create a histogram for each wine type (red
and white
) with a specified number of bins (10
).
Python
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].hist(wines[wines['type'] == 1].alcohol, bins=10, facecolor='red', alpha=0.5, label='Red wine')
ax[1].hist(wines[wines['type'] == 0].alcohol, bins=10, facecolor='white', edgecolor='black', lw=0.5, alpha=0.5, label='White wine')
for a in ax:
a.set_ylim([0, 1000])
a.set_xlabel('Alcohol in % Vol')
a.set_ylabel('Frequency')
ax[0].set_title('Alcohol Content in Red Wine')
ax[1].set_title('Alcohol Content in White Wine')
fig.suptitle('Distribution of Alcohol by Wine Type')
plt.tight_layout()
plt.show()
Output:
Alcohol Distribution Graph4. Splitting Data into Training and Testing Sets
We will split our dataset into training and testing.
- X contains all columns except the target variable (type), which is represented by y.
- train_test_split(): splits the dataset into training (66%) and testing (34%) sets.
- random_state=45: ensures that the split is reproducible.
Python
X = wines.iloc[:, :-1]
y = wines['type']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.34, random_state=45)
5. Creating Neural Network Model
The function Sequential() defines a neural network with 3 layers:
- Input layer: Dense(12) with 12 neurons and ReLU activation function where input_dim=12 matches the number of features (columns) in the input data.
- Hidden layer: Dense(9) with 9 neurons and ReLU activation.
- Output layer: Dense(1) with a single neuron and a sigmoid activation function since this is a binary classification problem (predicting red or white wine).
- The model is compiled using the binary crossentropy loss function and the Adam optimizer.
Python
model = Sequential()
model.add(Dense(12, activation='relu', input_dim=12))
model.add(Dense(9, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
6. Training the Model
We train the model using the training data (X_train, y_train) for 3 epochs with a batch size of 1.
Python
model.fit(X_train, y_train, epochs=3, batch_size=1, verbose=1)
Output:
Training the Model7. Making Predictions
We make predictions using the trained model on the test data (X_test) and get the predicted probabilities for each wine sample. We then convert these probabilities into binary labels (1 for Red wine, 0 for White wine) and display the wine type prediction for the first 12 samples.
Python
y_pred = model.predict(X_test)
y_pred_labels = (y_pred >= 0.5).astype(int)
for prediction in y_pred_labels[:12]:
wine_type = "Red wine" if prediction == 1 else "White wine"
print(f"Prediction: {wine_type}")
Output:
Making PredictionsIn this article, we explored how deep learning can be applied to predict wine quality based on its chemical composition.
You can download source code from here.
Similar Reads
IPL Score Prediction using Deep Learning
In todayâs world of cricket every run and decision can turn the game around. Using Deep Learning to predict IPL scores during live matches is becoming a game changer. This article shows how advanced algorithms help us to forecast scores with impressive accuracy, giving fans and analysts valuable ins
7 min read
Waiter's Tip Prediction using Machine Learning
If you have recently visited a restaurant for a family dinner or lunch and you have tipped the waiter for his generous behavior then this project might excite you. As in this article, we will try to predict what amount of tip a person will give based on his/her visit to the restaurant using some fea
7 min read
Wine Quality Prediction - Machine Learning
Here we will predict the quality of wine on the basis of given features. We use the wine quality dataset available on Internet for free. This dataset has the fundamental features which are responsible for affecting the quality of the wine. By the use of several Machine learning models, we will predi
5 min read
Next Word Prediction with Deep Learning in NLP
Next Word Prediction is a natural language processing (NLP) task where a model predicts the most likely word that should follow a given sequence of words in a sentence. It is a fundamental concept in language modeling and is widely used in various applications such as autocomplete systems, chatbots,
7 min read
AlphaFold: Predicting Protein Structures with Deep Learning
AlphaFold is an AI-driven system developed by DeepMind to predict protein structures with high accuracy. Its various iterations have been part of the continuous improvement in computational biology. Proteins are essential molecules in living organisms, involved in virtually every process within cell
6 min read
House Price Prediction using Machine Learning in Python
House price prediction is a problem in the real estate industry to make informed decisions. By using machine learning algorithms we can predict the price of a house based on various features such as location, size, number of bedrooms and other relevant factors. In this article we will explore how to
6 min read
Train a Deep Learning Model With Pytorch
Neural Network is a type of machine learning model inspired by the structure and function of human brain. It consists of layers of interconnected nodes called neurons which process and transmit information. Neural networks are particularly well-suited for tasks such as image and speech recognition,
6 min read
Bitcoin Price Prediction using Machine Learning in Python
Machine learning proves immensely helpful in many industries in automating tasks that earlier required human labor one such application of ML is predicting whether a particular trade will be profitable or not.In this article, we will learn how to predict a signal that indicates whether buying a part
7 min read
Why is Deep Learning used in Recommender Systems?
Deep learning enhances recommendation accuracy and personalization by automatically learning patterns and representations from large datasets. Deep learning is employed in recommender systems due to its capacity to address the complexities of user preferences and item characteristics within vast and
6 min read
What is Relational Deep Learning?
Relational Deep Learning (RDL) is an advanced paradigm that bridges deep learning and relational reasoning to model and understand interconnected data more effectively, Relational Deep Learning enables deep neural networks to operate on structured data and relational reasoning tasks by incorporating
10 min read