-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNNClass.py
More file actions
93 lines (78 loc) · 2.33 KB
/
Copy pathNNClass.py
File metadata and controls
93 lines (78 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Small neural network successfully completing classification task.
Created on Fri Oct 1 17:39:47 2021
@author: maxvondanwitz
"""
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
import tensorflow as tf
allData = np.load('normalizedData.npy')
labels = np.load('clusterLabels.npy')
# We use the kMeans-clustering result as ground truth.
plt.scatter(allData[:,0], allData[:,1], c=labels)
plt.xlabel('x')
plt.ylabel('y')
ax = plt.gca()
ax.set(xlim=(-2.5, 4), ylim=(-2.5, 4))
ax.set_aspect('equal','box')
plt.savefig('classGroundTrut.pdf')
plt.show()
x_train, x_test, y_train, y_test = train_test_split(allData, labels, random_state=91)
# Define NN
nn_class = tf.keras.models.Sequential([
tf.keras.layers.Dense(4, activation='relu'),
tf.keras.layers.Dense(4, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')])
# nn_class.compile(loss='binary_crossentropy', optimizer='adam')
nn_class.compile(loss='mse', optimizer='adam')
# Training
results_class = nn_class.fit(x_train, y_train, epochs=100, batch_size= 1, verbose=1)
# This is what we fed into the NN
plt.scatter(x_train[:,0], x_train[:,1], c=y_train)
plt.xlabel('x')
plt.ylabel('y')
ax = plt.gca()
ax.set(xlim=(-2.5, 4), ylim=(-2.5, 4))
ax.set_aspect('equal','box')
plt.savefig('classTrainData.pdf')
plt.show()
# Testing
y_pred = nn_class.predict(x_test)
# This is what we test with
plt.scatter(x_test[:,0], x_test[:,1])
plt.xlabel('x')
plt.ylabel('y')
ax = plt.gca()
ax.set(xlim=(-2.5, 4), ylim=(-2.5, 4))
ax.set_aspect('equal','box')
plt.savefig('testDataAlone.pdf')
plt.show()
# Pretty good result
plt.scatter(x_test[:,0], x_test[:,1], c=y_pred)
plt.xlabel('x')
plt.ylabel('y')
ax = plt.gca()
ax.set(xlim=(-2.5, 4), ylim=(-2.5, 4))
ax.set_aspect('equal','box')
plt.savefig('classCheckPred.pdf')
plt.show()
plt.scatter(x_test[:,0], x_test[:,1], c=y_pred)
plt.xlabel('x')
plt.ylabel('y')
ax = plt.gca()
ax.set(xlim=(-2.5, 4), ylim=(-2.5, 4))
ax.set_aspect('equal','box')
plt.savefig('classCheckPredTransparent.pdf', transparent=True)
plt.show()
# True assignments of testing data
plt.scatter(x_test[:,0], x_test[:,1], c=y_test)
plt.xlabel('x')
plt.ylabel('y')
ax = plt.gca()
ax.set(xlim=(-2.5, 4), ylim=(-2.5, 4))
ax.set_aspect('equal','box')
plt.savefig('classCheckTestData.pdf')
plt.show()