Search in sources :

Example 1 with LSQRResult

use of org.apache.ignite.ml.math.isolve.lsqr.LSQRResult in project ignite by apache.

the class LinearRegressionLSQRTrainer method fit.

/**
 * {@inheritDoc}
 */
@Override
public LinearRegressionModel fit(DatasetBuilder<K, V> datasetBuilder, IgniteBiFunction<K, V, double[]> featureExtractor, IgniteBiFunction<K, V, Double> lbExtractor, int cols) {
    LSQRResult res;
    try (LSQROnHeap<K, V> lsqr = new LSQROnHeap<>(datasetBuilder, new LinSysPartitionDataBuilderOnHeap<>((k, v) -> {
        double[] row = Arrays.copyOf(featureExtractor.apply(k, v), cols + 1);
        row[cols] = 1.0;
        return row;
    }, lbExtractor, cols + 1))) {
        res = lsqr.solve(0, 1e-12, 1e-12, 1e8, -1, false, null);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Vector weights = new DenseLocalOnHeapVector(Arrays.copyOfRange(res.getX(), 0, cols));
    return new LinearRegressionModel(weights, res.getX()[cols]);
}
Also used : DatasetTrainer(org.apache.ignite.ml.DatasetTrainer) Arrays(java.util.Arrays) Vector(org.apache.ignite.ml.math.Vector) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) LinSysPartitionDataBuilderOnHeap(org.apache.ignite.ml.math.isolve.LinSysPartitionDataBuilderOnHeap) AbstractLSQR(org.apache.ignite.ml.math.isolve.lsqr.AbstractLSQR) LSQROnHeap(org.apache.ignite.ml.math.isolve.lsqr.LSQROnHeap) LSQRResult(org.apache.ignite.ml.math.isolve.lsqr.LSQRResult) DatasetBuilder(org.apache.ignite.ml.dataset.DatasetBuilder) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) LSQROnHeap(org.apache.ignite.ml.math.isolve.lsqr.LSQROnHeap) LSQRResult(org.apache.ignite.ml.math.isolve.lsqr.LSQRResult) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Aggregations

Arrays (java.util.Arrays)1 DatasetTrainer (org.apache.ignite.ml.DatasetTrainer)1 DatasetBuilder (org.apache.ignite.ml.dataset.DatasetBuilder)1 Vector (org.apache.ignite.ml.math.Vector)1 IgniteBiFunction (org.apache.ignite.ml.math.functions.IgniteBiFunction)1 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)1 LinSysPartitionDataBuilderOnHeap (org.apache.ignite.ml.math.isolve.LinSysPartitionDataBuilderOnHeap)1 AbstractLSQR (org.apache.ignite.ml.math.isolve.lsqr.AbstractLSQR)1 LSQROnHeap (org.apache.ignite.ml.math.isolve.lsqr.LSQROnHeap)1 LSQRResult (org.apache.ignite.ml.math.isolve.lsqr.LSQRResult)1