<jupyter_start><jupyter_text>Problem Statement: PREDICT THE BURNED AREA OF FOREST FIRES WITH NEURAL NETWORKS<jupyter_code>import pandas as pd
import numpy as np 
from keras.layers import Dense 
from keras.models import Sequential
forest = pd.read_csv("G:/data sceince/Assignments/Neural Networks/forestfires.csv")
forest.head()
forest.shape
forest.info()
from sklearn.preprocessing import LabelEncoder
# convering the size_category column into Labels by using label encoder

label_encoder = LabelEncoder()
forest['size_category'] = label_encoder.fit_transform(forest.size_category)
forest = forest.drop(['month','day'], axis = 1)
forest.head()
forest.describe()
forest['size_category'].value_counts()
forest.dtypes
x = forest.iloc[:,0:28]
y = forest.iloc[:,28]
x
y
from sklearn.preprocessing import StandardScaler
std = StandardScaler()
x_std = std.fit_transform(x)<jupyter_output><empty_output><jupyter_text>Hyper tuning of parameters<jupyter_code>from sklearn.model_selection import GridSearchCV,KFold
from keras.wrappers.scikit_learn import KerasRegressor      # As the task is Regression
from tensorflow.keras.optimizers import Adam<jupyter_output><empty_output><jupyter_text>Creating a framework<jupyter_code>def create_model():
    model = Sequential()
    model.add(Dense(15,input_dim = 28,kernel_initializer = 'uniform', activation = 'relu'))
    model.add(Dense(10,kernel_initializer = 'uniform', activation = 'relu'))
    model.add(Dense(1,kernel_initializer = 'uniform', activation = 'sigmoid'))
    
    adam = Adam(lr = 0.01)
    model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    return model<jupyter_output><empty_output><jupyter_text>Creating the model  Tuning for batch size and epochs<jupyter_code>model = KerasRegressor(build_fn = create_model, verbose = 0)

batch_size = [20,30,40,50]
epochs = [10,40,60,70]

param_grid = dict(batch_size = batch_size, epochs = epochs)

grid = GridSearchCV(estimator = model, param_grid = param_grid, cv = KFold(), verbose = 10)
grid.fit(x_std,y)
print('Best: {} with: {}'.format(grid.best_score_ , grid.best_params_))

means = grid.cv_results_['mean_test_score']
std = grid.cv_results_['std_test_score']
params = grid.cv_results_['params']

for mean,stdev,param in zip(means,std,params):
       print('{} {} with : {}'.format(mean,stdev,param))<jupyter_output>Best: -0.3702536430209875 with: {'batch_size': 30, 'epochs': 70}
-0.5934700012207031 0.13394593728558982 with : {'batch_size': 20, 'epochs': 10}
-0.38718259185552595 0.17271277304817084 with : {'batch_size': 20, 'epochs': 40}
-0.5198528416454792 0.35094010770415995 with : {'batch_size': 20, 'epochs': 60}
-0.5328638419508934 0.5455766563305958 with : {'batch_size': 20, 'epochs': 70}
-0.5995655119419098 0.1228944673262816 with : {'batch_size': 30, 'epochs': 10}
-0.47591584324836733 0.15352073129483548 with : {'batch_size': 30, 'epochs': 40}
-0.3815214619040489 0.16474678542588708 with : {'batch_size': 30, 'epochs': 60}
-0.3702536430209875 0.2140591835975942 with : {'batch_size': 30, 'epochs': 70}
-0.6100983262062073 0.07049230230017384 with : {'batch_size': 40, 'epochs': 10}
-0.523179367184639 0.15865174244740232 with : {'batch_size': 40, 'epochs': 40}
-0.44226178526878357 0.15842839190078212 with : {'batch_size': 40, 'epochs': 60}
-0.4800052806735039 0.30118932475241533 with : {'batch_s[...]<jupyter_text>Tuning for dropout rate and learning rate<jupyter_code>from keras.layers import Dropout

def create_model(dropout_rate,learning_rate):
    model = Sequential()
    model.add(Dense(15,input_dim = 28,kernel_initializer = 'uniform', activation = 'relu'))
    model.add(Dropout(dropout_rate))
    model.add(Dense(10,kernel_initializer = 'uniform', activation = 'relu'))
    model.add(Dropout(dropout_rate))
    model.add(Dense(1,kernel_initializer = 'uniform', activation = 'sigmoid'))
    
    adam = Adam(lr = 0.01)
    model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    return model

model = KerasRegressor(build_fn = create_model,batch_size = 30, epochs = 70, verbose = 0)

dropout_rate = [0.1,0.2,0.5,0.7]
learning_rate = [0.1,0.01,0.001,0.0001]

param_grid = dict(dropout_rate = dropout_rate, learning_rate = learning_rate)

grid = GridSearchCV(estimator = model, param_grid = param_grid, cv = KFold(), verbose = 10)
grid.fit(x_std,y)
print('Best: {} with: {}'.format(grid.best_score_ , grid.best_params_))

means = grid.cv_results_['mean_test_score']
std = grid.cv_results_['std_test_score']
params = grid.cv_results_['params']

for mean,stdev,param in zip(means,std,params):
       print('{} {} with : {}'.format(mean,stdev,param))<jupyter_output>Best: -0.42305895686149597 with: {'dropout_rate': 0.5, 'learning_rate': 0.0001}
-0.5550265789031983 0.39329850418336193 with : {'dropout_rate': 0.1, 'learning_rate': 0.1}
-0.4568166047334671 0.2767589778063135 with : {'dropout_rate': 0.1, 'learning_rate': 0.01}
-0.43387077152729037 0.2995992225102095 with : {'dropout_rate': 0.1, 'learning_rate': 0.001}
-0.4379359155893326 0.24536487261222614 with : {'dropout_rate': 0.1, 'learning_rate': 0.0001}
-0.47393959015607834 0.2518050030263569 with : {'dropout_rate': 0.2, 'learning_rate': 0.1}
-0.4407588243484497 0.23829701201611922 with : {'dropout_rate': 0.2, 'learning_rate': 0.01}
-0.5625144951045513 0.3814054072040196 with : {'dropout_rate': 0.2, 'learning_rate': 0.001}
-0.503511980175972 0.3607765703514909 with : {'dropout_rate': 0.2, 'learning_rate': 0.0001}
-0.45873473286628724 0.18172963027220593 with : {'dropout_rate': 0.5, 'learning_rate': 0.1}
-0.49321253299713136 0.22507776599776724 with : {'dropout_rate': 0.5, 'learning_rate': 0.01}[...]<jupyter_text>Tuning for Activation Function and kernel initializer<jupyter_code>def create_model(activation_function,init):
    model = Sequential()
    model.add(Dense(15,input_dim = 28,kernel_initializer = init, activation = activation_function))
    model.add(Dropout(0.5))
    model.add(Dense(10,kernel_initializer = init, activation = activation_function))
    model.add(Dropout(0.5))
    model.add(Dense(1,kernel_initializer = 'uniform', activation = 'sigmoid'))
    
    adam = Adam(lr = 0.0001)
    model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    return model

model = KerasRegressor(build_fn = create_model,batch_size = 30, epochs = 70, verbose = 0)

activation_function = ['relu','maxout','elu','tanh','sigmoid']
init = ['uniform','normal','zero']

param_grid = dict(activation_function = activation_function, init = init)

grid = GridSearchCV(estimator = model, param_grid = param_grid, cv = KFold(), verbose = 10)
grid.fit(x_std,y)
print('Best: {} with: {}'.format(grid.best_score_ , grid.best_params_))

means = grid.cv_results_['mean_test_score']
std = grid.cv_results_['std_test_score']
params = grid.cv_results_['params']

for mean,stdev,param in zip(means,std,params):
       print('{} {} with : {}'.format(mean,stdev,param))<jupyter_output>Best: -0.3033557366579771 with: {'activation_function': 'tanh', 'init': 'uniform'}
-0.5180383905768394 0.24752721404297118 with : {'activation_function': 'relu', 'init': 'uniform'}
-0.5245446711778641 0.22128725568817859 with : {'activation_function': 'relu', 'init': 'normal'}
-0.6048707783222198 0.09106864744384366 with : {'activation_function': 'relu', 'init': 'zero'}
nan nan with : {'activation_function': 'maxout', 'init': 'uniform'}
nan nan with : {'activation_function': 'maxout', 'init': 'normal'}
nan nan with : {'activation_function': 'maxout', 'init': 'zero'}
-0.43926092237234116 0.3077857913427234 with : {'activation_function': 'elu', 'init': 'uniform'}
-0.4056859605014324 0.2490104610480714 with : {'activation_function': 'elu', 'init': 'normal'}
-0.6151598989963531 0.13361344064942401 with : {'activation_function': 'elu', 'init': 'zero'}
-0.3033557366579771 0.17921355375712386 with : {'activation_function': 'tanh', 'init': 'uniform'}
-0.3139221385121346 0.16030685551282872 wit[...]<jupyter_text>Tuning for number of neurons<jupyter_code>def create_model(neuron1,neuron2):
    model = Sequential()
    model.add(Dense(neuron1,input_dim = 28,kernel_initializer = 'uniform', activation = 'tanh'))
    model.add(Dropout(0.5))
    model.add(Dense(neuron2,kernel_initializer = 'uniform', activation = 'tanh'))
    model.add(Dropout(0.5))
    model.add(Dense(1,kernel_initializer = 'uniform', activation = 'sigmoid'))
    
    adam = Adam(lr = 0.0001)
    model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    return model

model = KerasRegressor(build_fn = create_model,batch_size = 30, epochs = 70, verbose = 0)

neuron1 = [10,12,15,20]
neuron2 = [5,8,10,14]

param_grid = dict(neuron1 = neuron1, neuron2 = neuron2)

grid = GridSearchCV(estimator = model, param_grid = param_grid, cv = KFold(), verbose = 10)
grid.fit(x_std,y)
print('Best: {} with: {}'.format(grid.best_score_ , grid.best_params_))

means = grid.cv_results_['mean_test_score']
std = grid.cv_results_['std_test_score']
params = grid.cv_results_['params']

for mean,stdev,param in zip(means,std,params):
       print('{} {} with : {}'.format(mean,stdev,param))<jupyter_output>Best: -0.28386979810893537 with: {'neuron1': 20, 'neuron2': 14}
-0.3438359022140503 0.13142178212950711 with : {'neuron1': 10, 'neuron2': 5}
-0.3342728026211262 0.15612313833701572 with : {'neuron1': 10, 'neuron2': 8}
-0.3252052828669548 0.16587506263959298 with : {'neuron1': 10, 'neuron2': 10}
-0.3178505964577198 0.19713036499877895 with : {'neuron1': 10, 'neuron2': 14}
-0.325211001932621 0.12817338671722364 with : {'neuron1': 12, 'neuron2': 5}
-0.3270144030451775 0.16100947885758365 with : {'neuron1': 12, 'neuron2': 8}
-0.3190512739121914 0.16850956897565827 with : {'neuron1': 12, 'neuron2': 10}
-0.31296044029295444 0.20411704225153862 with : {'neuron1': 12, 'neuron2': 14}
-0.2948486849665642 0.11954999697429546 with : {'neuron1': 15, 'neuron2': 5}
-0.31331661641597747 0.16141595224957336 with : {'neuron1': 15, 'neuron2': 8}
-0.2942019313573837 0.17038631292886242 with : {'neuron1': 15, 'neuron2': 10}
-0.2868425041437149 0.19814796582528824 with : {'neuron1': 15, 'neuron2': 14}
-0.29[...]<jupyter_text>Building the final model with optimum values<jupyter_code>from sklearn.model_selection import cross_val_score
import warnings
warnings.filterwarnings("ignore")
def create_model():
    model = Sequential()
    model.add(Dense(20,input_dim = 28,kernel_initializer = 'uniform', activation = 'tanh'))
    model.add(Dropout(0.5))
    model.add(Dense(14,kernel_initializer = 'uniform', activation = 'tanh'))
    model.add(Dropout(0.5))
    model.add(Dense(1,kernel_initializer = 'uniform', activation = 'sigmoid'))
    
    adam = Adam(lr = 0.0001)
    model.compile(loss = 'mean_squared_error', optimizer = 'adam', metrics = ['accuracy'])
    return model

model = KerasRegressor(build_fn = create_model,batch_size = 30, epochs = 70, verbose = 10)

model.fit(x_std,y)

kfold = KFold(n_splits=30)
results = cross_val_score(model, x_std, y, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (results.mean(), results.std()))<jupyter_output>Epoch 1/70
Epoch 2/70
Epoch 3/70
Epoch 4/70
Epoch 5/70
Epoch 6/70
Epoch 7/70
Epoch 8/70
Epoch 9/70
Epoch 10/70
Epoch 11/70
Epoch 12/70
Epoch 13/70
Epoch 14/70
Epoch 15/70
Epoch 16/70
Epoch 17/70
Epoch 18/70
Epoch 19/70
Epoch 20/70
Epoch 21/70
Epoch 22/70
Epoch 23/70
Epoch 24/70
Epoch 25/70
Epoch 26/70
Epoch 27/70
Epoch 28/70
Epoch 29/70
Epoch 30/70
Epoch 31/70
Epoch 32/70
Epoch 33/70
Epoch 34/70
Epoch 35/70
Epoch 36/70
Epoch 37/70
Epoch 38/70
Epoch 39/70
Epoch 40/70
Epoch 41/70
Epoch 42/70
Epoch 43/70
Epoch 44/70
Epoch 45/70
Epoch 46/70
Epoch 47/70
Epoch 48/70
Epoch 49/70
Epoch 50/70
Epoch 51/70
Epoch 52/70
Epoch 53/70
Epoch 54/70
Epoch 55/70
Epoch 56/70
Epoch 57/70
Epoch 58/70
Epoch 59/70
Epoch 60/70
Epoch 61/70
Epoch 62/70
Epoch 63/70
Epoch 64/70
Epoch 65/70
Epoch 66/70
Epoch 67/70
Epoch 68/70
Epoch 69/70
Epoch 70/70
Epoch 1/70
Epoch 2/70
Epoch 3/70
Epoch 4/70
Epoch 5/70
Epoch 6/70
Epoch 7/70
Epoch 8/70
Epoch 9/70
Epoch 10/70
Epoch 11/70
Epoch 12/70
Epoch 13/70
Epoch 14/70
Epoch 15/7[...]
{'fraction_non_alphanumeric': 0.1479831724820589, 'fraction_numerical': 0.1820506475294894, 'mean_word_length': 5.650575973669775, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '555507', 'license': 'MIT', 'n_tokens_mistral': 6126, 'n_tokens_neox': 4713, 'n_words': 975, 'path': 'Assign_ANN_forest_fires.ipynb', 'repo_name': 'anagha0397/ANN', 'size': '12123'}