# ---
# 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="zhRlBVwdE4PD"
# ## Preprocessing

# + colab={"base_uri": "https://localhost:8080/"} id="CcvJBLfpE4PJ" outputId="6d0e4c0b-65c9-40de-be96-1991a2b63c07"
# Import our dependencies
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import pandas as pd
import tensorflow as tf

#Mounting Drive
from google.colab import drive
drive.mount('/content/drive')



# + colab={"base_uri": "https://localhost:8080/", "height": 424} id="-nG8VLs7I8kp" outputId="36373bcf-d506-47c8-8965-827f2147d363"
#  Import and read the charity_data.csv.
import pandas as pd 
application_df = pd.read_csv("./drive/My Drive/Colab Notebooks/alphabet_soup/Resources/charity_data.csv")
application_df

# + id="AQ2i0n3mE4PM"
# Drop the non-beneficial ID columns, 'EIN' and 'NAME'.
#  YOUR CODE GOES HERE
app_df = application_df.drop(["EIN","NAME"],1)

# + id="tGZ9ilhkE4PN" colab={"base_uri": "https://localhost:8080/"} outputId="9a81a3d9-51ff-468f-9517-51c4c18c1dff"
# Determine the number of unique values in each column.
#  YOUR CODE GOES HERE
app_df.nunique()

# + id="ljcrwx5SE4PN" colab={"base_uri": "https://localhost:8080/"} outputId="865a5e8e-e073-468d-e772-0c692dd09925"
# Look at APPLICATION_TYPE value counts for binning
#  YOUR CODE GOES HERE
app_ct = app_df["APPLICATION_TYPE"].value_counts()
app_ct

# + colab={"base_uri": "https://localhost:8080/"} id="VL4MK9Kulnuj" outputId="0327acf7-f415-4881-c101-63d749b8bb51"
list(app_ct[app_ct < 500].index)

# + id="_eiwNZXLE4PO" colab={"base_uri": "https://localhost:8080/"} outputId="49df9cc8-5167-44ea-ea60-9a4d4a37eb87"
# Choose a cutoff value and create a list of application types to be replaced
# use the variable name `application_types_to_replace`
#  YOUR CODE GOES HERE
application_types_to_replace = list(app_ct[app_ct < 500].index)

# Replace in dataframe
for app in application_types_to_replace:
    app_df['APPLICATION_TYPE'] = app_df['APPLICATION_TYPE'].replace(app,"Other")

# Check to make sure binning was successful
app_df['APPLICATION_TYPE'].value_counts()

# + id="PqgSd9oYE4PO" colab={"base_uri": "https://localhost:8080/"} outputId="cecdffd9-ee43-4513-e762-39aa691ff134"
# Look at CLASSIFICATION value counts for binning
#  YOUR CODE GOES HERE
class_ct = app_df["CLASSIFICATION"].value_counts()
class_ct

# + id="gnrzJJaiE4PP" colab={"base_uri": "https://localhost:8080/"} outputId="05923403-d44f-45c9-cfa8-559626323207"
# You may find it helpful to look at CLASSIFICATION value counts >1
#  YOUR CODE GOES HERE
class_ct[class_ct > 1]

# + id="YE937bzlE4PQ" colab={"base_uri": "https://localhost:8080/"} outputId="b2340ab3-036c-42fd-ed5a-ab59abcb47f8"
# Choose a cutoff value and create a list of classifications to be replaced
# use the variable name `classifications_to_replace`
#  YOUR CODE GOES HERE
classifications_to_replace = list(class_ct[class_ct < 1000].index)

# Replace in dataframe
for cls in classifications_to_replace:
    app_df['CLASSIFICATION'] = app_df['CLASSIFICATION'].replace(cls,"Other")
    
# Check to make sure binning was successful
app_df['CLASSIFICATION'].value_counts()

# + id="GxlpqKJAE4PQ" colab={"base_uri": "https://localhost:8080/", "height": 461} outputId="63768ce6-f361-4197-888e-9d05a13c342f"
# Convert categorical data to numeric with `pd.get_dummies`
#  YOUR CODE GOES HERE
app_dummies_df = pd.get_dummies(app_df)
app_dummies_df

# + id="tQz0-KRjE4PR"
# Split our preprocessed data into our features and target arrays
#  YOUR CODE GOES HERE
X = app_dummies_df.drop(["IS_SUCCESSFUL"], axis="columns").values
y = app_dummies_df["IS_SUCCESSFUL"].values

# Split the preprocessed data into a training and testing dataset
#  YOUR CODE GOES HERE
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=50)

# + id="5I4J5jO1E4PR"
# 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="oU_hiPn-E4PR"
# ## Compile, Train and Evaluate the Model

# + id="YGBBbWigE4PS" colab={"base_uri": "https://localhost:8080/"} outputId="7922a555-a74a-4680-8339-06cd839a3dc5"
# Define the model - deep neural net, i.e., the number of input features and hidden nodes for each layer.
#  YOUR CODE GOES HERE
number_input_features = len(X_train[0])
hidden_nodes_layer1 = 150
hidden_nodes_layer2 = 100
# hidden_nodes_layer3 = 80
# hidden_nodes_layer4 = 30


nn = tf.keras.models.Sequential()

# First hidden layer
#  YOUR CODE GOES HERE
nn.add(
    tf.keras.layers.Dense(units=hidden_nodes_layer1, input_dim=number_input_features, activation="relu")
)

# Second hidden layer
#  YOUR CODE GOES HERE
nn.add(
    tf.keras.layers.Dense(units=hidden_nodes_layer2, activation="relu")
)

# Third hidden layer
#  YOUR CODE GOES HERE
# nn.add(
#     tf.keras.layers.Dense(units=hidden_nodes_layer3, activation="relu")
# )

# # fourth hidden layer
# #  YOUR CODE GOES HERE
# nn.add(
#     tf.keras.layers.Dense(units=hidden_nodes_layer4, activation="relu")
# )

# Output layer
#  YOUR CODE GOES HERE
nn.add(
    tf.keras.layers.Dense(units=1, activation="sigmoid")
)

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

# + id="dwxmVlxHE4PS"
# Compile the model
#  YOUR CODE GOES HERE
nn.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])

# + id="FZCoxUYgE4PS" colab={"base_uri": "https://localhost:8080/"} outputId="c4dbede0-836f-4522-b072-c24caa2431e7"
# Train the model
#  YOUR CODE GOES HERE
fit_model = nn.fit(X_train_scaled, y_train, epochs=100)

# + id="gDn-iTGYE4PT" colab={"base_uri": "https://localhost:8080/"} outputId="b9992165-c5d5-427a-9a24-d55a9fb6b34e"
# Evaluate the model using the test data
model_loss, model_accuracy = nn.evaluate(X_test_scaled,y_test,verbose=2)
print(f"Loss: {model_loss}, Accuracy: {model_accuracy}")

# + id="SlhNwsdRE4PT"
# Export our model to HDF5 file
#  YOUR CODE GOES HERE
nn.save("./drive/My Drive/Colab Notebooks/alphabet_soup/AlphabetSoupCharity_NOTOptimized.h5")

# + id="uuqjmtCP--2G"


StarcoderdataJupyterScriptsDedupFiltered