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;
}
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);
}
Aggregations