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

# + colab={"base_uri": "https://localhost:8080/"} id="ti0ZR-f3zEvS" outputId="9eef41fa-6b6c-4e36-8dd1-f187e4a9d00e"
pip install tensorflow

# + colab={"base_uri": "https://localhost:8080/", "height": 496} id="BLhcSleWcnvg" outputId="1d1b566a-928d-4e20-d212-07f22773c29d"
# Import our dependencies
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import pandas as pd
import tensorflow as tf

#  Import and read the charity_data.csv.
import pandas as pd 
application_df = pd.read_csv("charity_data.csv")
application_df.head()

# + colab={"base_uri": "https://localhost:8080/"} id="FQ_nN-S6cnvj" outputId="c2f95a78-4ae2-41ea-d036-c670f4fda8f2"
# Drop the non-beneficial ID columns, 'EIN' and 'NAME'.
application_df = application_df.drop(["EIN", "NAME"], 1)

# + colab={"base_uri": "https://localhost:8080/", "height": 267} id="gRDa7XqV0DwC" outputId="a145b097-5132-4738-cfc0-11c3c4985487"
application_df.head()

# + colab={"base_uri": "https://localhost:8080/"} id="1Z6S2LXxcnvj" outputId="b7ff7343-2762-4df2-dff7-3e290dd4daef"
# Determine the number of unique values in each column.
application_df.nunique()

# + [markdown] id="_7NdbQ2n1kIO"
# Application_Type and Classification are the focus as they have values greater than ten.

# + colab={"base_uri": "https://localhost:8080/"} id="1kLeHIyNcnvk" outputId="22bcb06a-b924-4fc2-b2c8-4957763f09f0"
# Look at APPLICATION_TYPE value counts for binning
application_counts = application_df["APPLICATION_TYPE"].value_counts()
application_counts

# + [markdown] id="PiR7Tfwo2aL8"
# Suggest cutting of at 500, less will be binned as other

# + colab={"base_uri": "https://localhost:8080/"} id="5oKYg6PFcnvl" outputId="1b5dfd6f-65d8-4566-c6e0-34187763eb3b"
# 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_counts[application_counts <500].index)

# 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 successful
application_df['APPLICATION_TYPE'].value_counts()

# + colab={"base_uri": "https://localhost:8080/"} id="6_nmfMOzcnvl" outputId="5f3a14b6-7bc2-476a-8df8-d997ebe9c309"
# Look at CLASSIFICATION value counts for binning
class_counts = application_df["CLASSIFICATION"].value_counts()
class_counts

# + [markdown] id="lUakpk-R3TTM"
# Cutting off where the classification_counts is >1.

# + colab={"base_uri": "https://localhost:8080/"} id="6w3nEq13cnvm" outputId="8e7d4f6c-b002-45c0-8bfa-25e9bbee7a5c"
# You may find it helpful to look at CLASSIFICATION value counts >1
class_counts[class_counts>1]

# + [markdown] id="BpEuYIWH3rtK"
# Update the cutoff to > 1000 for classification_counts

# + colab={"base_uri": "https://localhost:8080/"} id="JQJTAvDCcnvm" outputId="1286371a-7568-47da-c4c9-7c1dda40cafd"
# Choose a cutoff value and create a list of classifications to be replaced
# use the variable name `classifications_to_replace`
classifications_to_replace = list(class_counts[class_counts < 1000].index)

# Replace in dataframe
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()

# + colab={"base_uri": "https://localhost:8080/", "height": 287} id="Fm-Rv3JPcnvn" outputId="c418173b-72ec-45a5-d80c-2b91b0886010"
# Convert categorical data to numeric with `pd.get_dummies`
appication_with_dummies_df = pd.get_dummies(application_df)
appication_with_dummies_df.head(5)

# + id="rJdroEvFcnvo"
# Split our preprocessed data into our features and target arrays
X = appication_with_dummies_df.drop(["IS_SUCCESSFUL"],axis="columns").values
y = appication_with_dummies_df["IS_SUCCESSFUL"].values
# Split the preprocessed data into a training and testing dataset
X_train, X_test, y_train, y_test = train_test_split(X,y)

# + id="4g0in-Qscnvo"
# 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="fabODgRscnvp"
# ## Compile, Train and Evaluate the Model

# + colab={"base_uri": "https://localhost:8080/"} id="GQ7yS0jMcnvp" outputId="bbc55720-92b8-4ba9-b3cd-71a94037b22b"
# Define the model - deep neural net, i.e., the number of input features and hidden nodes for each layer.
number_input_features = len(X_train[0])
layer1Nodes = 80
layer2Nodes = 30


nn = tf.keras.models.Sequential()

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

# Second hidden layer
nn.add(
    tf.keras.layers.Dense(units=layer2Nodes, input_dim=layer1Nodes, activation='relu')
)

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

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

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

# + colab={"base_uri": "https://localhost:8080/"} id="bxKt1rAGcnvq" outputId="4b791f02-5348-4e98-f003-b91feaa1dca5"
# Train the model
nn.compile(loss="binary_crossentropy", optimizer='adam', metrics=['accuracy'])

# + colab={"base_uri": "https://localhost:8080/"} id="8yc1aQ9xcnvq" outputId="e6c9c80d-6856-489b-ad81-5355a27ec3b6"
# 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="IOY1j8wgcnvr"
# Export our model to HDF5 file
nn.save("AlphabetSoupCharity.h5")

StarcoderdataJupyterScriptsDedupFiltered