DNN classifier¶
Step 1:¶
import Modules¶
In [35]:
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix,classification_report
import scikitplot as skplt
%matplotlib inline
Step 2:¶
Process Data¶
In [2]:
def Data_Process():
"""
This will read the CSV and Normalize the Data and
Perform Train Test Split and Return
X_Train, X_Test, Y_Train, Y_Test
"""
# Name for the column or Features Map
columns_to_named = ["Pregnancies","Glucose","BloodPressure",
"SkinThickness","Insulin","BMI","DiabetesPedigreeFunction",
"Age","Class"]
# Read the Dataset and Rename the Column
df = pd.read_csv("pima-indians-diabetes.csv",header=0,names=columns_to_named)
col_norm =['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin',
'BMI', 'DiabetesPedigreeFunction']
# Normalization using Custom Lambda Function
df1_norm = df[col_norm].apply(lambda x :( (x - x.min()) / (x.max()-x.min()) ) )
X_Data = df1_norm
Y_Data = df["Class"]
X_Train, X_Test, Y_Train, Y_Test = train_test_split(X_Data,Y_Data, test_size=0.3,random_state=101)
return X_Train, X_Test, Y_Train, Y_Test
Step 3:¶
create a feature column¶
In [3]:
def create_feature_column():
feat_Pregnancies = tf.feature_column.numeric_column('Pregnancies')
feat_Glucose = tf.feature_column.numeric_column('Glucose')
feat_BloodPressure = tf.feature_column.numeric_column('BloodPressure')
feat_SkinThickness_tricep = tf.feature_column.numeric_column('SkinThickness')
feat_Insulin = tf.feature_column.numeric_column('Insulin')
feat_BMI = tf.feature_column.numeric_column('BMI')
feat_DiabetesPedigreeFunction = tf.feature_column.numeric_column('DiabetesPedigreeFunction')
feature_column = [feat_Pregnancies, feat_Glucose, feat_BloodPressure,
feat_SkinThickness_tricep, feat_Insulin,
feat_BMI , feat_DiabetesPedigreeFunction]
return feature_column
In [24]:
X_Train, X_Test, Y_Train, Y_Test = Data_Process()
feature_column = create_feature_column()
input_func = tf.estimator.inputs.pandas_input_fn(X_Train,
Y_Train,
batch_size=50,
num_epochs=1000,
shuffle=True)
eval_func = tf.estimator.inputs.pandas_input_fn(X_Test,
Y_Test,
batch_size=50,
num_epochs=1,
shuffle=False)
predict_input_fn = tf.estimator.inputs.pandas_input_fn(
x=X_Test,
num_epochs=1,
shuffle=False)
Step 4¶
create a DNN model¶
In [5]:
dnnmodel = tf.estimator.DNNClassifier(
hidden_units = [20,20],
feature_columns = feature_column,
n_classes=2,
activation_fn=tf.nn.softmax,
dropout=None,
optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
)
Step 5:¶
Train¶
In [8]:
history = dnnmodel.train(input_fn=input_func,
steps=500)
Test¶
In [9]:
dnnmodel.evaluate(eval_func)
Out[9]:
Predict¶
In [42]:
predictions = list(dnnmodel.predict(input_fn=predict_input_fn))
prediction = [p["class_ids"][0] for p in predictions]
data = classification_report(Y_Test,prediction)
conmat = confusion_matrix(Y_Test,prediction)
Analysis¶
In [46]:
skplt.metrics.plot_confusion_matrix(Y_Test,
prediction,
figsize=(6,6),
title="Confusion Matrix")
Out[46]:
In [44]:
print(data)
In [ ]:
No comments:
Post a Comment