Milk Quality Prediction
Milk Quality Prediction
Import libraries
In [1]: import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import seaborn as sns
import itertools
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
from plotly.subplots import make_subplots
import warnings
warnings.filterwarnings('ignore')
# NN models
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras import optimizers
from keras.wrappers.scikit_learn import KerasClassifier
from keras.callbacks import EarlyStopping, ModelCheckpoint
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
/kaggle/input/milkquality/milknew.csv
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 1/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
Out[3]: (1059, 8)
In [4]: df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1059 entries, 0 to 1058
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 pH 1059 non-null float64
1 Temprature 1059 non-null int64
2 Taste 1059 non-null int64
3 Odor 1059 non-null int64
4 Fat 1059 non-null int64
5 Turbidity 1059 non-null int64
6 Colour 1059 non-null int64
7 Grade 1059 non-null object
dtypes: float64(1), int64(6), object(1)
memory usage: 66.3+ KB
In [5]: df.isna().sum()
Out[5]: pH 0
Temprature 0
Taste 0
Odor 0
Fat 0
Turbidity 0
Colour 0
Grade 0
dtype: int64
In [6]: df.duplicated().sum()
Out[6]: 976
In [7]: df.nunique()
Out[7]: pH 16
Temprature 17
Taste 2
Odor 2
Fat 2
Turbidity 2
Colour 9
Grade 3
dtype: int64
In [8]: df['Grade'].value_counts()
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 2/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
In [10]: df.describe().T
Data Visualization
In [11]: for i in df.columns:
plt.figure(figsize=(13,7))
sns.histplot(data = df[i], kde=True, multiple='stack')
plt.xticks(rotation=90)
plt.show()
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 3/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 4/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 5/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
In [12]: plt.style.use("seaborn")
df.hist(figsize=(25,20), bins=15)
Out[12]: array([[<AxesSubplot:title={'center':'pH'}>,
<AxesSubplot:title={'center':'Temprature'}>,
<AxesSubplot:title={'center':'Taste'}>],
[<AxesSubplot:title={'center':'Odor'}>,
<AxesSubplot:title={'center':'Fat '}>,
<AxesSubplot:title={'center':'Turbidity'}>],
[<AxesSubplot:title={'center':'Colour'}>, <AxesSubplot:>,
<AxesSubplot:>]], dtype=object)
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 6/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
In [13]: plt.figure(figsize=(15,6))
sns.barplot(data = df, color = 'lightblue')
plt.xticks(rotation=90, fontsize=10)
plt.show()
In [14]: plt.figure(figsize=(15,6))
sns.boxenplot(data = df, color = 'lightblue')
plt.xticks(rotation=90, fontsize=10)
plt.show()
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 7/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
In [16]: sns.pairplot(df,hue='Grade')
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 8/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 9/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
Data Preparation
In [19]: x=df.drop('Grade',axis=1)
y=df['Grade']
Model Building
In [20]: from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.30, random_state
x_train_std = ss.fit_transform(x_train)
x_test_std = ss.transform(x_test)
SVM
In [22]: from sklearn import metrics
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.svm import SVC
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 10/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
svc=SVC()
svc.fit(x_train,y_train)
Out[22]: SVC()
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 11/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
Decision Tree
In [26]: from sklearn.tree import DecisionTreeClassifier
dtc=DecisionTreeClassifier(criterion='entropy',random_state=42)
dtc.fit(x_train,y_train)
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 12/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
Random Forest
In [30]: from sklearn.ensemble import RandomForestClassifier
rfc=RandomForestClassifier()
rfc.fit(x_train,y_train)
Out[30]: RandomForestClassifier()
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 13/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
Out[34]: GaussianNB()
sns.heatmap(cf_matrix, annot=True)
plt.title("Confusion Matrix for GaussianNB", fontsize=10, y=1.03)
Linear Regression
In [38]: from sklearn.linear_model import LinearRegression
lnr = LinearRegression()
lnr.fit(x_train, y_train)
Out[38]: LinearRegression()
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 15/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
Logistic Regression
In [40]: from sklearn.linear_model import LogisticRegression
lr=LogisticRegression()
lr.fit(x_train,y_train)
Out[40]: LogisticRegression()
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 16/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
XgBoost
In [44]: from xgboost import XGBClassifier
xgb = XGBClassifier()
xgb.fit(x_train, y_train)
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 17/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
KNeighbors
In [48]: from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(x_train,y_train)
Out[48]: KNeighborsClassifier()
sns.heatmap(cf_matrix, annot=True)
plt.title("Confusion Matrix for KNeighborsClassifier", fontsize=10, y=1.03)
GradientBoosting
In [52]: from sklearn.ensemble import GradientBoostingClassifier
gb = GradientBoostingClassifier()
gb.fit(x_train, y_train)
Out[52]: GradientBoostingClassifier()
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 19/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 20/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
# Model Optimizer
ann_model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['acc
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 21/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
Epoch 1/100
47/47 [==============================] - 2s 8ms/step - loss: 0.9364 - accuracy: 0.
4521 - val_loss: 6.3271 - val_accuracy: 0.3648
Epoch 2/100
47/47 [==============================] - 0s 3ms/step - loss: 0.4443 - accuracy: 0.
5412 - val_loss: 1.0002 - val_accuracy: 0.4528
Epoch 3/100
47/47 [==============================] - 0s 3ms/step - loss: 0.0975 - accuracy: 0.
5776 - val_loss: 0.7874 - val_accuracy: 0.4686
Epoch 4/100
47/47 [==============================] - 0s 4ms/step - loss: 0.0673 - accuracy: 0.
5344 - val_loss: -0.0601 - val_accuracy: 0.5252
Epoch 5/100
47/47 [==============================] - 0s 3ms/step - loss: -0.0162 - accuracy:
0.5574 - val_loss: 1.2288 - val_accuracy: 0.4088
Epoch 6/100
47/47 [==============================] - 0s 4ms/step - loss: -0.1415 - accuracy:
0.5762 - val_loss: 4.8013 - val_accuracy: 0.3648
Epoch 7/100
47/47 [==============================] - 0s 5ms/step - loss: -0.3351 - accuracy:
0.5803 - val_loss: 1.4062 - val_accuracy: 0.3742
Epoch 8/100
47/47 [==============================] - 0s 3ms/step - loss: -0.4995 - accuracy:
0.5830 - val_loss: 1.0389 - val_accuracy: 0.4119
Epoch 9/100
47/47 [==============================] - 0s 3ms/step - loss: -0.6944 - accuracy:
0.5843 - val_loss: -0.2170 - val_accuracy: 0.5629
Epoch 10/100
47/47 [==============================] - 0s 3ms/step - loss: -0.8488 - accuracy:
0.5870 - val_loss: -0.0185 - val_accuracy: 0.5597
Epoch 11/100
47/47 [==============================] - 0s 3ms/step - loss: -0.8800 - accuracy:
0.5695 - val_loss: -0.8343 - val_accuracy: 0.4874
Epoch 12/100
47/47 [==============================] - 0s 4ms/step - loss: -1.2688 - accuracy:
0.5843 - val_loss: -0.7517 - val_accuracy: 0.5126
Epoch 13/100
47/47 [==============================] - 0s 4ms/step - loss: -1.3348 - accuracy:
0.5884 - val_loss: 4.9570 - val_accuracy: 0.2799
Epoch 14/100
47/47 [==============================] - 0s 3ms/step - loss: -1.7121 - accuracy:
0.5735 - val_loss: 7.0937 - val_accuracy: 0.3648
Epoch 15/100
47/47 [==============================] - 0s 4ms/step - loss: -2.0444 - accuracy:
0.5816 - val_loss: 6.9729 - val_accuracy: 0.3648
Epoch 16/100
47/47 [==============================] - 0s 4ms/step - loss: -2.3847 - accuracy:
0.5951 - val_loss: 12.9911 - val_accuracy: 0.3648
Epoch 17/100
47/47 [==============================] - 0s 4ms/step - loss: -2.7639 - accuracy:
0.5789 - val_loss: 2.9185 - val_accuracy: 0.4057
Epoch 18/100
47/47 [==============================] - 0s 4ms/step - loss: -2.8672 - accuracy:
0.5695 - val_loss: -1.0519 - val_accuracy: 0.6635
Epoch 19/100
47/47 [==============================] - 0s 4ms/step - loss: -3.5181 - accuracy:
0.5830 - val_loss: 0.0758 - val_accuracy: 0.4654
Epoch 20/100
47/47 [==============================] - 0s 4ms/step - loss: -3.7408 - accuracy:
0.5682 - val_loss: -1.5211 - val_accuracy: 0.6006
Epoch 21/100
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 22/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 23/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 24/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
Epoch 62/100
47/47 [==============================] - 0s 3ms/step - loss: -43.1137 - accuracy:
0.5385 - val_loss: 49.5336 - val_accuracy: 0.3648
Epoch 63/100
47/47 [==============================] - 0s 3ms/step - loss: -44.0907 - accuracy:
0.5412 - val_loss: -66.1592 - val_accuracy: 0.4874
Epoch 64/100
47/47 [==============================] - 0s 3ms/step - loss: -42.1352 - accuracy:
0.5439 - val_loss: -49.3381 - val_accuracy: 0.5535
Epoch 65/100
47/47 [==============================] - 0s 3ms/step - loss: -45.9768 - accuracy:
0.5547 - val_loss: -6.1029 - val_accuracy: 0.5377
Epoch 66/100
47/47 [==============================] - 0s 3ms/step - loss: -51.6089 - accuracy:
0.5601 - val_loss: -78.2757 - val_accuracy: 0.4937
Epoch 67/100
47/47 [==============================] - 0s 4ms/step - loss: -49.7343 - accuracy:
0.5506 - val_loss: -69.9387 - val_accuracy: 0.5472
Epoch 68/100
47/47 [==============================] - 0s 3ms/step - loss: -43.6494 - accuracy:
0.5439 - val_loss: -51.1160 - val_accuracy: 0.6069
Epoch 69/100
47/47 [==============================] - 0s 4ms/step - loss: -50.6234 - accuracy:
0.5250 - val_loss: -67.5859 - val_accuracy: 0.4245
Epoch 70/100
47/47 [==============================] - 0s 3ms/step - loss: -49.0533 - accuracy:
0.5371 - val_loss: -77.0359 - val_accuracy: 0.4151
Epoch 71/100
47/47 [==============================] - 0s 3ms/step - loss: -54.8261 - accuracy:
0.5425 - val_loss: -81.6451 - val_accuracy: 0.4119
Epoch 72/100
47/47 [==============================] - 0s 3ms/step - loss: -54.1311 - accuracy:
0.5439 - val_loss: -45.4728 - val_accuracy: 0.5409
Epoch 73/100
47/47 [==============================] - 0s 4ms/step - loss: -58.9882 - accuracy:
0.5560 - val_loss: 32.9783 - val_accuracy: 0.3302
Epoch 74/100
47/47 [==============================] - 0s 3ms/step - loss: -56.2245 - accuracy:
0.5547 - val_loss: -106.2492 - val_accuracy: 0.4717
Epoch 75/100
47/47 [==============================] - 0s 4ms/step - loss: -53.6323 - accuracy:
0.5479 - val_loss: -67.5443 - val_accuracy: 0.3648
Epoch 76/100
47/47 [==============================] - 0s 3ms/step - loss: -54.8622 - accuracy:
0.5398 - val_loss: -54.1232 - val_accuracy: 0.6164
Epoch 77/100
47/47 [==============================] - 0s 3ms/step - loss: -59.2817 - accuracy:
0.5277 - val_loss: -72.1177 - val_accuracy: 0.3648
Epoch 78/100
47/47 [==============================] - 0s 4ms/step - loss: -60.6974 - accuracy:
0.5290 - val_loss: -95.8444 - val_accuracy: 0.4780
Epoch 79/100
47/47 [==============================] - 0s 4ms/step - loss: -58.1961 - accuracy:
0.5290 - val_loss: -115.4964 - val_accuracy: 0.4811
Epoch 80/100
47/47 [==============================] - 0s 4ms/step - loss: -65.8021 - accuracy:
0.5425 - val_loss: -108.7465 - val_accuracy: 0.4025
Epoch 81/100
47/47 [==============================] - 0s 3ms/step - loss: -69.1130 - accuracy:
0.5398 - val_loss: -96.1030 - val_accuracy: 0.4025
Epoch 82/100
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 25/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
In [59]: ann_model.summary()
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 26/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 32) 256
=================================================================
Total params: 2,817
Trainable params: 2,625
Non-trainable params: 192
_________________________________________________________________
Out[60]: <AxesSubplot:>
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 27/28
4/4/23, 12:48 PM milk-quality-prediction-using-machine-learning
[[ 65 51 0]
[ 10 106 0]
[ 0 86 0]]
score is: 0.5377358490566038
In [63]: print(classification_report(y_test,y_pred))
Prediction
In [64]: print(xgb.predict(x_test))
[2 0 1 0 2 1 0 2 2 1 0 2 1 1 2 1 2 2 2 0 2 0 1 2 2 0 2 2 0 1 2 0 0 1 2 2 0
1 2 0 2 1 2 0 1 0 2 0 1 0 2 1 1 1 0 1 1 0 1 0 1 0 1 2 0 1 0 1 1 2 2 1 1 2
1 1 1 0 2 1 0 1 1 0 1 1 2 0 2 0 1 2 1 0 2 1 0 2 1 1 2 1 0 1 2 2 2 2 0 0 2
2 2 2 0 1 0 2 1 2 1 1 0 2 2 0 0 0 1 2 0 1 0 1 2 1 1 1 1 0 1 2 0 0 1 2 0 0
0 2 0 1 1 0 2 2 1 1 1 1 1 1 0 1 1 2 2 0 2 1 0 0 1 2 2 0 2 2 2 1 1 0 0 0 0
2 1 2 0 0 2 0 0 0 1 0 1 1 2 1 0 2 1 0 1 1 0 1 1 0 2 1 1 1 1 1 2 1 2 0 2 2
1 2 1 0 0 1 2 2 2 0 0 2 0 1 0 0 0 2 0 2 0 1 2 0 1 1 0 0 1 0 2 0 0 0 2 0 2
0 0 1 1 0 1 1 0 1 1 1 1 1 0 0 0 2 1 0 0 1 1 0 2 2 0 1 2 1 1 1 0 1 0 0 0 0
0 0 0 0 1 1 0 1 1 1 1 0 1 1 1 0 0 0 2 0 2 0]
In [ ]:
localhost:8888/nbconvert/html/Downloads/milk-quality-prediction-using-machine-learning.ipynb?download=false 28/28