use of org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix in project ignite by apache.
the class ColumnDecisionTreeTrainerTest method testByGen.
/**
*/
private <D extends ContinuousRegionInfo> void testByGen(int totalPts, HashMap<Integer, Integer> catsInfo, SplitDataGenerator<DenseLocalOnHeapVector> gen, IgniteFunction<ColumnDecisionTreeTrainerInput, ? extends ContinuousSplitCalculator<D>> calc, IgniteFunction<ColumnDecisionTreeTrainerInput, IgniteFunction<DoubleStream, Double>> catImpCalc, IgniteFunction<DoubleStream, Double> regCalc, Random rnd) {
List<IgniteBiTuple<Integer, DenseLocalOnHeapVector>> lst = gen.points(totalPts, (i, rn) -> i).collect(Collectors.toList());
int featCnt = gen.featuresCnt();
Collections.shuffle(lst, rnd);
SparseDistributedMatrix m = new SparseDistributedMatrix(totalPts, featCnt + 1, StorageConstants.COLUMN_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
Map<Integer, List<LabeledVectorDouble>> byRegion = new HashMap<>();
int i = 0;
for (IgniteBiTuple<Integer, DenseLocalOnHeapVector> bt : lst) {
byRegion.putIfAbsent(bt.get1(), new LinkedList<>());
byRegion.get(bt.get1()).add(asLabeledVector(bt.get2().getStorage().data()));
m.setRow(i, bt.get2().getStorage().data());
i++;
}
ColumnDecisionTreeTrainer<D> trainer = new ColumnDecisionTreeTrainer<>(3, calc, catImpCalc, regCalc, ignite);
DecisionTreeModel mdl = trainer.train(new MatrixColumnDecisionTreeTrainerInput(m, catsInfo));
byRegion.keySet().forEach(k -> {
LabeledVectorDouble sp = byRegion.get(k).get(0);
Tracer.showAscii(sp.features());
X.println("Actual and predicted vectors [act=" + sp.label() + " " + ", pred=" + mdl.apply(sp.features()) + "]");
assert mdl.apply(sp.features()) == sp.doubleLabel();
});
}
use of org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix in project ignite by apache.
the class DistributedLinearRegressionWithQRTrainerExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws InterruptedException {
System.out.println();
System.out.println(">>> Linear regression model over sparse distributed matrix API usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
// Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
// because we create ignite cache internally.
IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SparseDistributedMatrixExample.class.getSimpleName(), () -> {
// Create SparseDistributedMatrix, new cache will be created automagically.
System.out.println(">>> Create new SparseDistributedMatrix inside IgniteThread.");
SparseDistributedMatrix distributedMatrix = new SparseDistributedMatrix(data);
System.out.println(">>> Create new linear regression trainer object.");
Trainer<LinearRegressionModel, Matrix> trainer = new LinearRegressionQRTrainer();
System.out.println(">>> Perform the training to get the model.");
LinearRegressionModel model = trainer.train(distributedMatrix);
System.out.println(">>> Linear regression model: " + model);
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Ground Truth\t|");
System.out.println(">>> ---------------------------------");
for (double[] observation : data) {
Vector inputs = new SparseDistributedVector(Arrays.copyOfRange(observation, 1, observation.length));
double prediction = model.apply(inputs);
double groundTruth = observation[0];
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
}
System.out.println(">>> ---------------------------------");
});
igniteThread.start();
igniteThread.join();
}
}
use of org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix in project ignite by apache.
the class FuzzyCMeansDistributedClusterer method calculateNewCenters.
/**
* Calculate new centers according to membership matrix.
*
* @param points Matrix with source points.
* @param membershipsAndSums Membership matrix and sums of membership coefficient for each center.
* @param k The number of centers.
* @return Array of new centers.
*/
private Vector[] calculateNewCenters(SparseDistributedMatrix points, MembershipsAndSums membershipsAndSums, int k) {
String cacheName = ((SparseDistributedMatrixStorage) points.getStorage()).cacheName();
UUID uuid = points.getUUID();
CentersArraySupplier supplier = new CentersArraySupplier(k, points.columnSize());
Vector[] centers = CacheUtils.distributedFold(cacheName, (IgniteBiFunction<Cache.Entry<SparseMatrixKey, ConcurrentHashMap<Integer, Double>>, Vector[], Vector[]>) (vectorWithIndex, centerSums) -> {
Integer idx = vectorWithIndex.getKey().index();
Vector pnt = MatrixUtil.localCopyOf(VectorUtils.fromMap(vectorWithIndex.getValue(), false));
Vector pntMemberships = membershipsAndSums.memberships.get(idx);
for (int i = 0; i < k; i++) {
Vector weightedPnt = pnt.times(pntMemberships.getX(i));
centerSums[i] = centerSums[i].plus(weightedPnt);
}
return centerSums;
}, key -> key.dataStructureId().equals(uuid), (sums1, sums2) -> {
for (int i = 0; i < k; i++) sums1[i] = sums1[i].plus(sums2[i]);
return sums1;
}, supplier);
for (int i = 0; i < k; i++) centers[i] = centers[i].divide(membershipsAndSums.membershipSums.getX(i));
return centers;
}
use of org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix in project ignite by apache.
the class FuzzyCMeansDistributedClusterer method calculateMembership.
/**
* Calculate matrix of membership coefficients for each point and each center.
*
* @param points Matrix with source points.
* @param centers Array of current centers.
* @return Membership matrix and sums of membership coefficients for each center.
*/
private MembershipsAndSums calculateMembership(SparseDistributedMatrix points, Vector[] centers) {
String cacheName = ((SparseDistributedMatrixStorage) points.getStorage()).cacheName();
UUID uuid = points.getUUID();
double fuzzyMembershipCoefficient = 2 / (exponentialWeight - 1);
MembershipsAndSumsSupplier supplier = new MembershipsAndSumsSupplier(centers.length);
return CacheUtils.distributedFold(cacheName, (IgniteBiFunction<Cache.Entry<SparseMatrixKey, ConcurrentHashMap<Integer, Double>>, MembershipsAndSums, MembershipsAndSums>) (vectorWithIndex, membershipsAndSums) -> {
Integer idx = vectorWithIndex.getKey().index();
Vector pnt = VectorUtils.fromMap(vectorWithIndex.getValue(), false);
Vector distances = new DenseLocalOnHeapVector(centers.length);
Vector pntMemberships = new DenseLocalOnHeapVector(centers.length);
for (int i = 0; i < centers.length; i++) distances.setX(i, distance(centers[i], pnt));
for (int i = 0; i < centers.length; i++) {
double invertedFuzzyWeight = 0.0;
for (int j = 0; j < centers.length; j++) {
double val = Math.pow(distances.getX(i) / distances.getX(j), fuzzyMembershipCoefficient);
if (Double.isNaN(val))
val = 1.0;
invertedFuzzyWeight += val;
}
double membership = Math.pow(1.0 / invertedFuzzyWeight, exponentialWeight);
pntMemberships.setX(i, membership);
}
membershipsAndSums.memberships.put(idx, pntMemberships);
membershipsAndSums.membershipSums = membershipsAndSums.membershipSums.plus(pntMemberships);
return membershipsAndSums;
}, key -> key.dataStructureId().equals(uuid), (mem1, mem2) -> {
mem1.merge(mem2);
return mem1;
}, supplier);
}
use of org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix in project ignite by apache.
the class KMeansDistributedClustererTestSingleNode method testPerformClusterAnalysisDegenerate.
/**
*/
public void testPerformClusterAnalysisDegenerate() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(new EuclideanDistance(), 1, 1, 1L);
double[] v1 = new double[] { 1959, 325100 };
double[] v2 = new double[] { 1960, 373200 };
SparseDistributedMatrix points = new SparseDistributedMatrix(2, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
points.setRow(0, v1);
points.setRow(1, v2);
KMeansModel mdl = clusterer.cluster(points, 1);
Assert.assertEquals(1, mdl.centers().length);
Assert.assertEquals(2, mdl.centers()[0].size());
}
Aggregations