Build a Wine Quality Predictor with Tribuo: Java’s Production‑Ready ML Library
This tutorial walks Java developers through using Oracle’s Tribuo library to load the UCI wine‑quality dataset, train a regression model with CART‑based random forests, evaluate its performance, save the model, and later load it for predicting wine quality scores.
1. Introduction
Machine learning (ML) and artificial intelligence (AI) are transforming software development. As a Java developer, you might think of using Python for training models, but Java also has production‑grade ML tools. Tribuo is an open‑source library from Oracle that supports classification, regression, clustering and integrates with TensorFlow and other frameworks.
2. What is Tribuo?
Tribuo is a Java‑centric open‑source ML library for production environments. It simplifies building robust ML models, similar to Weka and Deeplearning4j, and supports supervised learning (regression, classification) and unsupervised learning (clustering). It enforces strong typing, supports ONNX import/export, and provides provenance tracking.
3. Supported Machine‑Learning Algorithms
Tribuo supports various tasks:
Classification – predict discrete categories.
Regression – predict continuous values such as wine quality.
Clustering – group unlabeled data.
4. Setting Up a Tribuo Project
Add the Tribuo dependency to pom.xml:
<dependency>
<groupId>org.tribuo</groupId>
<artifactId>tribuo-all</artifactId>
<version>4.3.2</version>
</dependency>Download the UCI wine quality dataset and place it in src/main/resources/dataset. The dataset contains 11 physicochemical features and a continuous “quality” column.
5. Class‑Level Variables
public static final String DATASET_PATH = "src/main/resources/dataset/winequality-red.csv";
public static final String MODEL_PATH = "src/main/resources/model/winequality-red-regressor.ser";
public Model<Regressor> model;
public Trainer<Regressor> trainer;
public Dataset<Regressor> trainSet;
public Dataset<Regressor> testSet;6. Loading and Splitting the Dataset
void createDatasets() throws Exception {
RegressionFactory regressionFactory = new RegressionFactory();
CSVLoader<Regressor> csvLoader = new CSVLoader<>(';', CSVIterator.QUOTE, regressionFactory);
DataSource<Regressor> dataSource = csvLoader.loadDataSource(Paths.get(DATASET_PATH), "quality");
TrainTestSplitter<Regressor> dataSplitter = new TrainTestSplitter<>(dataSource, 0.7, 1L);
trainSet = new MutableDataset<>(dataSplitter.getTrain());
testSet = new MutableDataset<>(dataSplitter.getTest());
}7. Training the Regression Model
void createTrainer() {
CARTRegressionTrainer subsamplingTree = new CARTRegressionTrainer(
Integer.MAX_VALUE,
AbstractCARTTrainer.MIN_EXAMPLES,
0.001f,
0.7f,
new MeanSquaredError(),
Trainer.DEFAULT_SEED);
trainer = new RandomForestTrainer<>(subsamplingTree, new AveragingCombiner(), 10);
model = trainer.train(trainSet);
}8. Evaluation
void evaluate(Model<Regressor> model, String name, Dataset<Regressor> dataset) {
RegressionEvaluator evaluator = new RegressionEvaluator();
RegressionEvaluation evaluation = evaluator.evaluate(model, dataset);
Regressor dim0 = new Regressor("DIM-0", Double.NaN);
log.info("MAE: " + evaluation.mae(dim0));
log.info("RMSE: " + evaluation.rmse(dim0));
log.info("R^2: " + evaluation.r2(dim0));
}Running the program yields training MAE 0.25, RMSE 0.34, R² 0.82 and testing MAE 0.49, RMSE 0.66, R² 0.34.
9. Saving the Model
void saveModel() throws Exception {
File modelFile = new File(MODEL_PATH);
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(modelFile))) {
oos.writeObject(model);
}
}10. Using the Model for Prediction
public static void main(String[] args) throws IOException, ClassNotFoundException {
File modelFile = new File("src/main/resources/model/winequality-red-regressor.ser");
Model<Regressor> loadedModel;
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(modelFile))) {
loadedModel = (Model<Regressor>) ois.readObject();
}
ArrayExample<Regressor> wine = new ArrayExample<>(new Regressor("quality", Double.NaN));
wine.add("fixed acidity", 7.4f);
wine.add("volatile acidity", 0.7f);
wine.add("citric acid", 0.47f);
wine.add("residual sugar", 1.9f);
wine.add("chlorides", 0.076f);
wine.add("free sulfur dioxide", 11.0f);
wine.add("total sulfur dioxide", 34.0f);
wine.add("density", 0.9978f);
wine.add("pH", 3.51f);
wine.add("sulphates", 0.56f);
wine.add("alcohol", 9.4f);
Prediction<Regressor> prediction = loadedModel.predict(wine);
double predictedQuality = prediction.getOutput().getValues()[0];
log.info("Predicted wine quality: " + predictedQuality);
}12. Conclusion
The article introduced Tribuo, its key features, supported algorithms, and demonstrated building, training, evaluating, saving, and reusing a regression model for wine quality prediction.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
