<jupyter_start><jupyter_text>Preprocessing<jupyter_code>import os
os.getcwd()
#from google.colab import drive
#drive.mount('/content/drive')
# 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 tensorflow



#  Import and read the charity_data.csv.
import pandas as pd 
application_df = pd.read_csv('../Resources/charity_data.csv')
application_df.head()
# Drop the non-beneficial ID columns, 'EIN' and 'NAME'.
application_df1 = application_df.drop(['EIN','NAME'],axis=1)
application_df1.head()
# Determine the number of unique values in each column.
application_df1.apply(pd.Series.nunique)
# Look at APPLICATION_TYPE value counts for binning
application_df1.value_counts('APPLICATION_TYPE')

# Choose a cutoff value and create a list of application types to be replaced
# use the variable name `application_types_to_replace`
#  Cutoff value will be 1% of the data set which is ~ 330 
cutoff = 330
application_types_to_replace = ['T13','T12','T2','T14','T25','T29','T15','T17']


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

# Check to make sure binning was successful
application_df1['APPLICATION_TYPE'].value_counts()
# Look at CLASSIFICATION value counts for binning
classification_df1= pd.DataFrame(application_df1.value_counts('CLASSIFICATION'))
print(classification_df1.index)
# You may find it helpful to look at CLASSIFICATION value counts >1
df = classification_df1

df[df>1]

df2 = df[df<330]

df3 = df2.dropna()

df3
# Choose a cutoff value and create a list of classifications to be replaced
# use the variable name `classifications_to_replace`
# Will use the same logic here and go with the 1% rule of the dataset being the cutoff
cutoff = 330
classifications_to_replace = []
#for cls in df:
  #if cls < cutoff:
    #classifications_to_replace.append(cls.index.values)
    #print(cls)

classifications_to_replace = df3.index.values
classifications_to_replace
# Replace in dataframe
for cls in classifications_to_replace:
    application_df1['CLASSIFICATION'] = application_df1['CLASSIFICATION'].replace(cls,"Other")
   
  
# Check to make sure binning was successful
application_df1['CLASSIFICATION'].value_counts()
# Convert categorical data to numeric with `pd.get_dummies`
application_df2 = pd.get_dummies(application_df1)
# Split our preprocessed data into our features and target arrays
y = application_df2['IS_SUCCESSFUL'].values
X = application_df2.drop(['IS_SUCCESSFUL'],1).values

# Split the preprocessed data into a training and testing dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=78)
# 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)<jupyter_output><empty_output><jupyter_text>Compile, Train and Evaluate the Model<jupyter_code># 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])
hidden_nodes_layer1 =  8
hidden_nodes_layer2 = 5

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
nn.add(tf.keras.layers.Dense(units=hidden_nodes_layer2, activation="relu"))

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

# Check the structure of the model
nn.summary()
# Compile the model
nn.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
# Train the model
fit_model = nn.fit(X_train_scaled,y_train,epochs=100)
# 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}")
# Export our model to HDF5 file
model.save("charity_predictor_model1.h5")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09650180940892641, 'fraction_numerical': 0.01761158021712907, 'mean_word_length': 6.524500907441016, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '21621', 'license': 'ADSL', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1330, 'n_words': 364, 'path': 'Instructions/Charity_Predictor_Model1_run_locally.ipynb', 'repo_name': 'gpawlows/Charity-Fundraiser-Predictor', 'size': '4145'}