Search in sources :

Example 6 with CardinalityException

use of org.apache.ignite.ml.math.exceptions.CardinalityException in project ignite by apache.

the class AbstractMultipleLinearRegression method newSampleData.

/**
     * <p>Loads model x and y sample data from a flat input array, overriding any previous sample.
     * </p>
     * <p>Assumes that rows are concatenated with y values first in each row.  For example, an input
     * <code>data</code> array containing the sequence of values (1, 2, 3, 4, 5, 6, 7, 8, 9) with
     * <code>nobs = 3</code> and <code>nvars = 2</code> creates a regression dataset with two
     * independent variables, as below:
     * <pre>
     *   y   x[0]  x[1]
     *   --------------
     *   1     2     3
     *   4     5     6
     *   7     8     9
     * </pre>
     * </p>
     * <p>Note that there is no need to add an initial unitary column (column of 1's) when
     * specifying a model including an intercept term.  If {@link #isNoIntercept()} is <code>true</code>,
     * the X matrix will be created without an initial column of "1"s; otherwise this column will
     * be added.
     * </p>
     * <p>Throws IllegalArgumentException if any of the following preconditions fail:
     * <ul><li><code>data</code> cannot be null</li>
     * <li><code>data.length = nobs * (nvars + 1)</li>
     * <li><code>nobs > nvars</code></li></ul>
     * </p>
     *
     * @param data input data array
     * @param nobs number of observations (rows)
     * @param nvars number of independent variables (columns, not counting y)
     * @param like matrix(maybe empty) indicating how data should be stored
     * @throws NullArgumentException if the data array is null
     * @throws CardinalityException if the length of the data array is not equal to <code>nobs * (nvars + 1)</code>
     * @throws InsufficientDataException if <code>nobs</code> is less than <code>nvars + 1</code>
     */
public void newSampleData(double[] data, int nobs, int nvars, Matrix like) {
    if (data == null)
        throw new NullArgumentException();
    if (data.length != nobs * (nvars + 1))
        throw new CardinalityException(nobs * (nvars + 1), data.length);
    if (nobs <= nvars)
        throw new InsufficientDataException(RegressionsErrorMessages.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE);
    double[] y = new double[nobs];
    final int cols = noIntercept ? nvars : nvars + 1;
    double[][] x = new double[nobs][cols];
    int pointer = 0;
    for (int i = 0; i < nobs; i++) {
        y[i] = data[pointer++];
        if (!noIntercept)
            x[i][0] = 1.0d;
        for (int j = noIntercept ? 0 : 1; j < cols; j++) x[i][j] = data[pointer++];
    }
    xMatrix = MatrixUtil.like(like, nobs, cols).assign(x);
    yVector = MatrixUtil.likeVector(like, y.length).assign(y);
}
Also used : InsufficientDataException(org.apache.ignite.ml.math.exceptions.InsufficientDataException) NullArgumentException(org.apache.ignite.ml.math.exceptions.NullArgumentException) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 7 with CardinalityException

use of org.apache.ignite.ml.math.exceptions.CardinalityException in project ignite by apache.

the class LUDecomposition method solve.

/**
     * @param b Vector to solve using this decomposition.
     * @return Solution vector.
     */
public Vector solve(Vector b) {
    final int m = pivot.size();
    if (b.size() != m)
        throw new CardinalityException(b.size(), m);
    if (singular)
        throw new SingularMatrixException();
    final double[] bp = new double[m];
    // Apply permutations to b
    for (int row = 0; row < m; row++) bp[row] = b.get((int) pivot.get(row));
    // Solve LY = b
    for (int col = 0; col < m; col++) {
        final double bpCol = bp[col];
        for (int i = col + 1; i < m; i++) bp[i] -= bpCol * lu.get(i, col);
    }
    // Solve UX = Y
    for (int col = m - 1; col >= 0; col--) {
        bp[col] /= lu.get(col, col);
        final double bpCol = bp[col];
        for (int i = 0; i < col; i++) bp[i] -= bpCol * lu.get(i, col);
    }
    return b.like(m).assign(bp);
}
Also used : SingularMatrixException(org.apache.ignite.ml.math.exceptions.SingularMatrixException) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 8 with CardinalityException

use of org.apache.ignite.ml.math.exceptions.CardinalityException in project ignite by apache.

the class AbstractMatrix method setColumn.

/** {@inheritDoc} */
@Override
public Matrix setColumn(int col, double[] data) {
    checkColumnIndex(col);
    int rows = rowSize();
    if (rows != data.length)
        throw new CardinalityException(rows, data.length);
    for (int x = 0; x < rows; x++) setX(x, col, data[x]);
    return this;
}
Also used : CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 9 with CardinalityException

use of org.apache.ignite.ml.math.exceptions.CardinalityException in project ignite by apache.

the class AbstractMatrix method times.

/** {@inheritDoc} */
@Override
public Vector times(Vector vec) {
    int cols = columnSize();
    if (cols != vec.size())
        throw new CardinalityException(cols, vec.size());
    int rows = rowSize();
    Vector res = likeVector(rows);
    for (int x = 0; x < rows; x++) res.setX(x, vec.dot(viewRow(x)));
    return res;
}
Also used : CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException) Vector(org.apache.ignite.ml.math.Vector)

Example 10 with CardinalityException

use of org.apache.ignite.ml.math.exceptions.CardinalityException in project ignite by apache.

the class AbstractMatrix method setRow.

/** {@inheritDoc} */
@Override
public Matrix setRow(int row, double[] data) {
    checkRowIndex(row);
    int cols = columnSize();
    if (cols != data.length)
        throw new CardinalityException(cols, data.length);
    if (sto.isArrayBased())
        System.arraycopy(data, 0, sto.data()[row], 0, cols);
    else
        for (int y = 0; y < cols; y++) setX(row, y, data[y]);
    return this;
}
Also used : CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Aggregations

CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)11 Matrix (org.apache.ignite.ml.math.Matrix)2 SingularMatrixException (org.apache.ignite.ml.math.exceptions.SingularMatrixException)2 Vector (org.apache.ignite.ml.math.Vector)1 LUDecomposition (org.apache.ignite.ml.math.decompositions.LUDecomposition)1 InsufficientDataException (org.apache.ignite.ml.math.exceptions.InsufficientDataException)1 NullArgumentException (org.apache.ignite.ml.math.exceptions.NullArgumentException)1