Friday, May 17, 2019

Object Oriented Code for Tensorflow Machine Learning on Diabetes Dataset

Diabetes Object Oriented Code

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 ! ')
Library Loaded .........
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)
INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /var/folders/yh/7gktt0ls0fj77fnrs694ht6m0000gn/T/tmp5bz6d2dd
INFO:tensorflow:Using config: {'_model_dir': '/var/folders/yh/7gktt0ls0fj77fnrs694ht6m0000gn/T/tmp5bz6d2dd', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
  rewrite_options {
    meta_optimizer_iterations: ONE
  }
}
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x10da9f518>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Saving checkpoints for 0 into /var/folders/yh/7gktt0ls0fj77fnrs694ht6m0000gn/T/tmp5bz6d2dd/model.ckpt.
INFO:tensorflow:loss = 27.725887, step = 1
INFO:tensorflow:global_step/sec: 309.778
INFO:tensorflow:loss = 23.877613, step = 101 (0.326 sec)
INFO:tensorflow:global_step/sec: 318.953
INFO:tensorflow:loss = 25.032936, step = 201 (0.320 sec)
INFO:tensorflow:Saving checkpoints for 300 into /var/folders/yh/7gktt0ls0fj77fnrs694ht6m0000gn/T/tmp5bz6d2dd/model.ckpt.
INFO:tensorflow:Loss for final step: 21.865465.
INFO:tensorflow:Calling model_fn.
WARNING:tensorflow:From /anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/metrics_impl.py:2002: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Deprecated in favor of operator or tf.math.divide.
WARNING:tensorflow:Trapezoidal rule is known to produce incorrect PR-AUCs; please switch to "careful_interpolation" instead.
WARNING:tensorflow:Trapezoidal rule is known to produce incorrect PR-AUCs; please switch to "careful_interpolation" instead.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2019-05-17T14:51:51Z
INFO:tensorflow:Graph was finalized.
WARNING:tensorflow:From /anaconda3/lib/python3.7/site-packages/tensorflow/python/training/saver.py:1266: checkpoint_exists (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.
Instructions for updating:
Use standard file APIs to check for files with this prefix.
INFO:tensorflow:Restoring parameters from /var/folders/yh/7gktt0ls0fj77fnrs694ht6m0000gn/T/tmp5bz6d2dd/model.ckpt-300
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Finished evaluation at 2019-05-17-14:51:52
INFO:tensorflow:Saving dict for global step 300: accuracy = 0.72727275, accuracy_baseline = 0.64935064, auc = 0.7474897, auc_precision_recall = 0.6184933, average_loss = 0.5803154, global_step = 300, label/mean = 0.35064936, loss = 22.342142, precision = 0.875, prediction/mean = 0.35842043, recall = 0.25925925
INFO:tensorflow:Saving 'checkpoint_path' summary for global step 300: /var/folders/yh/7gktt0ls0fj77fnrs694ht6m0000gn/T/tmp5bz6d2dd/model.ckpt-300
In [24]:
results
Out[24]:
{'accuracy': 0.72727275,
 'accuracy_baseline': 0.64935064,
 'auc': 0.7474897,
 'auc_precision_recall': 0.6184933,
 'average_loss': 0.5803154,
 'label/mean': 0.35064936,
 'loss': 22.342142,
 'precision': 0.875,
 'prediction/mean': 0.35842043,
 'recall': 0.25925925,
 'global_step': 300}

No comments:

Post a Comment

How to Use Publish-Audit-Merge Workflow in Apache Iceberg: A Beginner’s Guide

publish How to Use Publish-Audit-Merge Workflow in Apache Iceberg: A Beginner’s Guide ¶ In [24]: from ...