Search in sources :

Example 51 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class LabeledDatasetTest method testAccessMethods.

/**
 */
public void testAccessMethods() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    double[][] mtx = new double[][] { { 1.0, 1.0 }, { 1.0, 2.0 }, { 2.0, 1.0 }, { -1.0, -1.0 }, { -1.0, -2.0 }, { -2.0, -1.0 } };
    double[] lbs = new double[] { 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 };
    final LabeledDataset dataset = new LabeledDataset(mtx, lbs, null, false);
    assertEquals(dataset.colSize(), 2);
    assertEquals(dataset.rowSize(), 6);
    final LabeledVector<Vector, Double> row = (LabeledVector<Vector, Double>) dataset.getRow(0);
    assertEquals(row.features().get(0), 1.0);
    assertEquals(row.label(), 1.0);
    dataset.setLabel(0, 2.0);
    assertEquals(row.label(), 2.0);
}
Also used : LabeledVector(org.apache.ignite.ml.structures.LabeledVector) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) Vector(org.apache.ignite.ml.math.Vector)

Example 52 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class AbstractMultipleLinearRegression method estimateResiduals.

/**
     * {@inheritDoc}
     */
@Override
public double[] estimateResiduals() {
    Vector b = calculateBeta();
    Vector e = yVector.minus(xMatrix.times(b));
    return e.getStorage().data();
}
Also used : Vector(org.apache.ignite.ml.math.Vector)

Example 53 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class QRDecomposition method solve.

/**
     * Least squares solution of {@code A*X = B}; {@code returns X}.
     *
     * @param mtx A matrix with as many rows as {@code A} and any number of cols.
     * @return {@code X<} that minimizes the two norm of {@code Q*R*X - B}.
     * @throws IllegalArgumentException if {@code B.rows() != A.rows()}.
     */
public Matrix solve(Matrix mtx) {
    if (mtx.rowSize() != rows)
        throw new IllegalArgumentException("Matrix row dimensions must agree.");
    int cols = mtx.columnSize();
    Matrix r = getR();
    checkSingular(r, threshold, true);
    Matrix x = like(mType, this.cols, cols);
    Matrix qt = getQ().transpose();
    Matrix y = qt.times(mtx);
    for (int k = Math.min(this.cols, rows) - 1; k >= 0; k--) {
        // X[k,] = Y[k,] / R[k,k], note that X[k,] starts with 0 so += is same as =
        x.viewRow(k).map(y.viewRow(k), Functions.plusMult(1 / r.get(k, k)));
        if (k == 0)
            continue;
        // Y[0:(k-1),] -= R[0:(k-1),k] * X[k,]
        Vector rCol = r.viewColumn(k).viewPart(0, k);
        for (int c = 0; c < cols; c++) y.viewColumn(c).viewPart(0, k).map(rCol, Functions.plusMult(-x.get(k, c)));
    }
    return x;
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) Vector(org.apache.ignite.ml.math.Vector)

Example 54 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class AbstractMultipleLinearRegressionTest method testNewSample.

/**
     * Verifies that newSampleData methods consistently insert unitary columns
     * in design matrix.  Confirms the fix for MATH-411.
     */
@Test
public void testNewSample() {
    double[] design = new double[] { 1, 19, 22, 33, 2, 20, 30, 40, 3, 25, 35, 45, 4, 27, 37, 47 };
    double[] y = new double[] { 1, 2, 3, 4 };
    double[][] x = new double[][] { { 19, 22, 33 }, { 20, 30, 40 }, { 25, 35, 45 }, { 27, 37, 47 } };
    AbstractMultipleLinearRegression regression = createRegression();
    regression.newSampleData(design, 4, 3, new DenseLocalOnHeapMatrix());
    Matrix flatX = regression.getX().copy();
    Vector flatY = regression.getY().copy();
    regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
    regression.newYSampleData(new DenseLocalOnHeapVector(y));
    Assert.assertEquals(flatX, regression.getX());
    Assert.assertEquals(flatY, regression.getY());
    // No intercept
    regression.setNoIntercept(true);
    regression.newSampleData(design, 4, 3, new DenseLocalOnHeapMatrix());
    flatX = regression.getX().copy();
    flatY = regression.getY().copy();
    regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
    regression.newYSampleData(new DenseLocalOnHeapVector(y));
    Assert.assertEquals(flatX, regression.getX());
    Assert.assertEquals(flatY, regression.getY());
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 55 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class OLSMultipleLinearRegressionTest method testNewSample2.

/**
     * Verifies that setting X and Y separately has the same effect as newSample(X,Y).
     */
@Test
public void testNewSample2() {
    double[] y = new double[] { 1, 2, 3, 4 };
    double[][] x = new double[][] { { 19, 22, 33 }, { 20, 30, 40 }, { 25, 35, 45 }, { 27, 37, 47 } };
    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    regression.newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
    Matrix combinedX = regression.getX().copy();
    Vector combinedY = regression.getY().copy();
    regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
    regression.newYSampleData(new DenseLocalOnHeapVector(y));
    Assert.assertEquals(combinedX, regression.getX());
    Assert.assertEquals(combinedY, regression.getY());
    // No intercept
    regression.setNoIntercept(true);
    regression.newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
    combinedX = regression.getX().copy();
    combinedY = regression.getY().copy();
    regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
    regression.newYSampleData(new DenseLocalOnHeapVector(y));
    Assert.assertEquals(combinedX, regression.getX());
    Assert.assertEquals(combinedY, regression.getY());
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Aggregations

Vector (org.apache.ignite.ml.math.Vector)116 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)62 Test (org.junit.Test)29 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)20 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)20 Matrix (org.apache.ignite.ml.math.Matrix)19 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)17 Random (java.util.Random)12 ArrayList (java.util.ArrayList)11 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)10 Arrays (java.util.Arrays)9 Ignite (org.apache.ignite.Ignite)9 SparseDistributedMatrixStorage (org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage)9 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)9 UUID (java.util.UUID)8 Collections (java.util.Collections)7 List (java.util.List)7 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)7 DenseLocalOffHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector)7 HashMap (java.util.HashMap)6