-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wine Quality Prediction.py
252 lines (126 loc) · 4.28 KB
/
Wine Quality Prediction.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn import metrics
from sklearn.svm import SVC
from xgboost import XGBClassifier
from sklearn.linear_model import LogisticRegression
import warnings
warnings.filterwarnings('ignore')
# In[2]:
df = pd.read_csv("WineQT.csv")
# In[3]:
df.head()
# In[4]:
df.info()
# In[6]:
df.describe().T
# In[5]:
#describes the dataset
df.describe()
# In[6]:
df.describe().T
#transposes index & columns of the table
# In[7]:
df.isnull()
#checks whether there are NULL values
# In[8]:
df.isnull().sum()
#sum of NULL values
# <b> if there were any missing data:
#
# for col in df.columns:
#
# if df[col].isnull().sum() > 0:
#
# df[col] = df[col].fillna(df[col].mean())
#
# df.isnull().sum()
# # Histogram
# To visualise distribution of data with continuous values in columns of dataset.
# In[9]:
df.hist(bins = 20, figsize = (10,10))
plt.show()
# # COUNT PLOT
# To visualise number data of each quality of wine
# In[10]:
plt.bar(df['quality'], df['alcohol'])
plt.xlabel('quality')
plt.ylabel('alcohol')
plt.show()
# # HEATMAP
# To remove redundant features
# In[11]:
plt.figure(figsize=(12,12))
sb.heatmap(df.corr() > 0.7, annot = True, cbar=False)
plt.show()
# # MODEL DEVELOPMENT
# Let’s prepare our data for training and splitting it into training and validation data so, that we can select which model’s performance is best as per the use case. We will train some of the state of the art machine learning classification models and then select best out of them using validation data.
# In[12]:
df['best quality'] = [1 if x > 5 else 0 for x in df.quality]
# We replace the column with 'object' data type with '0 and 1' as there are only two categories!
# In[13]:
df.replace({'white': 1, 'red': 0}, inplace=True)
# After segregating features & the target variable from the dataset,
# we split in into 80:20 for MODEL SELECTION.
# In[14]:
features = df.drop(['quality', 'best quality'], axis =1)
target = df['best quality']
xtrain, xtest, ytrain, ytest = train_test_split(features, target, test_size = 0.2, random_state=40)
xtrain.shape, xtest.shape
# # NORMALISING THE DATA
# Normalising the data before training helps us to achieve stable and fast training of the model.
# In[15]:
norm = MinMaxScaler()
xtrain = norm.fit_transform(xtrain)
xtest = norm.transform(xtest)
# As the data has been prepared completely,
# let's train some state of the art machine learning model on it.
# In[16]:
models = [LogisticRegression(), XGBClassifier(), SVC(kernel='rbf')]
for i in range(3):
models[i].fit(xtrain, ytrain)
print(f'{models[i]}: ')
print('Training Accuracy: ', metrics.roc_auc_score(ytrain, models[i].predict(xtrain)))
print('Validation Accuracy: ', metrics.roc_auc_score(ytest, models[i].predict(xtest)))
print()
# From the above accuarcies, we can say that
#
# <b> Logistic Regression and SVC performed better than XGBClassifier
#
# on the validation data with less difference between the validation and training data.
# # CONFUSION MATRIX
# Plot confusion matrix for validation data using logistic regression model:
# In[17]:
metrics.plot_confusion_matrix(models[0], xtest, ytest)
plt.show()
# Plot confusion matrix for validation data using XGBClassifier model:
# In[18]:
metrics.plot_confusion_matrix(models[1], xtest, ytest)
plt.show()
# Plot confusion matrix for validation data using SVC model:
# In[19]:
metrics.plot_confusion_matrix(models[2], xtest, ytest)
plt.show()
# # CLASSIFICATION REPORT
# Print classification report of linear regression model:
# In[20]:
print(metrics.classification_report(ytest, models[0].predict(xtest)))
# Print classification report of XGBCLassifier:
# In[21]:
print(metrics.classification_report(ytest, models[1].predict(xtest)))
# Print classification report of SVC model:
# In[22]:
print(metrics.classification_report(ytest, models[2].predict(xtest)))
# In[ ]:
# Dataset used: Wine Quality Dataset, Kaggle
# (https://www.kaggle.com/datasets/yasserh/wine-quality-dataset)
# Authored by:
# Soumya Kushwaha