use of org.apache.ignite.ml.regressions.linear.LinearRegressionModel in project ignite by apache.
the class LocalModelsTest method importExportLinearRegressionModelTest.
/**
*/
@Test
public void importExportLinearRegressionModelTest() throws IOException {
executeModelTest(mdlFilePath -> {
LinearRegressionModel model = new LinearRegressionModel(new DenseLocalOnHeapVector(new double[] { 1, 2 }), 3);
Exporter<LinearRegressionModel, String> exporter = new FileExporter<>();
model.saveModel(exporter, mdlFilePath);
LinearRegressionModel load = exporter.load(mdlFilePath);
Assert.assertNotNull(load);
Assert.assertEquals("", model, load);
return null;
});
}
use of org.apache.ignite.ml.regressions.linear.LinearRegressionModel in project ignite by apache.
the class BostonHousePricesPredictionExample method toString.
/**
* Prepare pretty string for model.
*
* @param mdl Model.
* @return String representation of model.
*/
private static String toString(LinearRegressionModel mdl) {
BiFunction<Integer, Double, String> formatter = (idx, val) -> String.format("%.2f*f%d", val, idx);
Vector weights = mdl.weights();
StringBuilder sb = new StringBuilder(formatter.apply(0, weights.get(0)));
for (int fid = 1; fid < weights.size(); fid++) {
double w = weights.get(fid);
sb.append(" ").append(w > 0 ? "+" : "-").append(" ").append(formatter.apply(fid, Math.abs(w)));
}
double intercept = mdl.intercept();
sb.append(" ").append(intercept > 0 ? "+" : "-").append(" ").append(String.format("%.2f", Math.abs(intercept)));
return sb.toString();
}
use of org.apache.ignite.ml.regressions.linear.LinearRegressionModel in project ignite by apache.
the class BostonHousePricesPredictionExample method main.
/**
* Runs example.
*/
public static void main(String[] args) throws IOException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
System.out.println(">>> Fill dataset cache.");
dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.BOSTON_HOUSE_PRICES);
DatasetTrainer<LinearRegressionModel, Double> trainer = new LinearRegressionLSQRTrainer().withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(0));
// This vectorizer works with values in cache of Vector class.
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(// FIRST means "label are stored at first coordinate of vector"
Vectorizer.LabelCoordinate.FIRST);
// Splits dataset to train and test samples with 80/20 proportion.
TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.8);
System.out.println(">>> Start training.");
LinearRegressionModel mdl = trainer.fit(ignite, dataCache, split.getTrainFilter(), vectorizer);
System.out.println(">>> Perform scoring.");
double score = Evaluator.evaluate(dataCache, split.getTestFilter(), mdl, vectorizer, MetricName.R2);
System.out.println(">>> Model: " + toString(mdl));
System.out.println(">>> R^2 score: " + score);
} finally {
if (dataCache != null)
dataCache.destroy();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.regressions.linear.LinearRegressionModel in project ignite by apache.
the class TrainTestDatasetSplitterExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws IOException {
System.out.println();
System.out.println(">>> Linear regression model over cache based dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.MORTALITY_DATA);
System.out.println(">>> Create new linear regression trainer object.");
LinearRegressionLSQRTrainer trainer = new LinearRegressionLSQRTrainer();
System.out.println(">>> Create new training dataset splitter object.");
TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.75);
System.out.println(">>> Perform the training to get the model.");
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
LinearRegressionModel mdl = trainer.fit(ignite, dataCache, split.getTrainFilter(), vectorizer);
System.out.println(">>> Linear regression model: " + mdl);
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Ground Truth\t|");
System.out.println(">>> ---------------------------------");
ScanQuery<Integer, Vector> qry = new ScanQuery<>();
qry.setFilter(split.getTestFilter());
try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(qry)) {
for (Cache.Entry<Integer, Vector> observation : observations) {
Vector val = observation.getValue();
Vector inputs = val.copyOfRange(1, val.size());
double groundTruth = val.get(0);
double prediction = mdl.predict(inputs);
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
}
}
System.out.println(">>> ---------------------------------");
System.out.println(">>> Linear regression model over cache based dataset usage example completed.");
} finally {
if (dataCache != null)
dataCache.destroy();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.regressions.linear.LinearRegressionModel in project ignite by apache.
the class LinearRegressionLSQRTrainerExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws IOException {
System.out.println();
System.out.println(">>> Linear regression model over cache based dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.MORTALITY_DATA);
System.out.println(">>> Create new linear regression trainer object.");
LinearRegressionLSQRTrainer trainer = new LinearRegressionLSQRTrainer();
System.out.println(">>> Perform the training to get the model.");
// This object is used to extract features and vectors from upstream entities which are
// essentially tuples of the form (key, value) (in our case (Integer, Vector)).
// Key part of tuple in our example is ignored.
// Label is extracted from 0th entry of the value (which is a Vector)
// and features are all remaining vector part. Alternatively we could use
// DatasetTrainer#fit(Ignite, IgniteCache, IgniteBiFunction, IgniteBiFunction) method call
// where there is a separate lambda for extracting label from (key, value) and a separate labmda for
// extracting features.
LinearRegressionModel mdl = trainer.fit(ignite, dataCache, new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
double rmse = Evaluator.evaluate(dataCache, mdl, new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST), MetricName.RMSE);
System.out.println("\n>>> Rmse = " + rmse);
System.out.println(">>> Linear regression model over cache based dataset usage example completed.");
} finally {
if (dataCache != null)
dataCache.destroy();
}
} finally {
System.out.flush();
}
}
Aggregations