use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class VectorCustomStorageExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
System.out.println();
System.out.println(">>> Vector custom storage API usage example started.");
System.out.println("\n>>> Creating perpendicular vectors.");
double[] data1 = new double[] { 1, 0, 3, 0, 5, 0 };
double[] data2 = new double[] { 0, 2, 0, 4, 0, 6 };
Vector v1 = new VectorCustomStorage(data1);
Vector v2 = new VectorCustomStorage(data2);
System.out.println(">>> First vector: " + Arrays.toString(data1));
System.out.println(">>> Second vector: " + Arrays.toString(data2));
double dotProduct = v1.dot(v2);
boolean dotProductIsAsExp = dotProduct == 0;
System.out.println("\n>>> Dot product of vectors: [" + dotProduct + "], it is 0 as expected: [" + dotProductIsAsExp + "].");
Vector hypotenuse = v1.plus(v2);
System.out.println("\n>>> Hypotenuse (sum of vectors): " + Arrays.toString(hypotenuse.getStorage().data()));
double lenSquared1 = v1.getLengthSquared();
double lenSquared2 = v2.getLengthSquared();
double lenSquaredHypotenuse = hypotenuse.getLengthSquared();
boolean lenSquaredHypotenuseIsAsExp = lenSquaredHypotenuse == lenSquared1 + lenSquared2;
System.out.println(">>> Squared length of first vector: [" + lenSquared1 + "].");
System.out.println(">>> Squared length of second vector: [" + lenSquared2 + "].");
System.out.println(">>> Squared length of hypotenuse: [" + lenSquaredHypotenuse + "], equals sum of squared lengths of two original vectors as expected: [" + lenSquaredHypotenuseIsAsExp + "].");
System.out.println("\n>>> Vector custom storage API usage example completed.");
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class KMeansDistributedClustererExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) throws InterruptedException {
// IMPL NOTE based on KMeansDistributedClustererTestSingleNode#testClusterizationOnDatasetWithObviousStructure
System.out.println(">>> K-means distributed clusterer 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(), () -> {
int ptsCnt = 10000;
SparseDistributedMatrix points = new SparseDistributedMatrix(ptsCnt, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
DatasetWithObviousStructure dataset = new DatasetWithObviousStructure(10000);
List<Vector> massCenters = dataset.generate(points);
EuclideanDistance dist = new EuclideanDistance();
KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(dist, 3, 100, 1L);
Vector[] resCenters = clusterer.cluster(points, 4).centers();
System.out.println("Mass centers:");
massCenters.forEach(Tracer::showAscii);
System.out.println("Cluster centers:");
Arrays.asList(resCenters).forEach(Tracer::showAscii);
points.destroy();
System.out.println("\n>>> K-means distributed clusterer example completed.");
});
igniteThread.start();
igniteThread.join();
}
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class SparseVectorExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
System.out.println();
System.out.println(">>> Sparse vector API usage example started.");
System.out.println("\n>>> Creating perpendicular sparse vectors.");
double[] data1 = new double[] { 1, 0, 3, 0, 5, 0 };
double[] data2 = new double[] { 0, 2, 0, 4, 0, 6 };
Vector v1 = new SparseLocalVector(data1.length, RANDOM_ACCESS_MODE);
Vector v2 = new SparseLocalVector(data2.length, RANDOM_ACCESS_MODE);
v1.assign(data1);
v2.assign(data2);
System.out.println(">>> First vector: " + Arrays.toString(data1));
System.out.println(">>> Second vector: " + Arrays.toString(data2));
double dotProduct = v1.dot(v2);
boolean dotProductIsAsExp = dotProduct == 0;
System.out.println("\n>>> Dot product of vectors: [" + dotProduct + "], it is 0 as expected: [" + dotProductIsAsExp + "].");
Vector hypotenuse = v1.plus(v2);
System.out.println("\n>>> Hypotenuse (sum of vectors): " + Arrays.toString(hypotenuse.getStorage().data()));
double lenSquared1 = v1.getLengthSquared();
double lenSquared2 = v2.getLengthSquared();
double lenSquaredHypotenuse = hypotenuse.getLengthSquared();
boolean lenSquaredHypotenuseIsAsExp = lenSquaredHypotenuse == lenSquared1 + lenSquared2;
System.out.println(">>> Squared length of first vector: [" + lenSquared1 + "].");
System.out.println(">>> Squared length of second vector: [" + lenSquared2 + "].");
System.out.println(">>> Squared length of hypotenuse: [" + lenSquaredHypotenuse + "], equals sum of squared lengths of two original vectors as expected: [" + lenSquaredHypotenuseIsAsExp + "].");
System.out.println("\n>>> Sparse vector API usage example completed.");
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class DatasetWithObviousStructure method generate.
/**
*/
List<Vector> generate(Matrix points) {
int ptsCnt = points.rowSize();
// Mass centers of dataset.
List<Vector> massCenters = new ArrayList<>();
int centersCnt = centers.size();
List<Integer> permutation = IntStream.range(0, ptsCnt).boxed().collect(Collectors.toList());
Collections.shuffle(permutation, rnd);
Vector[] mc = new Vector[centersCnt];
Arrays.fill(mc, VectorUtils.zeroes(2));
int totalCnt = 0;
int centIdx = 0;
massCenters.clear();
for (Integer count : centers.keySet()) {
for (int i = 0; i < count; i++) {
Vector pnt = getPoint(count);
mc[centIdx] = mc[centIdx].plus(pnt);
points.assignRow(permutation.get(totalCnt), pnt);
totalCnt++;
}
massCenters.add(mc[centIdx].times(1 / (double) count));
centIdx++;
}
return massCenters;
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class DatasetWithObviousStructure method getPoint.
/**
*/
private Vector getPoint(Integer cnt) {
Vector pnt = new DenseLocalOnHeapVector(2).assign(centers.get(cnt));
// Perturbate point on random value.
pnt.map(val -> val + rnd.nextDouble() * squareSideLen / 100);
return pnt;
}
Aggregations