<jupyter_start><jupyter_text>Predicting the Weather with Neural Networks=========================================== From Australian Government's Perth  Weather Dataset  Importing Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import GridSearchCV
import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Reading Data<jupyter_code>df = pd.read_csv("weatherPerth.csv")
df.head()
df.shape<jupyter_output><empty_output><jupyter_text>Pre-Processing the Data<jupyter_code>exclude = ['Date', 'RISK_MM', 'Location']
for attr in exclude:
    del df[attr]
# Dealing with missing values.
df.isna().sum()
df = df.dropna()
# Mapping Boolean variables to 0s and 1s.

bools = ['RainToday', 'RainTomorrow']

for attr in bools:
    df[attr] = df[attr].map({
        'Yes': 1,
        'No' : 0
    })
df.head()<jupyter_output><empty_output><jupyter_text>Cyclical attributes![title](img/cardinal.png)<jupyter_code># Mapping Cardinal Direction to Radians

dirs = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW']

angles = np.arange(0.0, 2.0*np.pi, 2.0*np.pi / 16.0)

wind_angles = dict(zip(dirs, angles))

print(wind_angles)
# Replacing cyclical attributes with sin() and cos()

wind_attributes = ['WindGustDir', 'WindDir9am', 'WindDir3pm']

for attr in wind_attributes:
    df[attr] = df[attr].map(wind_angles)
    df[attr+'_cos'] = np.cos(df[attr])
    df[attr+'_sin'] = np.sin(df[attr])
    df = df.drop(columns=attr)
df.head()<jupyter_output><empty_output><jupyter_text>Splitting the Data<jupyter_code>y= df['RainTomorrow']
y.head()
X = df.drop(columns='RainTomorrow')
X.head()
# Split dataset into training and testing subsets.

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 0)
print('X_train', X_train.shape)
print('X_test', X_test.shape)
print('y_train', y_train.shape)
print('y_test', y_test.shape)
# Normalizing Data with standard scaler

scaler = StandardScaler()
scaler.fit(X_train)

X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Traning the Neural Network![title](img/ANN_2_layers.png)<jupyter_code># Instantiating a neural network and training it.

# Multi Layer Perceptron
nn = MLPClassifier(
        hidden_layer_sizes=(50, 50),
        random_state=0,
        max_iter=500
    )

nn.fit(X_train, y_train)
# Predicting target class for the test set.

y_pred = nn.predict(X_test)
print(accuracy_score(y_test, y_pred))<jupyter_output>0.8928928928928929<jupyter_text>Grid Search to find best model setup<jupyter_code># Searching for best network layout.

p= {
        'hidden_layer_sizes':(
            (2,), (10,), (10,10), (50,50)
        )
    }

nn = MLPClassifier(
    max_iter=2000,
    random_state=0
)

# parameter cv is to split data to 3 equal parts
gs = GridSearchCV(nn, p, cv=3)
gs.fit(X_train,y_train)
# Display grid search results.

print(gs.cv_results_['params'])
print(gs.cv_results_['mean_test_score'])<jupyter_output>[{'hidden_layer_sizes': (2,)}, {'hidden_layer_sizes': (10,)}, {'hidden_layer_sizes': (10, 10)}, {'hidden_layer_sizes': (50, 50)}]
[0.90424355 0.89487179 0.88055373 0.88450873]<jupyter_text>Predictions using the Best neural network.<jupyter_code>best_nn = gs.best_estimator_
y_pred= best_nn.predict(X_test)
print(accuracy_score(y_test, y_pred))<jupyter_output>0.8938938938938938
{'fraction_non_alphanumeric': 0.14706677480400107, 'fraction_numerical': 0.03433360367666937, 'mean_word_length': 6.184466019417476, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, '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': '655939', 'license': 'MIT', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1356, 'n_words': 275, 'path': 'Weather_Predictor/predicting_weather.ipynb', 'repo_name': 'LasalJayawardena/Neural-Network-Projects', 'size': '3699'}