Step 1:¶
import modules¶
In [4]:
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix,classification_report
%matplotlib inline
Step 2:¶
process data¶
In [5]:
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:¶
Define Feature Columns¶
In [6]:
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
Create Input Function and Test Function¶
In [7]:
X_Train, X_Test, Y_Train, Y_Test = Data_Process()
feature_column = create_feature_column()
input_func = tf.estimator.inputs.pandas_input_fn(x=X_Train, y=Y_Train,
batch_size=40,num_epochs =1000,
shuffle=True)
eval_input_func = tf.estimator.inputs.pandas_input_fn(x=X_Test,
y=Y_Test,
batch_size=40,
num_epochs=1,
shuffle=False)
Step 4 :¶
Create Linear Classifier Model¶
In [8]:
model = tf.estimator.LinearClassifier(feature_columns=feature_column,
n_classes=2)
Step 5:¶
Train¶
In [10]:
history = model.train(input_fn=input_func, steps = 1000)
Test¶
In [16]:
results = model.evaluate(eval_input_func)
In [20]:
results["accuracy"]
Out[20]:
No comments:
Post a Comment