-
Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathPerceptronTest.java
More file actions
141 lines (112 loc) · 6.17 KB
/
Copy pathPerceptronTest.java
File metadata and controls
141 lines (112 loc) · 6.17 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.thealgorithms.machinelearning;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class PerceptronTest {
@Test
void learnsAndFunction() {
double[][] features = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
int[] labels = {0, 0, 0, 1};
Perceptron perceptron = new Perceptron(1.0, 20);
perceptron.fit(features, labels);
assertArrayEquals(labels, perceptron.predict(features));
assertTrue(perceptron.hasConverged());
assertTrue(perceptron.getEpochsRun() <= 20);
}
@Test
void predictsUnseenSamples() {
double[][] features = {{-2, -1}, {-1, -2}, {1, 2}, {2, 1}};
int[] labels = {0, 0, 1, 1};
Perceptron perceptron = new Perceptron(0.5, 20);
perceptron.fit(features, labels);
assertEquals(0, perceptron.predict(new double[] {-3, -1}));
assertEquals(1, perceptron.predict(new double[] {3, 1}));
}
@Test
void batchPredictionMatchesIndividualPredictions() {
double[][] features = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
int[] labels = {0, 0, 0, 1};
double[][] samples = {{0, 0}, {1, 0}, {1, 1}};
Perceptron perceptron = new Perceptron(1.0, 20);
perceptron.fit(features, labels);
assertArrayEquals(new int[] {0, 0, 1}, perceptron.predict(samples));
int[] individualPredictions = {perceptron.predict(samples[0]), perceptron.predict(samples[1]), perceptron.predict(samples[2])};
assertArrayEquals(individualPredictions, perceptron.predict(samples));
}
@Test
void emptyBatchProducesEmptyPrediction() {
Perceptron perceptron = new Perceptron(1.0, 10);
perceptron.fit(new double[][] {{0}}, new int[] {0});
assertArrayEquals(new int[] {}, perceptron.predict(new double[][] {}));
}
@Test
void nonSeparableDataStopsAtEpochLimitWithoutConverging() {
double[][] features = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
int[] labels = {0, 1, 1, 0};
Perceptron perceptron = new Perceptron(1.0, 8);
perceptron.fit(features, labels);
assertFalse(perceptron.hasConverged());
assertEquals(8, perceptron.getEpochsRun());
}
@Test
void fittingResetsPreviousModel() {
Perceptron perceptron = new Perceptron(1.0, 20);
perceptron.fit(new double[][] {{0}, {1}}, new int[] {0, 1});
perceptron.fit(new double[][] {{0}, {1}}, new int[] {1, 0});
assertArrayEquals(new int[] {1, 0}, perceptron.predict(new double[][] {{0}, {1}}));
}
@Test
void weightsAreReturnedAsDefensiveCopy() {
Perceptron perceptron = new Perceptron(1.0, 10);
perceptron.fit(new double[][] {{0}, {1}}, new int[] {0, 1});
double[] weights = perceptron.getWeights();
weights[0] = 1000;
assertEquals(1, perceptron.predict(new double[] {1}));
}
@Test
void predictionBeforeFitThrows() {
Perceptron perceptron = new Perceptron(1.0, 10);
assertThrows(IllegalStateException.class, () -> perceptron.predict(new double[] {1}));
assertThrows(IllegalStateException.class, () -> perceptron.predict(new double[][] {}));
assertThrows(IllegalStateException.class, perceptron::getWeights);
assertThrows(IllegalStateException.class, perceptron::getBias);
assertThrows(IllegalStateException.class, perceptron::hasConverged);
assertThrows(IllegalStateException.class, perceptron::getEpochsRun);
}
@Test
void invalidHyperparametersThrow() {
assertThrows(IllegalArgumentException.class, () -> new Perceptron(0.0, 10));
assertThrows(IllegalArgumentException.class, () -> new Perceptron(-1.0, 10));
assertThrows(IllegalArgumentException.class, () -> new Perceptron(Double.NaN, 10));
assertThrows(IllegalArgumentException.class, () -> new Perceptron(Double.POSITIVE_INFINITY, 10));
assertThrows(IllegalArgumentException.class, () -> new Perceptron(1.0, 0));
assertThrows(IllegalArgumentException.class, () -> new Perceptron(1.0, -1));
}
@Test
void invalidTrainingDataThrows() {
Perceptron perceptron = new Perceptron(1.0, 10);
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(null, new int[] {0}));
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}}, null));
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {}, new int[] {}));
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}}, new int[] {}));
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}, {1, 2}}, new int[] {0, 1}));
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {null}, new int[] {0}));
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{}}, new int[] {0}));
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}}, new int[] {2}));
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{Double.NaN}}, new int[] {0}));
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{Double.POSITIVE_INFINITY}}, new int[] {0}));
}
@Test
void invalidPredictionDataThrows() {
Perceptron perceptron = new Perceptron(1.0, 10);
perceptron.fit(new double[][] {{0, 0}}, new int[] {0});
assertThrows(IllegalArgumentException.class, () -> perceptron.predict((double[]) null));
assertThrows(IllegalArgumentException.class, () -> perceptron.predict(new double[] {0}));
assertThrows(IllegalArgumentException.class, () -> perceptron.predict(new double[] {0, Double.NaN}));
assertThrows(IllegalArgumentException.class, () -> perceptron.predict((double[][]) null));
assertThrows(IllegalArgumentException.class, () -> perceptron.predict(new double[][] {{0, 0}, null}));
}
}