· Java ML · 3 min read
Getting Started with SuperML Java Framework
The SuperML Java framework brings machine learning capabilities directly to your Java applications with an intuitive, object-oriented API. This tutorial will guide you through setting up the framework and creating your first ML model.
Prerequisites
- Java 8 or higher
- Maven or Gradle build tool
- Basic understanding of Java programming
Installation
Maven
Add the SuperML Java dependency to your pom.xml
:
<dependency>
<groupId>org.superml</groupId>
<artifactId>superml-java</artifactId>
<version>1.0.1</version>
</dependency>
Gradle
Add the dependency to your build.gradle
:
implementation 'org.superml:superml-java:1.0.0'
Your First ML Model
Let’s create a simple linear regression model to predict house prices:
import org.superml.data.DataLoader;
import org.superml.data.Dataset;
import org.superml.models.LinearRegression;
import org.superml.metrics.Metrics;
public class HousePricePredictor {
public static void main(String[] args) {
// Load dataset from CSV file
Dataset data = DataLoader.fromCSV("house_prices.csv");
// Split data: 80% training, 20% testing
data.split(0.8);
// Create and configure the model
LinearRegression model = new LinearRegression();
model.setLearningRate(0.01);
model.setMaxIterations(1000);
// Train the model
model.fit(data.getTrainX(), data.getTrainY());
// Make predictions on test data
double[] predictions = model.predict(data.getTestX());
// Evaluate model performance
double rmse = Metrics.rmse(data.getTestY(), predictions);
double r2 = Metrics.r2Score(data.getTestY(), predictions);
System.out.println("RMSE: " + rmse);
System.out.println("R² Score: " + r2);
// Save the trained model
model.save("house_price_model.superml");
}
}
Key Components
DataLoader
The DataLoader
class provides various methods to load data:
// From CSV file
Dataset data = DataLoader.fromCSV("data.csv");
// From arrays
double[][] features = {{1, 2}, {3, 4}, {5, 6}};
double[] labels = {1.5, 3.5, 5.5};
Dataset data = DataLoader.fromArrays(features, labels);
// From database
Dataset data = DataLoader.fromDatabase(connection, "SELECT * FROM training_data");
Dataset
The Dataset
class provides convenient methods for data manipulation:
// Split data
data.split(0.8); // 80% train, 20% test
data.split(0.7, 0.15, 0.15); // 70% train, 15% validation, 15% test
// Get data subsets
double[][] trainX = data.getTrainX();
double[] trainY = data.getTrainY();
double[][] testX = data.getTestX();
double[] testY = data.getTestY();
// Data preprocessing
data.normalize(); // Normalize features
data.standardize(); // Standardize features
data.shuffle(); // Shuffle data
Model Training
All models in SuperML Java follow a consistent interface:
// Create model
LinearRegression model = new LinearRegression();
// Configure hyperparameters
model.setLearningRate(0.01);
model.setMaxIterations(1000);
model.setRegularization(0.001);
// Train model
model.fit(trainX, trainY);
// Make predictions
double[] predictions = model.predict(testX);
// Get model information
String modelInfo = model.toString();
double[] coefficients = model.getCoefficients();
Data Preprocessing
SuperML Java provides built-in preprocessing utilities:
import org.superml.preprocessing.*;
// Feature scaling
StandardScaler scaler = new StandardScaler();
double[][] scaledData = scaler.fitTransform(data);
// Encoding categorical variables
LabelEncoder encoder = new LabelEncoder();
int[] encodedLabels = encoder.fitTransform(categoryData);
// Handling missing values
SimpleImputer imputer = new SimpleImputer(ImputeStrategy.MEAN);
double[][] imputedData = imputer.fitTransform(dataWithMissing);
Model Persistence
Save and load trained models easily:
// Save model
model.save("my_model.superml");
// Load model
LinearRegression loadedModel = LinearRegression.load("my_model.superml");
// Use loaded model
double[] newPredictions = loadedModel.predict(newData);
Error Handling
SuperML Java provides comprehensive error handling:
try {
Dataset data = DataLoader.fromCSV("data.csv");
LinearRegression model = new LinearRegression();
model.fit(data.getTrainX(), data.getTrainY());
} catch (DataLoadException e) {
System.err.println("Error loading data: " + e.getMessage());
} catch (ModelTrainingException e) {
System.err.println("Error training model: " + e.getMessage());
} catch (SuperMLException e) {
System.err.println("General SuperML error: " + e.getMessage());
}
Next Steps
Now that you’ve learned the basics, explore these topics:
- Classification with Logistic Regression - Learn binary and multiclass classification
- Decision Trees and Random Forest - Tree-based algorithms
- Neural Networks in Java - Deep learning with SuperML
- Model Evaluation Techniques - Comprehensive model assessment
Documentation
For complete API documentation, visit https://superml-java.superml.org/docs
Example Projects
Check out our GitHub repository for complete example projects and sample datasets.
The SuperML Java framework makes machine learning accessible to Java developers without sacrificing performance or flexibility. Start building intelligent applications today!