Search in sources :

Example 6 with MathIllegalArgumentException

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

the class SplitDataGenerator method split.

/**
 * Split region by categorical coordinate.
 *
 * @param regIdx Region index.
 * @param coordIdx Coordinate index.
 * @param cats Categories allowed for the left sub region.
 * @return {@code this}.
 */
public SplitDataGenerator<V> split(int regIdx, int coordIdx, int[] cats) {
    BitSet subset = new BitSet();
    Arrays.stream(cats).forEach(subset::set);
    Region regToSplit = regs.get(regIdx);
    CatCoordInfo cci = regToSplit.catCoords.get(coordIdx);
    BitSet ssc = (BitSet) subset.clone();
    BitSet set = cci.bs;
    ssc.and(set);
    if (ssc.length() != subset.length())
        throw new MathIllegalArgumentException("Splitter set is not a subset of a parent subset.");
    ssc.xor(set);
    set.and(subset);
    regToSplit.incTwoPow();
    Region newReg = Utils.copy(regToSplit);
    newReg.catCoords.put(coordIdx, new CatCoordInfo(ssc));
    regs.add(regIdx + 1, newReg);
    return this;
}
Also used : MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) BitSet(java.util.BitSet)

Example 7 with MathIllegalArgumentException

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

the class FuzzyCMeansLocalClusterer method cluster.

/**
 * {@inheritDoc}
 */
@Override
public FuzzyCMeansModel cluster(DenseLocalOnHeapMatrix points, int k, List<Double> weights) throws MathIllegalArgumentException, ConvergenceException {
    GridArgumentCheck.notNull(points, "points");
    GridArgumentCheck.notNull(weights, "weights");
    if (points.rowSize() != weights.size())
        throw new MathIllegalArgumentException("The number of points and the number of weights are not equal");
    if (k < 2)
        throw new MathIllegalArgumentException("The number of clusters is less than 2");
    Matrix centers = new DenseLocalOnHeapMatrix(k, points.columnSize());
    Matrix distances = new DenseLocalOnHeapMatrix(k, points.rowSize());
    Matrix membership = new DenseLocalOnHeapMatrix(k, points.rowSize());
    Vector weightsVector = new DenseLocalOnHeapVector(weights.size());
    for (int i = 0; i < weights.size(); i++) weightsVector.setX(i, weights.get(i));
    initializeCenters(centers, points, k, weightsVector);
    int iteration = 0;
    boolean finished = false;
    while (iteration < maxIterations && !finished) {
        calculateDistances(distances, points, centers);
        Matrix newMembership = calculateMembership(distances, weightsVector);
        Matrix newCenters = calculateNewCenters(points, newMembership);
        if (this.stopCond == StopCondition.STABLE_CENTERS)
            finished = areCentersStable(centers, newCenters);
        else
            finished = areMembershipStable(membership, newMembership);
        centers = newCenters;
        membership = newMembership;
        iteration++;
    }
    if (iteration == maxIterations)
        throw new ConvergenceException("Fuzzy C-Means algorithm has not converged after " + Integer.toString(iteration) + " iterations");
    Vector[] centersArr = new Vector[k];
    for (int i = 0; i < k; i++) centersArr[i] = centers.getRow(i);
    return new FuzzyCMeansModel(centersArr, measure);
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) ConvergenceException(org.apache.ignite.ml.math.exceptions.ConvergenceException) 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)

Aggregations

MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)7 BitSet (java.util.BitSet)2 Vector (org.apache.ignite.ml.math.Vector)2 ConvergenceException (org.apache.ignite.ml.math.exceptions.ConvergenceException)2 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)2 Matrix (org.apache.ignite.ml.math.Matrix)1 MathArithmeticException (org.apache.ignite.ml.math.exceptions.MathArithmeticException)1 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)1 LabeledVectorDouble (org.apache.ignite.ml.structures.LabeledVectorDouble)1