|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# NEURAL NETWORK IMPLEMENTATION - TESTS |
| 4 | +# 2022 (c) Micha Johannes Birklbauer |
| 5 | +# https://github.com/michabirklbauer/ |
| 6 | + |
| 7 | + |
| 8 | +def test_bcc(): |
| 9 | + |
| 10 | + #### Binary-class Classification #### |
| 11 | + |
| 12 | + from zipfile import ZipFile as zip |
| 13 | + |
| 14 | + with zip("data.zip") as f: |
| 15 | + f.extractall() |
| 16 | + f.close() |
| 17 | + |
| 18 | + from neuralnet import NeuralNetwork |
| 19 | + import numpy as np |
| 20 | + import pandas as pd |
| 21 | + from sklearn.metrics import accuracy_score |
| 22 | + from sklearn.preprocessing import OneHotEncoder |
| 23 | + from sklearn.model_selection import train_test_split |
| 24 | + |
| 25 | + data = pd.read_csv("binaryclass_train.csv", header = None) |
| 26 | + data["label"] = data[1].apply(lambda x: 1 if x == "M" else 0) |
| 27 | + train, test = train_test_split(data, test_size = 0.3) |
| 28 | + train_data = train.loc[:, ~train.columns.isin([0, 1, "label"])].to_numpy() |
| 29 | + train_target = train["label"].to_numpy() |
| 30 | + test_data = test.loc[:, ~test.columns.isin([0, 1, "label"])].to_numpy() |
| 31 | + test_target = test["label"].to_numpy() |
| 32 | + |
| 33 | + NN = NeuralNetwork(input_size = train_data.shape[1]) |
| 34 | + NN.add_layer(16, "relu") |
| 35 | + NN.add_layer(16, "relu") |
| 36 | + NN.add_layer(1, "sigmoid") |
| 37 | + NN.compile(loss = "binary crossentropy") |
| 38 | + NN.summary() |
| 39 | + |
| 40 | + hist = NN.fit(train_data, train_target, epochs = 1000, batch_size = 32, learning_rate = 0.01) |
| 41 | + |
| 42 | + train_predictions = np.round(NN.predict(train_data)) |
| 43 | + train_acc = accuracy_score(train["label"].to_numpy(), train_predictions) |
| 44 | + test_predictions = np.round(NN.predict(test_data)) |
| 45 | + test_acc = accuracy_score(test["label"].to_numpy(), test_predictions) |
| 46 | + |
| 47 | + import os |
| 48 | + os.remove("binaryclass_train.csv") |
| 49 | + os.remove("multiclass_train.csv") |
| 50 | + |
| 51 | + assert train_acc > 0.85 and test_acc > 0.85 |
| 52 | + |
| 53 | +def test_mcc(): |
| 54 | + |
| 55 | + #### Multi-class Classification #### |
| 56 | + |
| 57 | + from zipfile import ZipFile as zip |
| 58 | + |
| 59 | + with zip("data.zip") as f: |
| 60 | + f.extractall() |
| 61 | + f.close() |
| 62 | + |
| 63 | + from neuralnet import NeuralNetwork |
| 64 | + import numpy as np |
| 65 | + import pandas as pd |
| 66 | + from sklearn.metrics import accuracy_score |
| 67 | + from sklearn.preprocessing import OneHotEncoder |
| 68 | + from sklearn.model_selection import train_test_split |
| 69 | + |
| 70 | + data = pd.read_csv("multiclass_train.csv") |
| 71 | + train, test = train_test_split(data, test_size = 0.3) |
| 72 | + train_data = train.loc[:, train.columns != "label"].to_numpy() / 255 |
| 73 | + train_target = train["label"].to_numpy() |
| 74 | + test_data = test.loc[:, test.columns != "label"].to_numpy() / 255 |
| 75 | + test_target = test["label"].to_numpy() |
| 76 | + |
| 77 | + one_hot = OneHotEncoder(sparse = False, categories = "auto") |
| 78 | + train_target = one_hot.fit_transform(train_target.reshape(-1, 1)) |
| 79 | + test_target = one_hot.transform(test_target.reshape(-1, 1)) |
| 80 | + |
| 81 | + NN = NeuralNetwork(input_size = train_data.shape[1]) |
| 82 | + NN.add_layer(32, "relu") |
| 83 | + NN.add_layer(16, "relu") |
| 84 | + NN.add_layer(10, "softmax") |
| 85 | + NN.compile(loss = "categorical crossentropy") |
| 86 | + NN.summary() |
| 87 | + |
| 88 | + hist = NN.fit(train_data, train_target, epochs = 30, batch_size = 16, learning_rate = 0.05) |
| 89 | + |
| 90 | + train_predictions = np.argmax(NN.predict(train_data), axis = 1) |
| 91 | + train_acc = accuracy_score(train["label"].to_numpy(), train_predictions) |
| 92 | + test_predictions = np.argmax(NN.predict(test_data), axis = 1) |
| 93 | + test_acc = accuracy_score(test["label"].to_numpy(), test_predictions) |
| 94 | + |
| 95 | + import os |
| 96 | + os.remove("binaryclass_train.csv") |
| 97 | + os.remove("multiclass_train.csv") |
| 98 | + |
| 99 | + assert train_acc > 0.85 and test_acc > 0.85 |
0 commit comments