Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7601 +/- ##
============================================
+ Coverage 81.01% 81.04% +0.03%
- Complexity 7740 7776 +36
============================================
Files 825 826 +1
Lines 24594 24674 +80
Branches 4810 4827 +17
============================================
+ Hits 19925 19998 +73
- Misses 3906 3909 +3
- Partials 763 767 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
alxkm
left a comment
There was a problem hiding this comment.
Implementation looks good overall - validation runs before any mutation in fit(), getWeights() returns a defensive copy, and there's no Random, so the tests are deterministic. I traced AND and XOR by hand and the convergence behaviour matches what the tests assert.
Two things:
1. PMD is failing the build. TooManyStaticImports allows at most 4 static imports per file. Import Assertions once and qualify the calls, as the rest of the test suite does:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;- assertArrayEquals(labels, perceptron.predict(features));
+ Assertions.assertArrayEquals(labels, perceptron.predict(features));A wildcard static import would silence the rule but star imports are rejected too. mvn -B clean verify reproduces it locally.
2. getBias() has no positive-path coverage. It's only reached through the not-fitted path, so return bias; never executes. getWeights() is similar - the copy semantics are tested, the learned values aren't. One test covers both:
@Test
void learnsExpectedWeightsAndBias() {
Perceptron perceptron = new Perceptron(1.0, 10);
perceptron.fit(new double[][] {{0}, {1}}, new int[] {0, 1});
Assertions.assertArrayEquals(new double[] {1.0}, perceptron.getWeights());
Assertions.assertEquals(-1.0, perceptron.getBias());
}The model converges on epoch 3 with exact values, so no delta is needed.
| 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; |
There was a problem hiding this comment.
This is what's failing the build. PMD's TooManyStaticImports allows at most 4 static imports per file; there are 5 here.
Import Assertions once and qualify the calls, as the rest of the test suite does:
| 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.Assertions; | |
| import org.junit.jupiter.api.Test; |
Then update the call sites:
- assertArrayEquals(labels, perceptron.predict(features));
- assertTrue(perceptron.hasConverged());
+ Assertions.assertArrayEquals(labels, perceptron.predict(features));
+ Assertions.assertTrue(perceptron.hasConverged());A wildcard static import would silence this rule but star imports are rejected too. Verify
Summary
Adds a binary Perceptron classifier to
com.thealgorithms.machinelearning. The implementation uses deterministic zero initialization, an explicit bias term, configurable learning rate and epoch limit, input validation, single and batch prediction, defensive weight access, and convergence metadata.Tests
Verification
git diff --checkpassed.Checklist
Closes #7599