Search in sources :

Example 1 with InsufficientDataException

use of org.apache.ignite.ml.math.exceptions.InsufficientDataException 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)

Aggregations

CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)1 InsufficientDataException (org.apache.ignite.ml.math.exceptions.InsufficientDataException)1 NullArgumentException (org.apache.ignite.ml.math.exceptions.NullArgumentException)1