<jupyter_start><jupyter_text>00Z Model <NAME> MSDS Capstone Project Fall 2019  Import all Python Libraries and set up seed value<jupyter_code>import os
os.environ['PYTHONHASHSEED']=str(0)

import random
random.seed(0)

from numpy.random import seed
seed(0)

import tensorflow as tf

print(tf.__version__)
from tensorflow import keras
tf.random.set_seed(0)

from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from tensorflow.keras.regularizers import l2, l1, l1_l2


import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import tensorflow as tf
from tensorflow import keras
#tf.keras.wrappers.skikit_learn.KerasClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import make_classification
from sklearn.metrics import confusion_matrix

from sklearn.metrics import classification_report, accuracy_score, confusion_matrix


import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output>2.0.0-beta1<jupyter_text>Get the data and split into Train/Test sets<jupyter_code>data = pd.read_csv(r'D:\MSDS-Capstone-Project\implementation\Data\00Z-randomized.csv')
data.rename( columns={'Unnamed: 0':'Date'}, inplace=True )
data['Date'] = pd.to_datetime(data['Date'])
data.set_index('Date', inplace=True)
data = data.sample(frac=1)
data.loc[data.COUNT < 5, 'countBinary2'] = 0
data.loc[data.COUNT >= 5, 'countBinary2'] = 1
data
# creating input featues and target variables
X = data.iloc[:,0:26]
y = data.iloc[:,28]
X.shape
X.tail()
y.value_counts()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.25, random_state=0)
scaler = StandardScaler().fit(X_train)
X_Train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
X_val = scaler.transform(X_val)<jupyter_output><empty_output><jupyter_text>Function to plot the losses<jupyter_code>def plot_history(hist):
    fig = plt.figure(figsize=(6,4))
    # # summarize history for loss
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'], 'g--')
    plt.title('Classification Model Loss')
    plt.ylabel('Binary Crossentropy')
    plt.xlabel('Epoch')
    plt.legend(['Training Loss', 'Testing Loss'], loc='upper right')
    print ("Loss after final iteration: ", history.history['val_loss'][-1])
    plt.show()<jupyter_output><empty_output><jupyter_text>Machine Learning Models  Attempt 1<jupyter_code>model = keras.Sequential()
model.add(keras.layers.Dense(units=26, activation='relu', kernel_initializer=keras.initializers.he_uniform(seed=0), 
                             kernel_regularizer=l1_l2(0.001), input_shape=(26,)))
model.add(keras.layers.Dense(units=26, activation='relu', kernel_regularizer=l1_l2(0.001)))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(units=1, activation='sigmoid', kernel_regularizer=l1_l2(0.001)))
model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.SGD(learning_rate = 0.001, momentum=0.5), 
              metrics=['accuracy'])

history = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs = 10, batch_size=6500)
eval_model=model.evaluate(X_train, y_train)
print(eval_model)
model.evaluate(X_test, y_test, verbose=0)
plot_history(history)
predictions = pd.DataFrame(model.predict(X_test))


predictions[predictions > 0.5] = 1 #'5 or more Tornadoes'
predictions[predictions <= 0.5] = 0 #'Less than 5 Tornadoes'

print('accuracy', accuracy_score(predictions, y_test))
print('confusion matrix\n', confusion_matrix(predictions,y_test))

print(classification_report(predictions, y_test))<jupyter_output>accuracy 0.7676965193586234
confusion matrix
 [[1903  255]
 [ 339   60]]
              precision    recall  f1-score   support

         0.0       0.85      0.88      0.86      2158
         1.0       0.19      0.15      0.17       399

   micro avg       0.77      0.77      0.77      2557
   macro avg       0.52      0.52      0.52      2557
weighted avg       0.75      0.77      0.76      2557<jupyter_text>Attempt 2, using Adadelta optimizer<jupyter_code>model = keras.Sequential()
model.add(keras.layers.Dense(units=26, activation='relu', kernel_initializer=keras.initializers.he_uniform(seed=0), 
                             kernel_regularizer=l1_l2(0.001), input_shape=(26,)))
model.add(keras.layers.Dense(units=26, activation='relu', kernel_regularizer=l1_l2(0.001)))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(units=1, activation='sigmoid', kernel_regularizer=l1_l2(0.001)))
model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])

history = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs = 10, batch_size=6500)
eval_model=model.evaluate(X_train, y_train)
print(eval_model)
model.evaluate(X_test, y_test, verbose=0)
plot_history(history)
predictions = pd.DataFrame(model.predict(X_test))
predictions[predictions > 0.5] = 1 #'5 or more Tornadoes'
predictions[predictions <= 0.5] = 0 #'Less than 5 Tornadoes'

print('accuracy', accuracy_score(predictions, y_test))
print('confusion matrix\n', confusion_matrix(predictions,y_test))

print(classification_report(predictions, y_test))<jupyter_output>accuracy 0.38834571763785686
confusion matrix
 [[ 728   50]
 [1514  265]]
              precision    recall  f1-score   support

         0.0       0.32      0.94      0.48       778
         1.0       0.84      0.15      0.25      1779

   micro avg       0.39      0.39      0.39      2557
   macro avg       0.58      0.54      0.37      2557
weighted avg       0.68      0.39      0.32      2557<jupyter_text>Attempt 3, Adagrad<jupyter_code>model = keras.Sequential()
model.add(keras.layers.Dense(units=26, activation='relu', kernel_initializer=keras.initializers.he_uniform(seed=0), 
                             kernel_regularizer=l1_l2(0.001), input_shape=(26,)))
model.add(keras.layers.Dense(units=26, activation='relu', kernel_regularizer=l1_l2(0.001)))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(units=1, activation='sigmoid', kernel_regularizer=l1_l2(0.001)))
model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adagrad(), metrics=['accuracy'])

history = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs = 10, batch_size=6500)
eval_model=model.evaluate(X_train, y_train)
print(eval_model)
model.evaluate(X_test, y_test, verbose=0)
plot_history(history)
predictions = pd.DataFrame(model.predict(X_test))
predictions[predictions > 0.5] = 1 #'5 or more Tornadoes'
predictions[predictions <= 0.5] = 0 #'Less than 5 Tornadoes'

print('accuracy', accuracy_score(predictions, y_test))
print('confusion matrix\n', confusion_matrix(predictions,y_test))

print(classification_report(predictions, y_test))<jupyter_output>accuracy 0.3343762221353148
confusion matrix
 [[ 655  115]
 [1587  200]]
              precision    recall  f1-score   support

         0.0       0.29      0.85      0.43       770
         1.0       0.63      0.11      0.19      1787

   micro avg       0.33      0.33      0.33      2557
   macro avg       0.46      0.48      0.31      2557
weighted avg       0.53      0.33      0.26      2557<jupyter_text>Attempt 4, Adam<jupyter_code>model = keras.Sequential()
model.add(keras.layers.Dense(units=26, activation='relu', kernel_initializer=keras.initializers.he_uniform(seed=0), 
                             kernel_regularizer=l1_l2(0.001), input_shape=(26,)))
model.add(keras.layers.Dense(units=26, activation='relu', kernel_regularizer=l1_l2(0.001)))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(units=1, activation='sigmoid', kernel_regularizer=l1_l2(0.001)))
model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adam(learning_rate=0.001), 
              metrics=['accuracy'])



history = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs = 10, batch_size=6500)
eval_model=model.evaluate(X_train, y_train)
print(eval_model)
model.evaluate(X_test, y_test, verbose=0)
plot_history(history)
predictions = pd.DataFrame(model.predict(X_test))

#predictions

predictions[predictions > 0.5] = 1 #'5 or more Tornadoes'
predictions[predictions <= 0.5] = 0 #'Less than 5 Tornadoes'

print('accuracy', accuracy_score(predictions, y_test))
print('confusion matrix\n', confusion_matrix(predictions,y_test))

print(classification_report(predictions, y_test))<jupyter_output>accuracy 0.7027766914352758
confusion matrix
 [[1693  211]
 [ 549  104]]
              precision    recall  f1-score   support

         0.0       0.76      0.89      0.82      1904
         1.0       0.33      0.16      0.21       653

   micro avg       0.70      0.70      0.70      2557
   macro avg       0.54      0.52      0.52      2557
weighted avg       0.65      0.70      0.66      2557<jupyter_text>Attempt 5, NAdam<jupyter_code>model = keras.Sequential()
model.add(keras.layers.Dense(units=26, activation='relu', kernel_initializer=keras.initializers.he_uniform(seed=0), 
                             kernel_regularizer=l1_l2(0.001), input_shape=(26,)))
model.add(keras.layers.Dense(units=26, activation='relu', kernel_regularizer=l1_l2(0.001)))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(units=1, activation='sigmoid', kernel_regularizer=l1_l2(0.001)))
model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Nadam(learning_rate=0.003), 
              metrics=['accuracy'])



history = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs = 10, batch_size=6500)
eval_model=model.evaluate(X_train, y_train)
print(eval_model)
model.evaluate(X_test, y_test, verbose=0)
plot_history(history)
predictions = pd.DataFrame(model.predict(X_test))

#predictions

predictions[predictions > 0.5] = 1 #'5 or more Tornadoes'
predictions[predictions <= 0.5] = 0 #'Less than 5 Tornadoes'

print('accuracy', accuracy_score(predictions, y_test))
print('confusion matrix\n', confusion_matrix(predictions,y_test))

print(classification_report(predictions, y_test))<jupyter_output>accuracy 0.872897927258506
confusion matrix
 [[2191  274]
 [  51   41]]
              precision    recall  f1-score   support

         0.0       0.98      0.89      0.93      2465
         1.0       0.13      0.45      0.20        92

   micro avg       0.87      0.87      0.87      2557
   macro avg       0.55      0.67      0.57      2557
weighted avg       0.95      0.87      0.90      2557
{'fraction_non_alphanumeric': 0.12297489341544292, 'fraction_numerical': 0.07266698247276172, 'mean_word_length': 3.96519285042333, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '187048', 'license': 'MIT', 'n_tokens_mistral': 4153, 'n_tokens_neox': 3760, 'n_words': 685, 'path': 'implementation/00Z Model.ipynb', 'repo_name': 'sburgholzer/MSDS-Capstone-Project', 'size': '10565'}