Machine Learning Model for Wine DataSet¶
Results 98.14 %¶
In [79]:
skplt.metrics.plot_confusion_matrix(Y_Test, prediction)
Out[79]:
Import the Library¶
In [75]:
from sklearn.datasets import load_wine
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import confusion_matrix,classification_report
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import pandas as pd
import numpy as np
import scikitplot as skplt
In [74]:
!pip install scikit-plot
In [ ]:
Load the Dataset¶
In [2]:
wine_data = load_wine()
In [3]:
type(wine_data)
Out[3]:
In [4]:
wine_data.keys()
Out[4]:
In [5]:
print(wine_data["DESCR"])
In [49]:
label = ["Alcohol","Malic Acid","Ash","Alcalinity of Ash","Magnesium","Total Phenols","Flavanoids",
"Nonflavanoid Phenols", "Proanthocyanins","Colour Intensity","Hue","OD280/OD315 of diluted wines",
"Proline"]
Get the X_Data¶
In [6]:
feat_data = wine_data["data"]
Get the Y_Data¶
In [7]:
label = wine_data["target"]
Create Train and Test Data using Train Test Split¶
In [8]:
X_Train, X_Test, Y_Train, Y_Test = train_test_split(feat_data, label, test_size=0.3,random_state=101)
Create a Min Max Scalar to Scale the Dataset¶
In [9]:
scalar = MinMaxScaler()
Scale the X_Data¶
In [10]:
scaled_X_Data = scalar.fit_transform(X_Train)
Scale the Y_ Data¶
In [11]:
scaled_X_Test = scalar.fit_transform(X_Test)
Check the Shape of Data set¶
In [12]:
scaled_X_Data.shape
Out[12]:
In [13]:
scaled_X_Test.shape
Out[13]:
Create a Feature Colummn¶
In [14]:
feat_cols = [tf.feature_column.numeric_column('x',shape=[13])]
Create Model¶
In [21]:
model = tf.estimator.DNNClassifier(
feature_columns=feat_cols,
hidden_units=[13,13,13],
optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.01),
n_classes=3,
dropout=None,
batch_norm=False
)
Create a input Function¶
In [22]:
input_func = tf.estimator.inputs.numpy_input_fn({'x':scaled_X_Data},
y=Y_Train,
shuffle=True,
batch_size=10,
num_epochs=100)
Create a Test input Function¶
In [23]:
test_func = tf.estimator.inputs.numpy_input_fn({'x':scaled_X_Test},
y=Y_Test,
shuffle=False,
batch_size=10,
num_epochs=1)
In [24]:
model.train(input_fn=input_func,steps=500)
Out[24]:
Test the Data¶
In [35]:
preds = list(model.predict(input_fn=test_func))
In [37]:
prediction = [p["class_ids"][0] for p in preds]
In [ ]:
label = []
In [41]:
data = classification_report(Y_Test,prediction,label)
In [54]:
print(data)
In [57]:
conmat = confusion_matrix(Y_Test,prediction)
In [61]:
df = pd.DataFrame(data=conmat)
df
Out[61]:
Confusion Matrix¶
In [78]:
skplt.metrics.plot_confusion_matrix(Y_Test, prediction)
Out[78]:
No comments:
Post a Comment