<jupyter_start><jupyter_text>Configurations and Imports<jupyter_code>%matplotlib inline
%load_ext autoreload
%autoreload 2
%config InlineBackend.figure_format = 'retina'

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>path = 'Bike-Sharing-Dataset/hour.csv'

df = pd.read_csv(path)<jupyter_output><empty_output><jupyter_text>Inspecting data  What's the overall structure of the dataset?<jupyter_code>df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 17379 entries, 0 to 17378
Data columns (total 17 columns):
instant       17379 non-null int64
dteday        17379 non-null object
season        17379 non-null int64
yr            17379 non-null int64
mnth          17379 non-null int64
hr            17379 non-null int64
holiday       17379 non-null int64
weekday       17379 non-null int64
workingday    17379 non-null int64
weathersit    17379 non-null int64
temp          17379 non-null float64
atemp         17379 non-null float64
hum           17379 non-null float64
windspeed     17379 non-null float64
casual        17379 non-null int64
registered    17379 non-null int64
cnt           17379 non-null int64
dtypes: float64(4), int64(12), object(1)
memory usage: 2.3+ MB<jupyter_text>How do the columns look statistically?<jupyter_code>df.describe()<jupyter_output><empty_output><jupyter_text>How does the data look like?<jupyter_code>df.head(24)
df[:24*10].plot(x='dteday', y='cnt')<jupyter_output><empty_output><jupyter_text>Preprocessing data  Transform categorical features into binary representations<jupyter_code>categorical_features = ['season', 'weathersit', 'mnth', 'hr', 'weekday']
for feature in categorical_features:
    binary_features = pd.get_dummies(df[feature], prefix=feature, drop_first=False)
    df = pd.concat([df, binary_features], axis=1)

fields_to_drop = ['instant', 'dteday', 'season', 'weathersit', 
                  'weekday', 'atemp', 'mnth', 'workingday', 'hr']
df = df.drop(fields_to_drop, axis=1)
df.head()<jupyter_output><empty_output><jupyter_text>Scaling continuous variables to zero mean and standard deviation = 1<jupyter_code>quantitative_features = ['casual', 'registered', 'cnt', 'temp', 'hum', 'windspeed']
# Store scalings in a dictionary so we can convert back later
scaled_features = {}
for feature in quantitative_features:
    mean, std = df[feature].mean(), df[feature].std()
    scaled_features[feature] = [mean, std]
    df.loc[:, feature] = (df[feature] - mean)/std<jupyter_output><empty_output><jupyter_text>Splitting data into training, testing and validation sets<jupyter_code># Save data for approximately the last 21 days 
test_data = df[-21*24:]

# Now remove the test data from the data set 
df = df[:-21*24]

# Separate the data into features and targets
target_fields = ['cnt', 'casual', 'registered']
features, targets = df.drop(target_fields, axis=1), df[target_fields]
test_features, test_targets = test_data.drop(target_fields, axis=1), test_data[target_fields]
# Hold out the last 60 days or so of the remaining data as a validation set
train_features, train_targets = features[:-60*24], targets[:-60*24]
val_features, val_targets = features[-60*24:], targets[-60*24:]<jupyter_output><empty_output><jupyter_text>Build the neural network<jupyter_code>from neural_network import NeuralNetwork
def MSE(y, Y):
    return np.mean((y-Y)**2)
import unittest

inputs = np.array([[0.5, -0.2, 0.1]])
targets = np.array([[0.4]])
test_w_i_h = np.array([[0.1, -0.2],
                       [0.4, 0.5],
                       [-0.3, 0.2]])
test_w_h_o = np.array([[0.3],
                       [-0.1]])

class TestMethods(unittest.TestCase):
    
    ##########
    # Unit tests for data loading
    ##########
    
    def test_data_path(self):
        # Test that file path to dataset has been unaltered
        self.assertTrue(path.lower() == 'bike-sharing-dataset/hour.csv')
        
    def test_data_loaded(self):
        # Test that data frame loaded
        self.assertTrue(isinstance(df, pd.DataFrame))
    
    ##########
    # Unit tests for network functionality
    ##########

    def test_activation(self):
        network = NeuralNetwork(3, 2, 1, 0.5)
        # Test that the activation function is a sigmoid
        self.assertTrue(np.all(network.activation_function(0.5) == 1/(1+np.exp(-0.5))))

    def test_train(self):
        # Test that weights are updated correctly on training
        network = NeuralNetwork(3, 2, 1, 0.5)
        network.weights_input_to_hidden = test_w_i_h.copy()
        network.weights_hidden_to_output = test_w_h_o.copy()
        
        network.train(inputs, targets)
        self.assertTrue(np.allclose(network.weights_hidden_to_output, 
                                    np.array([[ 0.37275328], 
                                              [-0.03172939]])))
        self.assertTrue(np.allclose(network.weights_input_to_hidden,
                                    np.array([[ 0.10562014, -0.20185996], 
                                              [0.39775194, 0.50074398], 
                                              [-0.29887597, 0.19962801]])))

    def test_run(self):
        # Test correctness of run method
        network = NeuralNetwork(3, 2, 1, 0.5)
        network.weights_input_to_hidden = test_w_i_h.copy()
        network.weights_hidden_to_output = test_w_h_o.copy()
        self.assertTrue(np.allclose(network.run(inputs), 0.09998924))

suite = unittest.TestLoader().loadTestsFromModule(TestMethods())
unittest.TextTestRunner().run(suite)<jupyter_output>.....
----------------------------------------------------------------------
Ran 5 tests in 0.005s

OK
{'fraction_non_alphanumeric': 0.13087719298245615, 'fraction_numerical': 0.05473684210526316, 'mean_word_length': 3.146181818181818, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '541787', 'license': 'MIT', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1814, 'n_words': 428, 'path': 'neural_network.ipynb', 'repo_name': 'floanwelt/nn-python', 'size': '5700'}