sklearn-java

July 2, 2026 · View on GitHub

Build Coverage Javadoc Java License GitHub Discussions GitHub stars

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

ModuleStatusCoverage
math✅ CompleteVectors, dense matrices, random generators
core✅ CompleteEstimator/Predictor/Transformer interfaces, Pipeline
preprocessing✅ CompleteStandardScaler, MinMaxScaler, Normalizer, RobustScaler, MaxAbsScaler, OneHotEncoder, LabelEncoder, OrdinalEncoder, PolynomialFeatures, Binarizer, KBinsDiscretizer, FunctionTransformer
linear_model✅ CompleteLinearRegression, Ridge, Lasso, ElasticNet, LogisticRegression, SGDClassifier, SGDRegressor, RidgeCV, BayesianRidge, HuberRegressor, Perceptron, PassiveAggressiveClassifier/Regressor, LassoCV, ElasticNetCV, RidgeClassifier, RidgeClassifierCV
tree✅ CompleteDecisionTreeClassifier/Regressor, ExtraTreeClassifier/Regressor (random thresholds)
ensemble✅ CompleteRandomForest, AdaBoost, GradientBoosting, Bagging, Voting, Stacking, IsolationForest, ExtraTrees, ExtraTreesEmbedding, HistGradientBoosting
svm✅ CompleteSVC (one-vs-one), SVR, LinearSVC, linear/poly/RBF/sigmoid kernels
naive_bayes✅ CompleteGaussianNB, MultinomialNB, BernoulliNB
neighbors✅ CompleteKNeighborsClassifier, KNeighborsRegressor, NearestNeighbors, RadiusNeighborsClassifier/Regressor, LocalOutlierFactor, KernelDensity
cluster✅ CompleteKMeans (Lloyd's + k-means++), DBSCAN
feature_selection✅ CompleteVarianceThreshold, SelectKBest, SelectPercentile, GenericUnivariateSelect, RFE, SelectFromModel, FScoring (f_classif, f_regression, r_regression)
neural_network✅ CompleteMLPClassifier, MLPRegressor (ReLU/tanh/logistic, SGD/Adam, backprop)
dummy✅ CompleteDummyClassifier, DummyRegressor
impute✅ CompleteSimpleImputer (mean/median/most_frequent/constant)
decomposition✅ CompletePCA
metrics✅ CompleteClassificationMetrics, RegressionMetrics, RankingMetrics, PairwiseMetrics, ClusteringMetrics
model_selection✅ CompleteKFold, StratifiedKFold, LeaveOneOut, RepeatedKFold, CrossValidation, GridSearchCV, TrainTestSplit
pipeline✅ CompletePipeline, FeatureUnion, make_pipeline
utils✅ CompleteValidation, matrix/vector utilities
datasets❌ Not startedToy datasets will be added in Phase C

Quick Start

// 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);
// 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());
// 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

export JAVA_HOME=/opt/homebrew/opt/openjdk@21
./gradlew build

Testing

./gradlew test
./gradlew jacocoTestReport  # coverage report at build/reports/

Contributing

We welcome contributions! See 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 and the COVERAGE_PLAN.md for remaining algorithms.

Roadmap

PhaseFocusStatus
Phase A1Metrics + Model Selection✅ Complete
Phase A2Ensemble Methods✅ Complete
Phase A3Linear Models (SGD, RidgeCV, Bayesian, Huber, Perceptron, PA, CV variants)✅ Complete
Phase A4Naive Bayes + Neighbors (full: Multinomial/Bernoulli, NearestNeighbors, RadiusNeighbors, LOF, KernelDensity)✅ Complete
Phase A5Neural Network (MLP) + Feature Selection (SelectKBest, RFE, SelectFromModel)🔜 Written, testing pending
Phase BManifold, Impute, Pipeline (full)📋 Planned
Phase CDecomposition, Covariance, Cross-decomposition📋 Planned
Phase DSemi-supervised, Multi-output, Niche estimators📋 Planned

See COVERAGE_PLAN.md for full 400+ item breakdown.

License

Apache 2.0 — see LICENSE.

Stats

Alt