<jupyter_start><jupyter_text><jupyter_code>sns.countplot(data['Class variable (0 or 1)'])
plt.show()
#Univariate Analysis of Age and BMI
x_axis = ["Age (years)", "Body mass index (weight in kg/(height in m)^2)", "Class variable (0 or 1)"]
for x in x_axis:
    fig, axes = plt.subplots(1, 2, figsize=(18,4))
    sns.distplot(data[x], ax=axes[0], kde = False)
    sns.boxplot(data[x], ax=axes[1], orient = "h", showmeans = True, color = "pink")
    fig.suptitle('Univariate Analysis of ' + x, fontsize=16)
#Bivariate Analysis of Age vs BMI
plt.scatter(data["Age (years)"], data["Body mass index (weight in kg/(height in m)^2)"])
plt.xlabel("Age", fontsize = 13)
plt.ylabel("BMI", fontsize = 13)
plt.title("Scatter Plot", fontsize = 15)
plt.grid()
plt.show()
#Univariate Analysis of Age and Diastolic blood pressure
x_axis = ["Age (years)", "Diastolic blood pressure (mm Hg)", "Class variable (0 or 1)"]
for x in x_axis:
    fig, axes = plt.subplots(1, 2, figsize=(18,4))
    sns.distplot(data[x], ax=axes[0], kde = False)
    sns.boxplot(data[x], ax=axes[1], orient = "h", showmeans = True, color = "pink")
    fig.suptitle('Univariate Analysis of ' + x, fontsize=16)
#Bivariate Analysis of Age vs Diastolic Blood Pressure
plt.scatter(data["Age (years)"], data["Diastolic blood pressure (mm Hg)"])
plt.xlabel("Age", fontsize = 13)
plt.ylabel("Diastolic Blood Pressure", fontsize = 13)
plt.title("Scatter Plot", fontsize = 15)
plt.grid()
plt.show()
import scipy.stats as stats
fvalue, pvalue = stats.ttest_ind(data[data["Body mass index (weight in kg/(height in m)^2)"] == 1]["Class variable (0 or 1)"],
                               data[data["Body mass index (weight in kg/(height in m)^2)"] == 0]["Class variable (0 or 1)"])
fvalue, pvalue

plt.scatter(data["Body mass index (weight in kg/(height in m)^2)"], data["Class variable (0 or 1)"])
plt.xlabel("Body mass index (weight in kg/(height in m)^2)", fontsize = 13)
plt.ylabel("Class variable (0 or 1)", fontsize = 13)
plt.title("Scatter Plot", fontsize = 15)
plt.grid()
plt.show()
sns.boxplot(x = "Class variable (0 or 1)", y = "Body mass index (weight in kg/(height in m)^2)", data = data, showmeans = True)
plt.figure(figsize = (15,5))
sns.scatterplot(x = "Class variable (0 or 1)", y = "Age (years)", data = data, hue = "Body mass index (weight in kg/(height in m)^2)")
plt.title("Scatter Plot with Purchase Details", fontsize = 14)
plt.show()
# Multivariate Analysis
sns.pairplot(data1,hue="Class variable (0 or 1)",size=3)
plt.show()
#Logistic Regression model only with Age and BMI
X = data.iloc[:, [5, 7]].values
y = data.iloc[:, -1].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Training the Logistic Regression model on the Training set
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)

# Visualising the Training set results
from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Logistic Regression (Training set)')
plt.xlabel('BMI')
plt.ylabel('Age')
plt.legend()
plt.show()

accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
print(accuracy)
print(misclass)
##Logistic Regression Classifier
X = data.iloc[:, [0,1,2,3,4,5,6,7]].values
y = data.iloc[:, -1].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
from sklearn.metrics import confusion_matrix

from sklearn.metrics import classification_report, confusion_matrix
labels = ['Diabetes', 'Non-Diabetes']
cm = confusion_matrix(y_test, y_pred)
print(cm)
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(cm)
plt.title('Confusion matrix of the classifier\n')
fig.colorbar(cax)
ax.set_xticklabels(['1'] + labels)
ax.set_yticklabels(['2'] + labels)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
print(classification_report(y_test,y_pred))
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
print(accuracy)
print(misclass)
#Accuracy Rate for Logistic Regression Classifier 
from sklearn.metrics import roc_curve, auc
total = len(y_test) 
  
# Counting '1' labels in test data 
one_count = np.sum(y_test) 
  
# counting '0' lables in test data  
zero_count = total - one_count 
  
plt.figure(figsize = (10, 6)) 
  
# x-axis ranges from 0 to total people contacted  
# y-axis ranges from 0 to the total positive outcomes. 
  
plt.plot([0, total], [0, one_count], c = 'r',  
         linestyle = '--', label = 'Random Model') 
plt.legend() 
lm = [y for _, y in sorted(zip(y_pred, y_test), reverse = True)] 
x = np.arange(0, total + 1) 
y = np.append([0], np.cumsum(lm)) 
plt.plot(x, y, c = 'b', label = 'Logistic Regression Classifier', linewidth = 2) 
plt.legend() 
plt.plot([0, one_count, total], [0, one_count, one_count], 
         c = 'grey', linewidth = 2, label = 'Perfect Model')
plt.legend() 

a = auc([0, total], [0, one_count])
# Area between Perfect and Random Model
aP = auc([0, one_count, total], [0, one_count, one_count]) - a

# Area between Trained and Random Model
aR = auc(x, y) - a

print("Accuracy Rate for Logistic Regression Classifier: {}".format(aR/aP))
#K-NN model
X = data.iloc[:, [0,1,2,3,4,5,6,7]].values
y = data.iloc[:, -1].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Training the K-NN model on the Training set
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
print(classification_report(y_test,y_pred))
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
print(accuracy)
print(misclass)
#Accuracy Rate for K-NN Classifier 
from sklearn.metrics import roc_curve, auc
total = len(y_test) 
  
# Counting '1' labels in test data 
one_count = np.sum(y_test) 
  
# counting '0' lables in test data  
zero_count = total - one_count 
  
plt.figure(figsize = (10, 6)) 
  
# x-axis ranges from 0 to total people contacted  
# y-axis ranges from 0 to the total positive outcomes. 
  
plt.plot([0, total], [0, one_count], c = 'b',  
         linestyle = '--', label = 'Random Model') 
plt.legend() 
lm = [y for _, y in sorted(zip(y_pred, y_test), reverse = True)] 
x = np.arange(0, total + 1) 
y = np.append([0], np.cumsum(lm)) 
plt.plot(x, y, c = 'b', label = 'K-NN Classifier ', linewidth = 2) 
plt.legend() 
plt.plot([0, one_count, total], [0, one_count, one_count], 
         c = 'grey', linewidth = 2, label = 'Perfect Model')
plt.legend() 

a = auc([0, total], [0, one_count])
# Area between Perfect and Random Model
aP = auc([0, one_count, total], [0, one_count, one_count]) - a

# Area between Trained and Random Model
aR = auc(x, y) - a

print("Accuracy Rate for  K-NN Classifier Classifier: {}".format(aR/aP))
#SVC implementation (kernel = 'linear')
from sklearn.svm import SVC
classifier = SVC(kernel = 'linear', random_state = 0)
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
print(classification_report(y_test,y_pred))
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
print(accuracy)
print(misclass)
#Accuracy Rate for Support Vector Classifier(SVC model kernel = 'linear')
from sklearn.metrics import roc_curve, auc
total = len(y_test) 
  
# Counting '1' labels in test data 
one_count = np.sum(y_test) 
  
# counting '0' lables in test data  
zero_count = total - one_count 
  
plt.figure(figsize = (10, 6)) 
  
# x-axis ranges from 0 to total people contacted  
# y-axis ranges from 0 to the total positive outcomes. 
  
plt.plot([0, total], [0, one_count], c = 'r',  
         linestyle = '--', label = 'Random Model') 
plt.legend() 
lm = [y for _, y in sorted(zip(y_pred, y_test), reverse = True)] 
x = np.arange(0, total + 1) 
y = np.append([0], np.cumsum(lm)) 
plt.plot(x, y, c = 'b', label = 'Support Vector Classifier', linewidth = 2) 
plt.legend() 
plt.plot([0, one_count, total], [0, one_count, one_count], 
         c = 'grey', linewidth = 2, label = 'Perfect Model')
plt.legend() 

a = auc([0, total], [0, one_count])
# Area between Perfect and Random Model
aP = auc([0, one_count, total], [0, one_count, one_count]) - a

# Area between Trained and Random Model
aR = auc(x, y) - a

print("Accuracy Rate for Support Vector Classifier kernel = 'linear': {}".format(aR/aP))
#SVC implementation (kernel = 'rbf')
# Training the SVM model on the Training set
from sklearn.svm import SVC
classifier = SVC(kernel = 'rbf', random_state = 0)
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
print(accuracy)
print(misclass)
print(classification_report(y_test,y_pred))
#Accuracy Rate for Support Vector Classifier(SVC model kernel = 'rbf')
from sklearn.metrics import roc_curve, auc
total = len(y_test) 
  
# Counting '1' labels in test data 
one_count = np.sum(y_test) 
  
# counting '0' lables in test data  
zero_count = total - one_count 
  
plt.figure(figsize = (10, 6)) 
  
# x-axis ranges from 0 to total people contacted  
# y-axis ranges from 0 to the total positive outcomes. 
  
plt.plot([0, total], [0, one_count], c = 'b',  
         linestyle = '--', label = 'Random Model') 
plt.legend() 
lm = [y for _, y in sorted(zip(y_pred, y_test), reverse = True)] 
x = np.arange(0, total + 1) 
y = np.append([0], np.cumsum(lm)) 
plt.plot(x, y, c = 'b', label = 'Support Vector Classifier', linewidth = 2) 
plt.legend() 
plt.plot([0, one_count, total], [0, one_count, one_count], 
         c = 'grey', linewidth = 2, label = 'Perfect Model')
plt.legend() 

a = auc([0, total], [0, one_count])
# Area between Perfect and Random Model
aP = auc([0, one_count, total], [0, one_count, one_count]) - a

# Area between Trained and Random Model
aR = auc(x, y) - a

print("Accuracy Rate for Support Vector Classifier kernel = 'rbf: {}".format(aR/aP))
#Naive Bayes classifier
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Training the Naive Bayes model on the Training set
from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
print(accuracy)
print(misclass)
print(classification_report(y_test,y_pred))
#Accuracy Rate for Naive Bayes  Classifier
from sklearn.metrics import roc_curve, auc
total = len(y_test) 
  
# Counting '1' labels in test data 
one_count = np.sum(y_test) 
  
# counting '0' lables in test data  
zero_count = total - one_count 
  
plt.figure(figsize = (10, 6)) 
  
# x-axis ranges from 0 to total people contacted  
# y-axis ranges from 0 to the total positive outcomes. 
  
plt.plot([0, total], [0, one_count], c = 'b',  
         linestyle = '--', label = 'Random Model') 
plt.legend() 
lm = [y for _, y in sorted(zip(y_pred, y_test), reverse = True)] 
x = np.arange(0, total + 1) 
y = np.append([0], np.cumsum(lm)) 
plt.plot(x, y, c = 'b', label = 'Naive Bayes Classifier', linewidth = 2) 
plt.legend() 
plt.plot([0, one_count, total], [0, one_count, one_count], 
         c = 'grey', linewidth = 2, label = 'Perfect Model')
plt.legend() 

a = auc([0, total], [0, one_count])
# Area between Perfect and Random Model
aP = auc([0, one_count, total], [0, one_count, one_count]) - a

# Area between Trained and Random Model
aR = auc(x, y) - a

print("Accuracy Rate for Naive Bayes Classifier-: {}".format(aR/aP))
# Decision Tree Classifier
from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
print(accuracy)
print(misclass)
print(classification_report(y_test,y_pred))
#Accuracy Rate for Decision Tree Classifier
from sklearn.metrics import roc_curve, auc
total = len(y_test) 
  
# Counting '1' labels in test data 
one_count = np.sum(y_test) 
  
# counting '0' lables in test data  
zero_count = total - one_count 
  
plt.figure(figsize = (10, 6)) 
  
# x-axis ranges from 0 to total people contacted  
# y-axis ranges from 0 to the total positive outcomes. 
  
plt.plot([0, total], [0, one_count], c = 'b',  
         linestyle = '--', label = 'Random Model') 
plt.legend() 
lm = [y for _, y in sorted(zip(y_pred, y_test), reverse = True)] 
x = np.arange(0, total + 1) 
y = np.append([0], np.cumsum(lm)) 
plt.plot(x, y, c = 'b', label = 'Decision Tree Classifier ', linewidth = 2) 
plt.legend() 
plt.plot([0, one_count, total], [0, one_count, one_count], 
         c = 'grey', linewidth = 2, label = 'Perfect Model')
plt.legend() 

a = auc([0, total], [0, one_count])
# Area between Perfect and Random Model
aP = auc([0, one_count, total], [0, one_count, one_count]) - a

# Area between Trained and Random Model
aR = auc(x, y) - a

print("Accuracy Rate for Decision Tree Classifier -: {}".format(aR/aP))
#Random Forest Classifier
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
print(accuracy)
print(misclass)
print(classification_report(y_test,y_pred))
#Accuracy Rate for Random Forest Classifier
from sklearn.metrics import roc_curve, auc
total = len(y_test) 
  
# Counting '1' labels in test data 
one_count = np.sum(y_test) 
  
# counting '0' lables in test data  
zero_count = total - one_count 
  
plt.figure(figsize = (10, 6)) 
  
# x-axis ranges from 0 to total people contacted  
# y-axis ranges from 0 to the total positive outcomes. 
  
plt.plot([0, total], [0, one_count], c = 'b',  
         linestyle = '--', label = 'Random Model') 
plt.legend() 
lm = [y for _, y in sorted(zip(y_pred, y_test), reverse = True)] 
x = np.arange(0, total + 1) 
y = np.append([0], np.cumsum(lm)) 
plt.plot(x, y, c = 'b', label = 'Random Forest Classifier ', linewidth = 2) 
plt.legend() 
plt.plot([0, one_count, total], [0, one_count, one_count], 
         c = 'grey', linewidth = 2, label = 'Perfect Model')
plt.legend() 

a = auc([0, total], [0, one_count])
# Area between Perfect and Random Model
aP = auc([0, one_count, total], [0, one_count, one_count]) - a

# Area between Trained and Random Model
aR = auc(x, y) - a

print("Accuracy Rate for Random Forest Classifier: {}".format(aR/aP))
#Artificial Neural Network (ANN) implementation
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import preprocessing
from tensorflow.keras import models
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.preprocessing import image
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.models import Sequential
import numpy as np
#### build the model
model = tf.keras.models.Sequential()
#### adding the layers (1 layers)
model.add(tf.keras.layers.Dense(len(X)
,input_dim=8,activation='relu')) ## input layer
#### adding the layers (1 layers)
model.add(tf.keras.layers.Dense(1,activation='sigmoid')) ## output layer
### compile the model
model.compile(loss='mean_squared_error',optimizer='adam',metrics=['binary_accuracy'])
#### Train the model
#history = model.fit(X_train, y_train,epochs=70)
history = model.fit(X_train, y_train, validation_split=0.33, epochs=150, batch_size=10, verbose=0)

import matplotlib.pyplot as plt
print(history.history.keys())
# Loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['loss', 'val_loss'], loc='upper left')
plt.show()
print(history.history.keys())
print(history.history['loss'])
plt.plot(history.history['binary_accuracy'])
plt.plot(history.history['val_binary_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['accuracy', 'val_accuracy'], loc='upper left')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1362540399117632, 'fraction_numerical': 0.016262247986456677, 'mean_word_length': 4.062061802129317, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '515714', 'license': 'Unlicense', 'n_tokens_mistral': 7414, 'n_tokens_neox': 7027, 'n_words': 1975, 'path': 'pima_indians_diabetes.ipynb', 'repo_name': 'Vijayp1983/Diabetes-Prediction-using-Pima-Dataset', 'size': '19493'}