Object Oriented Code for Tensorflow Machine Learning on Diabetes Dataset¶
In [43]:
try:
import tensorflow as tf
# for Data Processing
import numpy as np
import pandas as pd
# for Plotting
import matplotlib.pyplot as plt
# for Data Processing
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix,classification_report
print('Library Loaded .........')
except:
print('One or More Library was not Found ! ')
In [20]:
class NeuralNetwork(object):
def __init__(self,epoch=100,batch_size =30):
self.epoch = epoch
self.batch_size = batch_size
def process_data(self):
"""
: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("/Users/soumilshah/IdeaProjects/mytensorflow/Dataset/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"]
self.X_Train, self.X_Test, self.Y_Train, self.Y_Test = train_test_split(X_Data,Y_Data, test_size=0.3,random_state=101)
return self.X_Train, self.X_Test, self.Y_Train, self.Y_Test
def create_feature_columns(self):
"""
:return: feature_column ITS a LIST
"""
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')
self.feature_column = [feat_Pregnancies, feat_Glucose, feat_BloodPressure,
feat_SkinThickness_tricep, feat_Insulin,
feat_BMI , feat_DiabetesPedigreeFunction]
return self.feature_column
def create_input_and_test_func(self):
"""
:return: input_func, eval_input_func
"""
self.X_Train, self.X_Test, self.Y_Train, self.Y_Test = self.process_data()
self.input_func = tf.estimator.inputs.pandas_input_fn(x=self.X_Train, y=self.Y_Train,
batch_size=self.batch_size,
num_epochs =self.epoch,
shuffle=True)
self.eval_input_func = tf.estimator.inputs.pandas_input_fn(x=self.X_Test,
y=self.Y_Test,
batch_size=self.batch_size,
num_epochs=1,
shuffle=False)
return self.input_func, self.eval_input_func
def create_Model(self):
"""
:return: model
"""
self.feature_column = self.create_feature_columns()
self.model = tf.estimator.LinearClassifier(feature_columns=self.feature_column,
n_classes=2)
return self.model
def train(self,model):
"""
:param model:
:return: history
"""
self.input_func, self.eval_input_func = self.create_input_and_test_func()
self.history = model.train(input_fn=self.input_func, steps = self.epoch)
return self.history
def test(self, model):
"""
:param model:
:return: results
"""
self.input_func, self.eval_input_func = self.create_input_and_test_func()
self.results = model.evaluate(self.eval_input_func)
return self.results
In [23]:
if __name__ == "__main__":
n = NeuralNetwork(epoch=300,batch_size=40)
model = n.create_Model()
history = n.train(model)
results = n.test(model)
In [24]:
results
Out[24]:
No comments:
Post a Comment