use of org.apache.ignite.ml.math.exceptions.NullArgumentException in project ignite by apache.
the class AbstractMultipleLinearRegression method newXSampleData.
/**
* <p>Loads new x sample data, overriding any previous data.
* </p>
* The input <code>x</code> array should have one row for each sample
* observation, with columns corresponding to independent variables.
* For example, if <pre>
* <code> x = new double[][] {{1, 2}, {3, 4}, {5, 6}} </code></pre>
* then <code>setXSampleData(x) </code> results in a model with two independent
* variables and 3 observations:
* <pre>
* x[0] x[1]
* ----------
* 1 2
* 3 4
* 5 6
* </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.
* </p>
*
* @param x the rectangular array representing the x sample
* @throws NullArgumentException if x is null
* @throws NoDataException if x is empty
* @throws CardinalityException if x is not rectangular
*/
protected void newXSampleData(Matrix x) {
if (x == null)
throw new NullArgumentException();
if (x.rowSize() == 0)
throw new NoDataException();
if (noIntercept)
// TODO: Should we copy here?
xMatrix = x;
else {
// Augment design matrix with initial unitary column
xMatrix = MatrixUtil.like(x, x.rowSize(), x.columnSize() + 1);
xMatrix.viewColumn(0).map(Functions.constant(1.0));
xMatrix.viewPart(0, x.rowSize(), 1, x.columnSize()).assign(x);
}
}
use of org.apache.ignite.ml.math.exceptions.NullArgumentException 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);
}
Aggregations