# ---
# 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
# ---

# ## Model 6
# Original model, but changes the standarization for normalization using MinMaxScaler, and number of epochs to 80.

# Imports
import pandas as pd
import numpy as np
from pathlib import Path
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler,OneHotEncoder
from datetime import date

# ---
#
# ## Prepare the data to be used on a neural network model

# ### Step 1: Read the `applicants_data.csv` file into a Pandas DataFrame. Review the DataFrame, looking for categorical variables that will need to be encoded, as well as columns that could eventually define your features and target variables.  
#

# +
# Read the applicants_data.csv file from the Resources folder into a Pandas DataFrame
applicant_data_df = pd.read_csv(
    Path('../Resources/applicants_data.csv',)
)

# Review the DataFrame
applicant_data_df.head()

# -

# Review the data types associated with the columns
applicant_data_df.dtypes




# ### Step 2: Drop the “EIN” (Employer Identification Number) and “NAME” columns from the DataFrame, because they are not relevant to the binary classification model.

# +
# Drop the 'EIN' and 'NAME' columns from the DataFrame
applicant_data_df = applicant_data_df.drop(columns=['EIN','NAME'])

# Review the DataFrame
applicant_data_df.head()

# -

# ### Step 3: Encode the dataset’s categorical variables using `OneHotEncoder`, and then place the encoded variables into a new DataFrame.

# +
# Create a list of categorical variables 
categorical_variables = list(applicant_data_df.dtypes[applicant_data_df.dtypes=='object'].index)

# Display the categorical variables list
categorical_variables

# -

# Create a OneHotEncoder instance
enc = OneHotEncoder(sparse=False)


# Encode the categorcal variables using OneHotEncoder
encoded_data = enc.fit_transform(applicant_data_df[categorical_variables])


# +
# Create a DataFrame with the encoded variables
encoded_df = pd.DataFrame(
    encoded_data,
    columns=enc.get_feature_names(categorical_variables)
)

# Review the DataFrame
encoded_df.head()

# -

# ### Step 4: Add the original DataFrame’s numerical variables to the DataFrame containing the encoded variables.
#
# > **Note** To complete this step, you will employ the Pandas `concat()` function that was introduced earlier in this course. 

non_categorical_variables = list(applicant_data_df.dtypes[applicant_data_df.dtypes!='object'].index)
non_categorical_variables

# +
# Add the numerical variables from the original DataFrame to the one-hot encoding DataFrame
encoded_df =pd.concat([applicant_data_df[non_categorical_variables], encoded_df], axis=1)

# Review the Dataframe
encoded_df.head()

# -

# ### Step 5: Using the preprocessed data, create the features (`X`) and target (`y`) datasets. The target dataset should be defined by the preprocessed DataFrame column “IS_SUCCESSFUL”. The remaining columns should define the features dataset. 
#
#

# +
# Define the target set y using the IS_SUCCESSFUL column
y = encoded_df['IS_SUCCESSFUL']

# Display a sample of y
y.head()


# +
# Define features set X by selecting all columns but IS_SUCCESSFUL
X = encoded_df.drop(columns=['IS_SUCCESSFUL'])

# Review the features DataFrame
X.head()

# -

# ### Step 6: Split the features and target sets into training and testing datasets.
#

# Split the preprocessed data into a training and testing dataset
# Assign the function a random_state equal to 1
X_train, X_test, y_train, y_test = train_test_split(X,y)


# ### Step 7: Use scikit-learn's `StandardScaler` to scale the features data.

# +
# Create a StandardScaler instance
scaler = MinMaxScaler()

# Fit the scaler to the features training dataset
X_scaler = scaler.fit(X_train)

# Fit the scaler to the features training dataset
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)

# -

# ---
#
# ## Compile and Evaluate a Binary Classification Model Using a Neural Network

# ### Step 1: Create a deep neural network by assigning the number of input features, the number of layers, and the number of neurons on each layer using Tensorflow’s Keras.
#
# > **Hint** You can start with a two-layer deep neural network model that uses the `relu` activation function for both layers.
#

# +
# Define the the number of inputs (features) to the model
# This is the numbers of columns in the X array
number_input_features = X.shape[1]

# Review the number of features
number_input_features

# -

# Define the number of neurons in the output layer
number_output_neurons = 1

# +
# Define the number of hidden nodes for the first hidden layer
hidden_nodes_layer1 = np.ceil(np.sqrt(number_input_features * number_output_neurons))

# Review the number hidden nodes in the first layer
hidden_nodes_layer1


# +
# Define the number of hidden nodes for the second hidden layer
hidden_nodes_layer2 =  np.ceil(np.sqrt(hidden_nodes_layer1 * number_output_neurons))

# Review the number hidden nodes in the second layer
hidden_nodes_layer2

# -

# Create the Sequential model instance
nn = Sequential()


# Add the first hidden layer
nn.add(
    Dense(
        units=hidden_nodes_layer1,
        activation='relu',
        input_dim=number_input_features
    )
)


# Add the second hidden layer
nn.add(
    Dense(
        units=hidden_nodes_layer2,
        activation='relu'
    )
)


# Add the output layer to the model specifying the number of output neurons and activation function
nn.add(
    Dense(
        units=1,
        activation='sigmoid'
    )
)


# Display the Sequential model summary
nn.summary()


# ### Step 2: Compile and fit the model using the `binary_crossentropy` loss function, the `adam` optimizer, and the `accuracy` evaluation metric.
#

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


# Fit the model using 50 epochs and the training data
# nn_model will take the history
epochs=80
nn_model=nn.fit(X_train_scaled, y_train, epochs=epochs)


# ### Step 3: Evaluate the model using the test data to determine the model’s loss and accuracy.
#

# +
# Evaluate the model loss and accuracy metrics using the evaluate method and the test data
model_loss, model_accuracy = nn.evaluate(
                                        X_test_scaled,
                                        y_test,
                                        verbose=2
)

# Display the model loss and accuracy results
print(f"Loss: {model_loss}, Accuracy: {model_accuracy}")
# -

# ### Step 4: Save and export your model to an HDF5 file, and name the file `AlphabetSoup.h5`. 
#

# +
# Set the model's file path
file_path = Path('../Resources/AlphabetSoup_Model_6.h5')

# Export your model to a HDF5 file
nn.save(file_path)

# -

# ---
#
# ## Optimize the neural network models
# In separate files there are 3 alternative neural networks models for the classification
#

# ### Step 2: After finishing your models, display the accuracy scores achieved by each model, and compare the results.

# +
# Display the model loss and accuracy results
results_df=pd.DataFrame(
                    data={
                        'Date': [date.today()],
                        'Model'   : [6],
                        'Observations': ['Original model, MinMaxScaler'],
                        'Loss'    : [model_loss], 
                        'Accuracy': [model_accuracy],
                        'Hidden layers': 2,
                        'Input Features':[number_input_features],
                        'Output Neurons': [number_output_neurons],
                        'Neurons in Hidden Layers1' : [hidden_nodes_layer1],
                        'Neurons in Hidden Layers2' : [hidden_nodes_layer2],
                        'Epochs'         : [epochs],
                        'Activation Hidden layer 1': ['relu'],
                        'Activation Hidden layer 2': ['relu'],
                        'Activation Output layer 1': ['sigmoid'],
                        'loss':['binary_crossentropy'],
                        'optimizer':['adam'],
                        'metrics':['accuracy']          
                    })

print(results_df.T)
# -

results_df.to_csv('../Resources/results.csv', mode='a') 





StarcoderdataJupyterScriptsDedupFiltered