# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="zJpHYEZ0o5qB"
# ## AlphabetSoupCharity_Optimizer 2
#
# - Added hidden layers 
# - Increased the number of neurons / layer
# - Activation formula use sigmoid more because binary result
# - Using ModelCheckpoint and batches
#
#

# + [markdown] id="-Vjaj_kh_0Jb"
# ## Preprocessing

# + colab={"base_uri": "https://localhost:8080/"} id="5p97Y95Q8cIK" outputId="03199bc5-efac-4a1b-f3fe-066fac492c9e"
 # Mount the drive, last folder lhs, give permissions, copy path
from google.colab import drive
drive.mount('/content/drive')

# + id="vhqjqXZjo5qE"
# Import our dependencies
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import pandas as pd
import tensorflow as tf  
# to create check points
from tensorflow.keras.callbacks import ModelCheckpoint

# Plotting
import matplotlib.pyplot as plt


# + id="RkMYZ6qvYZ6q"

# Greate a check point path
checkpoint_path = "/content/drive/MyDrive/Colab Notebooks/Deep-Learning/checkpoints/weights.{epoch:02d}.hdf5"



# + colab={"base_uri": "https://localhost:8080/", "height": 496} id="a7VDO9rYo5qF" outputId="0f5bd150-697b-43e7-d9a2-c74d8040fe19"
#  Import and read the charity_data.csv.
application_df = pd.read_csv("/content/drive/MyDrive/Colab Notebooks/Deep-Learning/Resources/charity_data.csv")
application_df.head()

# + id="RTU7Smy-o5qG" outputId="e517dc5c-2811-4b4f-8274-148c36a47cd8" colab={"base_uri": "https://localhost:8080/"}
application_df.shape

# + id="wUBE6sW4o5qH" outputId="20ce55f6-5a99-42da-94a6-636d376cdb25" colab={"base_uri": "https://localhost:8080/"}
application_df.info()

# + [markdown] id="v1v_NI5qo5qH"
# ### Data Preparation

# + id="-9F4v1coo5qI"
# Drop the non-beneficial ID columns, 'EIN' and 'NAME'.
application_df.drop(['EIN','NAME'], axis=1, inplace= True)

# + id="mJynnDlxo5qI" outputId="6a6ed364-e51d-4223-eb1d-737a5bfd3870" colab={"base_uri": "https://localhost:8080/", "height": 206}
application_df.head()

# + id="AsLnvJbqo5qJ" outputId="493c65c2-88df-427e-8530-5970ce66ce19" colab={"base_uri": "https://localhost:8080/"}
# Determine the number of unique values in each column.
application_df.nunique()

# + id="KhChMpYro5qJ" outputId="cadb9f82-d74a-4212-fc15-1009b32dd0b0" colab={"base_uri": "https://localhost:8080/"}
# Look at APPLICATION_TYPE value counts for binning
application_value = application_df['APPLICATION_TYPE'].value_counts()
application_value

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="haH7XWcVsEpT" outputId="f074723a-4b22-497d-c47b-9137f9163d01"
# Visualize the value counts of APPLICATION_TYPE
application_value.plot.density()

# + id="CYexIX6Ko5qK" outputId="286d5f61-f9d1-470d-c1c0-9d74f1d3141f" colab={"base_uri": "https://localhost:8080/"}
# Choose a cutoff value and create a list of application types to be replaced
# use the variable name `application_types_to_replace`
application_types_to_replace = list(application_value[application_value < 500].index)
application_types_to_replace


# + id="IHkcXrOoo5qL" outputId="22853e3e-4044-411b-d410-72e7b3ef8aff" colab={"base_uri": "https://localhost:8080/"}
# Replace in dataframe
for app in application_types_to_replace:
    application_df['APPLICATION_TYPE'] = application_df['APPLICATION_TYPE'].replace(app,"Other")

# Check to make sure binning was successfull
application_df['APPLICATION_TYPE'].value_counts()

# + id="Ja_mW075o5qL" outputId="b9654603-84e5-4f49-a9bb-1d4e11d59fd3" colab={"base_uri": "https://localhost:8080/"}
# Look at CLASSIFICATION value counts for binning
classification_count_binning = application_df['CLASSIFICATION'].value_counts()
classification_count_binning

# + id="tvEDuHVpo5qM" outputId="e56f6f0f-f8ab-45a6-de29-e4544581abd6" colab={"base_uri": "https://localhost:8080/"}
# Look at CLASSIFICATION value counts >1.  :: used > 10
counts_classification = classification_count_binning[classification_count_binning > 10]
counts_classification.shape

# + id="Jrajfqgz4BoY" outputId="b7ed5562-bd38-492f-f513-9f2fd28e6f54" colab={"base_uri": "https://localhost:8080/"}
counts_classification

# + id="DTd7g_wxryAY" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="df2fe46c-9a8a-4b73-89b5-2172b7818a27"
# Plot look at CLASSIFICATION value counts >1 >10, > 100. 1000n +- 5000 range
counts_classification.plot(kind='density')

# + id="w8Ggy68Po5qM" outputId="56b36494-2755-44ca-e0ee-64ff0fa8e86e" colab={"base_uri": "https://localhost:8080/"}
# Choose a cutoff value and create a list of classifications to be replaced
# use the variable name `classifications_to_replace`
classifications_to_replace = list(classification_count_binning[classification_count_binning < 1000].index)
classifications_to_replace

# + colab={"base_uri": "https://localhost:8080/"} id="vpgzaA3WxnSo" outputId="810e36bd-67cf-42c5-e16a-0b2ca0560d42"
len(classifications_to_replace)

# + id="INPTLyM4o5qN" outputId="80c33c3e-4a6e-4b66-d048-b170f2bfd30a" colab={"base_uri": "https://localhost:8080/"}
# Replace in dataframe
# taking all those classifications <1000 and put in category as "Other"
for cls in classifications_to_replace:
    application_df['CLASSIFICATION'] = application_df['CLASSIFICATION'].replace(cls,"Other")
    
# Check to make sure binning was successful
application_df['CLASSIFICATION'].value_counts()

# + id="RyXkWnIJo5qN" outputId="b624472e-42a6-4539-f1c9-5f98c307651e" colab={"base_uri": "https://localhost:8080/", "height": 288}
# Convert categorical data to numeric with `pd.get_dummies`
# One hot encoding
application_df = pd.get_dummies(application_df,dtype=float)
application_df.head()

# + colab={"base_uri": "https://localhost:8080/"} id="_ue_LwHLc18V" outputId="4bd79161-0277-4e6d-be5a-8c060020d1ee"
application_df.shape

# + colab={"base_uri": "https://localhost:8080/"} id="jZ_5V022lyh5" outputId="81a7dd32-f360-4fce-ea55-a32c232736a7"
application_df.columns

# + id="n05tHaNfo5qN" outputId="a7ebaa13-4a7b-48b5-b35d-85a591f85cd0" colab={"base_uri": "https://localhost:8080/"}
# Split our preprocessed data into our features and target arrays
y = application_df['IS_SUCCESSFUL'].values
y

# + id="WGXaxl_Vo5qN" colab={"base_uri": "https://localhost:8080/"} outputId="efc54a37-6eeb-40d4-f342-8eb748b5c183"
# Drop the 'IS_SUCCESSFUL' from the features
X = application_df.drop('IS_SUCCESSFUL', axis=1).values
X

# + id="4tHp9zwoo5qO"
# Split the preprocessed data into a training and testing dataset
# train_split=0.8, val_split=0.1, test_split=0.1.  test_size=0.2,
X_train, X_test, y_train, y_test = train_test_split(X,y,random_state = 42)

# + id="ZRXGVYtDo5qO" outputId="55748ad0-298b-4386-dda6-9ac436cb4b95" colab={"base_uri": "https://localhost:8080/"}
X_train.shape, y_train.shape

# + id="X87XGVqno5qO"
# Create a StandardScaler instances
scaler = StandardScaler()

# Fit the StandardScaler
X_scaler = scaler.fit(X_train)

# Scale the data
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)

# + [markdown] id="sEBAiX9Qo5qO"
# ## Compile, Train and Evaluate the Model

# + id="v8_Gv3V2o5qO" outputId="e23d93f5-2d26-4332-e324-935414df4fca" colab={"base_uri": "https://localhost:8080/"}
# Define the model - deep neural net, i.e., the number of input features and hidden nodes for each layer.

# Increased layers to 4 hidden layers
# Use more sigmoid because binary classification, result should be 0 or 1
number_input_features = len( X_train_scaled[0])
hidden_nodes_layer1=180
hidden_nodes_layer2=50
hidden_nodes_layer3=10



nn = tf.keras.models.Sequential()

# First hidden layer
nn.add(tf.keras.layers.Dense(units=hidden_nodes_layer1, input_dim=number_input_features, activation='relu'))

# Second hidden layer. sigmoid
nn.add(tf.keras.layers.Dense(units=hidden_nodes_layer2, activation='sigmoid'))

# Third hidden layer.  sigmoid
nn.add(tf.keras.layers.Dense(units=hidden_nodes_layer3, activation='sigmoid'))

# Output layer
nn.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))

# Check the structure of the model
nn.summary()

# + colab={"base_uri": "https://localhost:8080/", "height": 533} id="EFb7ay5S3BTX" outputId="118ba5ec-5460-43ce-c350-39b7e71ee7e8"
from tensorflow.keras.utils import plot_model
# summarize the model
images_dir = '/content/drive/MyDrive/Colab Notebooks/Deep-Learning/Images'
plot_model(nn, f'{images_dir}/AlphabetSoupModelOptimized2.png', show_shapes=True)

# + id="PZh9sC7go5qO"
# Compile the model
nn.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics=['accuracy', tf.keras.metrics.Recall()])



# + id="3EgUdMMBf-Cs"
# Callback to save every 5, use this for optimizer
# Model to compile and train every 5 epochs below in train

batch_size=100
cp_callback = ModelCheckpoint(
    filepath=checkpoint_path, 
    verbose=1, 
    save_weights_only=True,
    save_freq= int(batch_size*13))


# + id="Psjz4UT0pvCi" colab={"base_uri": "https://localhost:8080/"} outputId="090f7ec6-ad26-4a63-d9e1-ec9dd50b7cfc"
# Train the model. 
fit_model = nn.fit(X_train_scaled,y_train, epochs=100, batch_size=batch_size, callbacks=[cp_callback]) 
#fit_model = nn.fit(X_train_scaled,y_train, epochs=150) 

# + id="Y5SQPcgoo5qP" outputId="94500047-8059-4160-b9f4-28879a8cf166" colab={"base_uri": "https://localhost:8080/"}
# Evaluate the model using the test data
model_loss, model_accuracy, model_Recall = nn.evaluate(X_test_scaled,y_test,verbose=2)
print(f"Loss: {model_loss}, Accuracy: {model_accuracy}")

# + [markdown] id="9xVosr8w7LyM"
# ### History and Plots 
#
#

# + colab={"base_uri": "https://localhost:8080/"} id="hbzcH4sYesy_" outputId="375ac248-d7c6-445a-dfea-04eb11b5601d"
# list all data in history
print(fit_model.history.keys())

# + id="MyAvBvAU7VJS" colab={"base_uri": "https://localhost:8080/", "height": 322} outputId="df10c471-efbf-439c-f4e5-0dec0822152f"
# Create a DataFrame containing training history
history_df = pd.DataFrame(fit_model.history, index=range(1,len(fit_model.history["loss"])+1))

# Plot the accuracy
# Visualize Loss/Accuracy
from google.colab import files
images_dir = '/content/drive/MyDrive/Colab Notebooks/Deep-Learning/Images'

fig, ax = plt.subplots()
plt.title('Opitimize Model 2: Accuracy and Loss \n',fontsize=20)
loss = ax.plot(history_df["loss"], color="red", label="Loss")
ax.set_xlabel("Epoch")
ax.set_ylabel("Loss")

ax2 = ax.twinx()
acc = ax2.plot(history_df["accuracy"], color="blue", label="Accuracy")
ax2.set_ylabel("Accuracy")

curves = loss + acc
labs = [l.get_label() for l in curves]
ax.legend(curves, labs, loc="center right")

plt.savefig(f"{images_dir}/AlphabetSoupCharity_Optimizer2_Acc_Loss.png")
files.download(f"{images_dir}/AlphabetSoupCharity_Optimizer2_Acc_Loss.png")

plt.show()


# + id="GhZWahmVo5qP" colab={"base_uri": "https://localhost:8080/", "height": 17} outputId="6695e62a-2f73-4ebc-9bb1-fc7dd8889e96"
# Export our model to HDF5 file. # (equation (with all structure )# info on model , to load model on diffe dataset, neural network structure to compile
nn.save('/content/drive/MyDrive/Colab Notebooks/Deep-Learning/Models/AlphabetSoupCharity_Optimizer2.h5')
files.download('/content/drive/MyDrive/Colab Notebooks/Deep-Learning/Models/AlphabetSoupCharity_Optimizer2.h5')



# + id="oweZf_2tHJkk"
# get the best fit model

StarcoderdataJupyterScriptsDedupFiltered