<jupyter_start><jupyter_text>Load Dataset<jupyter_code>data = pd.read_csv(r"E:\ExcelR Assignment\Assignment 16 - Neural Networks\forestfires.csv")
data.head()<jupyter_output><empty_output><jupyter_text>Drop month and day as we already have one hot encoded values<jupyter_code>data = data.drop(['month','day','daysun','monthdec'],axis=1)
data.head(2)
data.dtypes<jupyter_output><empty_output><jupyter_text>Data Analysis & Data Visualization<jupyter_code>import warnings
warnings.filterwarnings("ignore")
fig, axes = plt.subplots(3, 3, figsize=(20, 15))
fig.suptitle('Univariate Analysis',fontsize=20)
sns.distplot(data['FFMC'],ax=axes[0,0],color='indigo')
sns.distplot(data['DMC'],ax=axes[0,1],color='orange')
sns.distplot(data['DC'],ax=axes[0,2],color='olive')
sns.distplot(data['ISI'],ax=axes[1,0],color='indigo')
sns.distplot(data['temp'],ax=axes[1,1],color='orange')
sns.distplot(data['RH'],ax=axes[1,2],color='olive')
sns.distplot(data['wind'],ax=axes[2,0],color='indigo')
sns.distplot(data['rain'],ax=axes[2,1],color='orange')
sns.distplot(data['area'],ax=axes[2,2],color='olive')<jupyter_output><empty_output><jupyter_text>Inferences:1. Left Skewness :  DMC | ISI | RH | rain | area2. Right Skewness : FFMC | DC  3. temp | wind  seems to have normally distribution<jupyter_code>label_encoder = LabelEncoder()
data['size_category'] = label_encoder.fit_transform(data['size_category'])
data.head()<jupyter_output><empty_output><jupyter_text>So small = 1 and large = 0 as per Label Encoding  Splitting the variables<jupyter_code>X = data.iloc[:,0:-1]
Y = data.iloc[:,-1]
data['size_category'].value_counts()<jupyter_output><empty_output><jupyter_text>Up-sampling as the Target Variable is not balanced<jupyter_code>from imblearn.combine import SMOTETomek
# Performing Oversampling method to handle imbalanced data

smk = SMOTETomek(random_state=42)
X_res,y_res = smk.fit_resample(X,Y)
(X.shape,Y.shape),(X_res.shape,y_res.shape)<jupyter_output><empty_output><jupyter_text>Create Train & Validation Set<jupyter_code>X_train,X_test,y_train,y_test = train_test_split(X_res,y_res,stratify=y_res,random_state=42,test_size=0.3)<jupyter_output><empty_output><jupyter_text>Standardizing the first few columns till area<jupyter_code>scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Define Architecture<jupyter_code>input_neurons = X_train.shape[1]
output_neurons = 1
Hidden_Layers = 2
hidden_layer_1 = 20
hidden_layer_2 = 8
model = Sequential()
model.add(InputLayer(input_shape=(input_neurons,)))
model.add(Dense(units=hidden_layer_1,activation='relu'))
model.add(Dense(units=hidden_layer_2,activation='relu'))
model.add(Dense(units=output_neurons,activation='sigmoid'))
model.summary()
model.compile(loss='binary_crossentropy',optimizer='Adam',metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>model_history = model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=100)<jupyter_output>Epoch 1/100
17/17 [==============================] - 2s 95ms/step - loss: 1.0045 - accuracy: 0.5220 - val_loss: 0.9628 - val_accuracy: 0.4911
Epoch 2/100
17/17 [==============================] - 0s 6ms/step - loss: 0.9270 - accuracy: 0.4860 - val_loss: 0.8644 - val_accuracy: 0.4554
Epoch 3/100
17/17 [==============================] - 0s 6ms/step - loss: 0.8003 - accuracy: 0.5120 - val_loss: 0.8054 - val_accuracy: 0.4821
Epoch 4/100
17/17 [==============================] - 0s 6ms/step - loss: 0.7512 - accuracy: 0.4931 - val_loss: 0.7667 - val_accuracy: 0.4732
Epoch 5/100
17/17 [==============================] - 0s 5ms/step - loss: 0.7253 - accuracy: 0.5052 - val_loss: 0.7413 - val_accuracy: 0.4955
Epoch 6/100
17/17 [==============================] - 0s 5ms/step - loss: 0.6954 - accuracy: 0.5467 - val_loss: 0.7191 - val_accuracy: 0.5312
Epoch 7/100
17/17 [==============================] - 0s 5ms/step - loss: 0.6824 - accuracy: 0.5810 - val_loss: 0.7015 - val_accuracy: 0.5670
Epoch 8/100
[...]<jupyter_text>Evaluate Model Performance  Summarizing history for loss<jupyter_code>plt.plot(model_history.history['loss'])
plt.plot(model_history.history['val_loss'])
plt.title('model_loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train','validation'],loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>Summarizing history for accuracy<jupyter_code>plt.plot(model_history.history['accuracy'])
plt.plot(model_history.history['val_accuracy'])
plt.title('model_accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train','validation'],loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>Model Performance on Validation set<jupyter_code>prediction = model.predict_classes(X_test)
accuracy_score(y_test,prediction)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19421487603305784, 'fraction_numerical': 0.05619834710743802, 'mean_word_length': 8.624254473161034, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '491986', 'license': 'CC0-1.0', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1807, 'n_words': 263, 'path': 'Assignment 16 Part 1.ipynb', 'repo_name': 'amit0902/Neural-Network', 'size': '4840'}