|
| 1 | +package com.thealgorithms.machinelearning; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +class PerceptronTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + void learnsAndFunction() { |
| 15 | + double[][] features = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}; |
| 16 | + int[] labels = {0, 0, 0, 1}; |
| 17 | + |
| 18 | + Perceptron perceptron = new Perceptron(1.0, 20); |
| 19 | + perceptron.fit(features, labels); |
| 20 | + |
| 21 | + assertArrayEquals(labels, perceptron.predict(features)); |
| 22 | + assertTrue(perceptron.hasConverged()); |
| 23 | + assertTrue(perceptron.getEpochsRun() <= 20); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void predictsUnseenSamples() { |
| 28 | + double[][] features = {{-2, -1}, {-1, -2}, {1, 2}, {2, 1}}; |
| 29 | + int[] labels = {0, 0, 1, 1}; |
| 30 | + |
| 31 | + Perceptron perceptron = new Perceptron(0.5, 20); |
| 32 | + perceptron.fit(features, labels); |
| 33 | + |
| 34 | + assertEquals(0, perceptron.predict(new double[] {-3, -1})); |
| 35 | + assertEquals(1, perceptron.predict(new double[] {3, 1})); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void batchPredictionMatchesIndividualPredictions() { |
| 40 | + double[][] features = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}; |
| 41 | + int[] labels = {0, 0, 0, 1}; |
| 42 | + double[][] samples = {{0, 0}, {1, 0}, {1, 1}}; |
| 43 | + |
| 44 | + Perceptron perceptron = new Perceptron(1.0, 20); |
| 45 | + perceptron.fit(features, labels); |
| 46 | + |
| 47 | + assertArrayEquals(new int[] {0, 0, 1}, perceptron.predict(samples)); |
| 48 | + int[] individualPredictions = {perceptron.predict(samples[0]), perceptron.predict(samples[1]), perceptron.predict(samples[2])}; |
| 49 | + assertArrayEquals(individualPredictions, perceptron.predict(samples)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void emptyBatchProducesEmptyPrediction() { |
| 54 | + Perceptron perceptron = new Perceptron(1.0, 10); |
| 55 | + perceptron.fit(new double[][] {{0}}, new int[] {0}); |
| 56 | + |
| 57 | + assertArrayEquals(new int[] {}, perceptron.predict(new double[][] {})); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void nonSeparableDataStopsAtEpochLimitWithoutConverging() { |
| 62 | + double[][] features = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}; |
| 63 | + int[] labels = {0, 1, 1, 0}; |
| 64 | + |
| 65 | + Perceptron perceptron = new Perceptron(1.0, 8); |
| 66 | + perceptron.fit(features, labels); |
| 67 | + |
| 68 | + assertFalse(perceptron.hasConverged()); |
| 69 | + assertEquals(8, perceptron.getEpochsRun()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void fittingResetsPreviousModel() { |
| 74 | + Perceptron perceptron = new Perceptron(1.0, 20); |
| 75 | + perceptron.fit(new double[][] {{0}, {1}}, new int[] {0, 1}); |
| 76 | + perceptron.fit(new double[][] {{0}, {1}}, new int[] {1, 0}); |
| 77 | + |
| 78 | + assertArrayEquals(new int[] {1, 0}, perceptron.predict(new double[][] {{0}, {1}})); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void weightsAreReturnedAsDefensiveCopy() { |
| 83 | + Perceptron perceptron = new Perceptron(1.0, 10); |
| 84 | + perceptron.fit(new double[][] {{0}, {1}}, new int[] {0, 1}); |
| 85 | + |
| 86 | + double[] weights = perceptron.getWeights(); |
| 87 | + weights[0] = 1000; |
| 88 | + |
| 89 | + assertEquals(1, perceptron.predict(new double[] {1})); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void predictionBeforeFitThrows() { |
| 94 | + Perceptron perceptron = new Perceptron(1.0, 10); |
| 95 | + |
| 96 | + assertThrows(IllegalStateException.class, () -> perceptron.predict(new double[] {1})); |
| 97 | + assertThrows(IllegalStateException.class, () -> perceptron.predict(new double[][] {})); |
| 98 | + assertThrows(IllegalStateException.class, perceptron::getWeights); |
| 99 | + assertThrows(IllegalStateException.class, perceptron::getBias); |
| 100 | + assertThrows(IllegalStateException.class, perceptron::hasConverged); |
| 101 | + assertThrows(IllegalStateException.class, perceptron::getEpochsRun); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void invalidHyperparametersThrow() { |
| 106 | + assertThrows(IllegalArgumentException.class, () -> new Perceptron(0.0, 10)); |
| 107 | + assertThrows(IllegalArgumentException.class, () -> new Perceptron(-1.0, 10)); |
| 108 | + assertThrows(IllegalArgumentException.class, () -> new Perceptron(Double.NaN, 10)); |
| 109 | + assertThrows(IllegalArgumentException.class, () -> new Perceptron(Double.POSITIVE_INFINITY, 10)); |
| 110 | + assertThrows(IllegalArgumentException.class, () -> new Perceptron(1.0, 0)); |
| 111 | + assertThrows(IllegalArgumentException.class, () -> new Perceptron(1.0, -1)); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void invalidTrainingDataThrows() { |
| 116 | + Perceptron perceptron = new Perceptron(1.0, 10); |
| 117 | + |
| 118 | + assertThrows(IllegalArgumentException.class, () -> perceptron.fit(null, new int[] {0})); |
| 119 | + assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}}, null)); |
| 120 | + assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {}, new int[] {})); |
| 121 | + assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}}, new int[] {})); |
| 122 | + assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}, {1, 2}}, new int[] {0, 1})); |
| 123 | + assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {null}, new int[] {0})); |
| 124 | + assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{}}, new int[] {0})); |
| 125 | + assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}}, new int[] {2})); |
| 126 | + assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{Double.NaN}}, new int[] {0})); |
| 127 | + assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{Double.POSITIVE_INFINITY}}, new int[] {0})); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void invalidPredictionDataThrows() { |
| 132 | + Perceptron perceptron = new Perceptron(1.0, 10); |
| 133 | + perceptron.fit(new double[][] {{0, 0}}, new int[] {0}); |
| 134 | + |
| 135 | + assertThrows(IllegalArgumentException.class, () -> perceptron.predict((double[]) null)); |
| 136 | + assertThrows(IllegalArgumentException.class, () -> perceptron.predict(new double[] {0})); |
| 137 | + assertThrows(IllegalArgumentException.class, () -> perceptron.predict(new double[] {0, Double.NaN})); |
| 138 | + assertThrows(IllegalArgumentException.class, () -> perceptron.predict((double[][]) null)); |
| 139 | + assertThrows(IllegalArgumentException.class, () -> perceptron.predict(new double[][] {{0, 0}, null})); |
| 140 | + } |
| 141 | +} |
0 commit comments