<jupyter_start><jupyter_text>Pima-Diabetes-Prediction<jupyter_code>import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.model_selection import KFold 
from sklearn.model_selection import RepeatedKFold
from sklearn.cross_validation import train_test_split
from  sklearn.ensemble import GradientBoostingClassifier
from sklearn.cross_validation import train_test_split, cross_val_score
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import accuracy_score, roc_auc_score, roc_curve
from sklearn.preprocessing import binarize
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

from __future__ import print_function
import tensorflow as tf
from six.moves import range
import sys

from IPython.display import display, Image

# Config the matlotlib backend as plotting inline in IPython
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
import seaborn as sns
from keras.models import Sequential

from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import confusion_matrix,classification_report

from tensorflow.contrib import keras
from keras import models, layers, losses, optimizers, metrics
import os
col_names = ['pregnant', 'glucose', 'bp', 'skin', 'insulin', 'bmi', 'pedigree', 'age', 'label']
data = pd.read_csv('pima-indians-diabetes.data', names=col_names)

data.count()
data.head()
data.info()
data.shape
data.describe()
data.isnull().values.any()
def plot_correlated(data, size=11):
    """
    Function to show plot of correlation between the data columns
    data : the data from pandas Dataframe
    size : the horizontal and vertical size of the plot
    
    Result : blue - cyan - yellow - red -darkred = less to more correlated columns = 0 ---- 1 
    """
    corr = data.corr()
    fig, ax = plt.subplots(figsize=(size, size))
    ax.matshow(corr)
    plt.xticks(range(len(corr.columns)), corr.columns)
    plt.yticks(range(len(corr.columns)), corr.columns)
data.corr()
feature_columns = ['pregnant', 'glucose', 'bp', 'skin', 'insulin', 'bmi', 'pedigree', 'age']
predicted_class = ['label']

X = data[feature_columns].values
y = data[predicted_class].values
#split_test_size =0.30

#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = split_test_size, random_state=10)
label = data['label']
data.drop('label', axis=1, inplace=True)
X_p, y_p = data, label
# selection of relevant features
#label = data['Outcome']
#data.drop('', axis=1, inplace=True)

#X, y = data, label
#X_p = pd.read_csv("diabetes.csv", usecols = [0,1,2,3,4,5,6,7])
#y_p = pd.read_csv("diabetes.csv", usecols = ['Outcome'])


#X_p.head(5)


#X_train.shape
print(X)
#from sklearn.model_selection import KFold 
kf = RepeatedKFold(n_splits=5, n_repeats=10, random_state=10) 

for train_index, test_index in kf.split(X):
    #print("Train:", train_index, "Validation:",test_index)
    X_train, X_test = X[train_index], X[test_index] 
    y_train, y_test = y[train_index], y[test_index]
X_train.shape
y_train.shape
X_test.shape
y_test.shape
from sklearn.linear_model import LogisticRegression

log_reg_model = LogisticRegression(C=1.0, random_state=10)

log_reg_model.fit(X_train, y_train.ravel())
lr_predict_train_data = log_reg_model.predict(X_train)

from sklearn import metrics

print("Accuracy = {0:.3f}".format(metrics.accuracy_score(y_train, lr_predict_train_data)))
lr_predict_test_data = log_reg_model.predict(X_test)

from sklearn import metrics


print("Accuracy = {0:.3f}".format(metrics.accuracy_score(y_test, lr_predict_test_data)))
print("Confusion matrix")

print("{0}".format(metrics.confusion_matrix(y_test, lr_predict_test_data, labels=[1,0])))

print("Classification report")

print("{0}".format(metrics.classification_report(y_test, lr_predict_test_data, labels=[1,0])))
log_reg_model = LogisticRegression(C=1.0, class_weight="balanced", random_state=11)

log_reg_model.fit(X_train, y_train.ravel())

lr_predict_test_data = log_reg_model.predict(X_test)

print("Accuracy = {0:.3f}".format(metrics.accuracy_score(y_test, lr_predict_test_data)))
print("Confusion matrix")

print("{0}".format(metrics.confusion_matrix(y_test, lr_predict_test_data, labels=[1,0])))

print("Classification report")

print("{0}".format(metrics.classification_report(y_test, lr_predict_test_data, labels=[1,0])))

from sklearn.linear_model import LogisticRegressionCV
lrcv_model = LogisticRegressionCV(n_jobs=-1, random_state=10, Cs=10, cv=5, refit=True, class_weight="balanced")
lrcv_model.fit(X, y.ravel())
lrcv_predict_test_data = lrcv_model.predict(X_test)
print("Accuracy = {0:.3f}".format(metrics.accuracy_score(y_test, lrcv_predict_test_data)))
print("Confusion matrix")

print("{0}".format(metrics.confusion_matrix(y_test, lrcv_predict_test_data, labels=[1,0])))

print("Classification report")

print("{0}".format(metrics.classification_report(y_test, lrcv_predict_test_data, labels=[1,0])))

from sklearn.ensemble  import GradientBoostingClassifier
from sklearn.cross_validation import train_test_split, cross_val_score
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import accuracy_score, roc_auc_score, roc_curve
from sklearn.preprocessing import binarize
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
%%time
# GridSearch procedure = cpu demanding
gradient_boost_eval = GradientBoostingClassifier(random_state=0)

params = {
    'learning_rate': [0.05, 0.1, 0.5],
    'max_features': [0.5, 1],
    'max_depth': [3, 4, 5] 
         }

grid_search = GridSearchCV(gradient_boost_eval, params, cv=10, scoring='roc_auc')
grid_search.fit(X_p,y_p)

print(grid_search.best_params_)
print(grid_search.best_score_)
# GBC belong to the Random Tree Classifier family
# a complex model who perform well on small Dataset
# We set our hyper-params with the best params providing by GridSearch
gradient_boost = GradientBoostingClassifier(learning_rate=0.05, max_depth=3, max_features=0.5, random_state=0)

#Cross Validation handle the .fit and .predict method
cross_val_score(gradient_boost, X_p, y_p, cv=10, scoring='roc_auc').mean()
#X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
#from sklearn.model_selection import KFold 
kf = RepeatedKFold(n_splits=10, n_repeats=10, random_state=10) 

for train_index, test_index in kf.split(X_p):
    #print("Train:", train_index, "Validation:",test_index)
    X_train, X_test = X[train_index], X[test_index] 
    y_train, y_test = y[train_index], y[test_index]
#Adjusting the Threshold for improvement
gradient_boost_imp = GradientBoostingClassifier(learning_rate=0.05, max_depth=3, max_features=0.5, random_state=0)



gradient_boost_imp.fit(X_train, y_train)
# Storing the predicted probabilities of classification
y_pred_prob = gradient_boost_imp.predict_proba(X_test)[:, 1]
# level of predictive precision
roc_auc_score(y_test, y_pred_prob)<jupyter_output><empty_output><jupyter_text>using Keras Data Scaling We scale the data so as to increase the model accuracy.<jupyter_code>scaler = MinMaxScaler()

scaled_x_train = scaler.fit_transform(X_train)
scaled_x_test = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>KERAS DATA MODELWe will use the following parameters in our implementation -- Hidden Layers - 3 , each consisiting of 8 neurons.- Activation - RELU for hidden, SOFTMAX for output layer.- Optimizer - SGD- Learning Rate Decay - 0.0001- L2 Regularization<jupyter_code># Create Keras DNN Model

model = models.Sequential()

# Hyperparameters
hold_prob = 0.0
beta = 1e-6
alpha = 0.05
lr_decay = 0.01
iterations = 1000
validation_split = 0.5
opt_momentum = 0.8 # (Use only for SGD)

# Optimizer
opt = optimizers.SGD(lr=alpha, decay=lr_decay, momentum=opt_momentum, nesterov=True)

# First Layer
model.add(layers.Dense(input_dim=8, units=8, activation='relu'))

# Hidden Layers
model.add(layers.Dense(units=8, activation='relu', kernel_regularizer=keras.regularizers.l2(beta)))
model.add(layers.Dropout(hold_prob))

model.add(layers.Dense(units=8, activation='relu', kernel_regularizer=keras.regularizers.l2(beta)))
model.add(layers.Dropout(hold_prob))

model.add(layers.Dense(units=8, activation='relu', kernel_regularizer=keras.regularizers.l2(beta)))
model.add(layers.Dropout(hold_prob))

# Output Layer
model.add(layers.Dense(units=2, activation='softmax'))

# Compiling the Model
model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train
model.fit(x=scaled_x_train, y=y_train, epochs=iterations, validation_split=validation_split)
predictions = model.predict_classes(scaled_x_test)
print(classification_report(y_test, predictions))<jupyter_output>precision    recall  f1-score   support

          0       0.72      0.82      0.77        44
          1       0.69      0.56      0.62        32

avg / total       0.71      0.71      0.70        76<jupyter_text>Accuracy on test data set<jupyter_code>roc_auc_score(y_test,predictions )<jupyter_output><empty_output><jupyter_text>Accuracy on train data set<jupyter_code>predictions = model.predict_classes(scaled_x_train)
roc_auc_score(y_train,predictions)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11420157068062828, 'fraction_numerical': 0.019197207678883072, 'mean_word_length': 6.418284789644013, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '423124', 'license': 'MIT', 'n_tokens_mistral': 3344, 'n_tokens_neox': 3205, 'n_words': 690, 'path': 'Pima-Diabetes-Prediction/PIMA.ipynb', 'repo_name': 'code-neverends/Machine-Learning-Projects', 'size': '9168'}