<jupyter_start><jupyter_text><jupyter_code>#Description: This program detects if a person has diabetes or not
#Load Libraries
from keras.models import Sequential
from keras.layers import Dense
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
#Load the Data
from google.colab import files
uploaded = files.upload()
#Store the data set
df = pd.read_csv('datasets_228_482_diabetes.csv')

#Print the first 7 rows odf data
df.head(7)
#Show the shape (No. of rows and columns)
df.shape
#Check for duplicacies and remove them
df.drop_duplicates(inplace= True)
#Show the shape (No. of rows and columns)
df.shape
#show the no. of missing data for each column
df.isnull().sum()
#Convert the data into array
dataset = df.values
dataset
#Get all of the rows from the first eight columns of the dataset
X = dataset[:, 0:8]
Y = dataset[:, 8]

#Process the data
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)
X_scale
#Split the data into 80% training and 20% testing
X_train, X_test, Y_train, Y_test = train_test_split(X_scale,Y, test_size =0.2, random_state = 4)
#Build the Model

model = Sequential([
             Dense(12, activation='relu', input_shape=(8,)),
             Dense(15, activation='relu'),
             Dense(1, activation='sigmoid')       
])
#Compile the Model
model.compile(
    optimizer = 'sgd',
    loss = 'binary_crossentropy',
    metrics = ['accuracy']

)
#Train the Model
hist = model.fit(X_train, Y_train, batch_size = 57, epochs=1000, validation_split=0.2)
#Visualize the training loss and validation loss to see if the model is overfitting
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('Model loss')
plt.ylabel('loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc = 'upper right')
plt.show()
#Visualize the training accuracy and validation accuracy to see if the model is overfitting
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc = 'lower right')
plt.show()
#Make a prediction & print the actual values
prediction = model.predict(X_test)
prediction = [1 if Y>= 0.5 else 0 for Y in prediction]
print(prediction)
print(Y_test)
#Evaluate the model on the training dataset
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

pred = model.predict(X_train)
pred = [1 if Y>= 0.5 else 0 for Y in pred]
print(classification_report(Y_train, pred))
print('Confusion Matrix: \n', confusion_matrix(Y_train, pred))
print()
print('Accuracy: ', accuracy_score(Y_train, pred))
#Evaluate the model on the test dataset
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

pred = model.predict(X_test)
pred = [1 if Y>= 0.5 else 0 for Y in pred]
print(classification_report(Y_test, pred))
print('Confusion Matrix: \n', confusion_matrix(Y_test, pred))
print()
print('Accuracy: ', accuracy_score(Y_test, pred))<jupyter_output>precision    recall  f1-score   support

         0.0       0.84      0.86      0.85       102
         1.0       0.71      0.67      0.69        52

    accuracy                           0.80       154
   macro avg       0.78      0.77      0.77       154
weighted avg       0.80      0.80      0.80       154

Confusion Matrix: 
 [[88 14]
 [17 35]]

Accuracy:  0.7987012987012987
{'fraction_non_alphanumeric': 0.10888825378679623, 'fraction_numerical': 0.03629608459559874, 'mean_word_length': 4.079825834542816, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '648514', 'license': 'MIT', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1128, 'n_words': 366, 'path': 'DiabetesPrediction(ArtificialNeuralNetwork).ipynb', 'repo_name': 'GauravKakoti/Data-Science', 'size': '3499'}