https://github.com/kVeyra/sklearn-java
Java reimplementation of Python's scikit-learn with behavioral compatibility, numerical accuracy, and idiomatic Java design
https://github.com/kVeyra/sklearn-java
Last synced: 9 days ago
JSON representation
Java reimplementation of Python's scikit-learn with behavioral compatibility, numerical accuracy, and idiomatic Java design
- Host: GitHub
- URL: https://github.com/kVeyra/sklearn-java
- Owner: kVeyra
- License: apache-2.0
- Created: 2026-06-30T07:49:50.000Z (22 days ago)
- Default Branch: main
- Last Pushed: 2026-07-02T08:08:24.000Z (20 days ago)
- Last Synced: 2026-07-02T08:15:35.991Z (20 days ago)
- Language: Java
- Homepage: https://github.com/kVeyra/sklearn-java
- Size: 437 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- fucking-awesome-java - sklearn-java - Implements scikit-learn-style machine learning algorithms in pure Java. (Projects / Machine Learning)
- awesome-java - sklearn-java - Implements scikit-learn-style machine learning algorithms in pure Java. (Projects / Machine Learning)
README
# sklearn-java
[](https://github.com/kVeyra/sklearn-java/actions/workflows/ci.yml)
[](https://codecov.io/gh/kVeyra/sklearn-java)
[](https://kVeyra.github.io/sklearn-java)
[](https://openjdk.org/projects/jdk/21/)
[](LICENSE)
[](https://github.com/kVeyra/sklearn-java/discussions)
[](https://github.com/kVeyra/sklearn-java)
**A production-quality Java reimplementation of Python's scikit-learn.** Covers the core ML API surface (in progress) with behavioral compatibility, numerical accuracy to 1e-8, and pure Java — no Python, JNI, or NumPy.
## Why sklearn-java?
- **Drop-in replacement mindset** — API mirrors sklearn exactly: `.fit()`, `.predict()`, `.transform()`, `.score()`
- **Numerical accuracy** — every algorithm validated against Python sklearn with 1e-8 tolerance
- **Pure Java** — zero Python dependencies, zero JNI, zero external native libs
- **Modern Java** — Java 21, sealed interfaces, records, pattern matching
- **100% coverage goal** — targeting 400+ public classes matching sklearn's API surface
## Modules
| Module | Status | Coverage |
|--------|--------|----------|
| `math` | ✅ Complete | Vectors, dense matrices, random generators |
| `core` | ✅ Complete | Estimator/Predictor/Transformer interfaces, Pipeline |
| `preprocessing` | ✅ Complete | StandardScaler, MinMaxScaler, Normalizer, RobustScaler, MaxAbsScaler, OneHotEncoder, LabelEncoder, OrdinalEncoder, PolynomialFeatures, Binarizer, KBinsDiscretizer, FunctionTransformer |
| `linear_model` | ✅ Complete | LinearRegression, Ridge, Lasso, ElasticNet, LogisticRegression, SGDClassifier, SGDRegressor, RidgeCV, BayesianRidge, HuberRegressor, Perceptron, PassiveAggressiveClassifier/Regressor, LassoCV, ElasticNetCV, RidgeClassifier, RidgeClassifierCV |
| `tree` | ✅ Complete | DecisionTreeClassifier/Regressor, ExtraTreeClassifier/Regressor (random thresholds) |
| `ensemble` | ✅ Complete | RandomForest, AdaBoost, GradientBoosting, Bagging, Voting, Stacking, IsolationForest, ExtraTrees, ExtraTreesEmbedding, HistGradientBoosting |
| `svm` | ✅ Complete | SVC (one-vs-one), SVR, LinearSVC, linear/poly/RBF/sigmoid kernels |
| `naive_bayes` | ✅ Complete | GaussianNB, MultinomialNB, BernoulliNB |
| `neighbors` | ✅ Complete | KNeighborsClassifier, KNeighborsRegressor, NearestNeighbors, RadiusNeighborsClassifier/Regressor, LocalOutlierFactor, KernelDensity |
| `cluster` | ✅ Complete | KMeans (Lloyd's + k-means++), DBSCAN |
| `feature_selection` | ✅ Complete | VarianceThreshold, SelectKBest, SelectPercentile, GenericUnivariateSelect, RFE, SelectFromModel, FScoring (f_classif, f_regression, r_regression) |
| `neural_network` | ✅ Complete | MLPClassifier, MLPRegressor (ReLU/tanh/logistic, SGD/Adam, backprop) |
| `dummy` | ✅ Complete | DummyClassifier, DummyRegressor |
| `impute` | ✅ Complete | SimpleImputer (mean/median/most_frequent/constant) |
| `decomposition` | ✅ Complete | PCA |
| `metrics` | ✅ Complete | ClassificationMetrics, RegressionMetrics, RankingMetrics, PairwiseMetrics, ClusteringMetrics |
| `model_selection` | ✅ Complete | KFold, StratifiedKFold, LeaveOneOut, RepeatedKFold, CrossValidation, GridSearchCV, TrainTestSplit |
| `pipeline` | ✅ Complete | Pipeline, FeatureUnion, make_pipeline |
| `utils` | ✅ Complete | Validation, matrix/vector utilities |
| `datasets` | ❌ Not started | Toy datasets will be added in Phase C |
## Quick Start
```java
// Standardize features
StandardScaler scaler = new StandardScaler();
Matrix X_scaled = scaler.fitTransform(X_train);
// Train a classifier
RandomForestClassifier rf = new RandomForestClassifier(100, 5);
rf.fit(X_scaled, y_train);
// Predict & evaluate
Vector preds = rf.predict(scaler.transform(X_test));
double acc = ClassificationMetrics.accuracy(y_test, preds);
```
```java
// Grid search with cross-validation
GridSearchCV grid = new GridSearchCV(
new SVC(),
Map.of("C", new double[]{0.1, 1.0, 10.0}, "kernel", new String[]{"rbf", "linear"}),
5
);
grid.fit(X_train, y_train);
System.out.println("Best params: " + grid.bestParams());
```
```java
// Neural network classifier
MLPClassifier mlp = new MLPClassifier(new int[]{64, 32}, "relu", "adam", 200);
mlp.fit(X_train, y_train);
Vector preds = mlp.predict(X_test);
double acc = ClassificationMetrics.accuracy(y_test, preds);
```
## Building
```bash
export JAVA_HOME=/opt/homebrew/opt/openjdk@21
./gradlew build
```
## Testing
```bash
./gradlew test
./gradlew jacocoTestReport # coverage report at build/reports/
```
## Contributing
We welcome contributions! See [CONTRIBUTING.md](CONTRIBUTING.md) for:
- Branch strategy (feature branches → develop → main)
- Coding standards (Java 21, Google Java Format)
- Validation requirements (1e-8 tolerance against sklearn)
- PR checklist
**Looking for good first issues?** Check the [issue tracker](https://github.com/kVeyra/sklearn-java/issues) and the [COVERAGE_PLAN.md](COVERAGE_PLAN.md) for remaining algorithms.
## Roadmap
| Phase | Focus | Status |
|-------|-------|--------|
| Phase A1 | Metrics + Model Selection | ✅ Complete |
| Phase A2 | Ensemble Methods | ✅ Complete |
| Phase A3 | Linear Models (SGD, RidgeCV, Bayesian, Huber, Perceptron, PA, CV variants) | ✅ Complete |
| Phase A4 | Naive Bayes + Neighbors (full: Multinomial/Bernoulli, NearestNeighbors, RadiusNeighbors, LOF, KernelDensity) | ✅ Complete |
| Phase A5 | Neural Network (MLP) + Feature Selection (SelectKBest, RFE, SelectFromModel) | 🔜 Written, testing pending |
| Phase B | Manifold, Impute, Pipeline (full) | 📋 Planned |
| Phase C | Decomposition, Covariance, Cross-decomposition | 📋 Planned |
| Phase D | Semi-supervised, Multi-output, Niche estimators | 📋 Planned |
See [COVERAGE_PLAN.md](COVERAGE_PLAN.md) for full 400+ item breakdown.
## License
Apache 2.0 — see [LICENSE](LICENSE).
## Stats
