use of org.apache.ignite.ml.knn.models.KNNModel in project ignite by apache.
the class IgniteKNNClassificationBenchmark method test.
/**
* {@inheritDoc}
*/
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
// Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
// because we create ignite cache internally.
IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), this.getClass().getSimpleName(), new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
// IMPL NOTE originally taken from KNNClassificationExample.
// Obtain shuffled dataset.
LabeledDataset dataset = new Datasets().shuffleIris((int) (DataChanger.next()));
// Random splitting of iris data as 70% train and 30% test datasets.
LabeledDatasetTestTrainPair split = new LabeledDatasetTestTrainPair(dataset, 0.3);
LabeledDataset test = split.test();
LabeledDataset train = split.train();
KNNModel knnMdl = new KNNModel(5, new EuclideanDistance(), KNNStrategy.SIMPLE, train);
// Calculate predicted classes.
for (int i = 0; i < test.rowSize() - 1; i++) knnMdl.apply(test.getRow(i).features());
}
});
igniteThread.start();
igniteThread.join();
return true;
}
use of org.apache.ignite.ml.knn.models.KNNModel in project ignite by apache.
the class IgniteKNNRegressionBenchmark method test.
/**
* {@inheritDoc}
*/
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
// Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
// because we create ignite cache internally.
IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), this.getClass().getSimpleName(), new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
// IMPL NOTE originally taken from KNNRegressionExample.
// Obtain shuffled dataset.
LabeledDataset dataset = new Datasets().shuffleClearedMachines((int) (DataChanger.next()));
// Normalize dataset
Normalizer.normalizeWithMiniMax(dataset);
// Random splitting of iris data as 80% train and 20% test datasets.
LabeledDatasetTestTrainPair split = new LabeledDatasetTestTrainPair(dataset, 0.2);
LabeledDataset test = split.test();
LabeledDataset train = split.train();
// Builds weighted kNN-regression with Manhattan Distance.
KNNModel knnMdl = new KNNMultipleLinearRegression(7, new ManhattanDistance(), KNNStrategy.WEIGHTED, train);
// Clone labels
final double[] labels = test.labels();
// Calculate predicted classes.
for (int i = 0; i < test.rowSize() - 1; i++) knnMdl.apply(test.getRow(i).features());
}
});
igniteThread.start();
igniteThread.join();
return true;
}
use of org.apache.ignite.ml.knn.models.KNNModel in project ignite by apache.
the class KNNClassificationTest method testBinaryClassificationTest.
/**
*/
public void testBinaryClassificationTest() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
double[][] mtx = new double[][] { { 1.0, 1.0 }, { 1.0, 2.0 }, { 2.0, 1.0 }, { -1.0, -1.0 }, { -1.0, -2.0 }, { -2.0, -1.0 } };
double[] lbs = new double[] { 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 };
LabeledDataset training = new LabeledDataset(mtx, lbs);
KNNModel knnMdl = new KNNModel(3, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
Vector firstVector = new DenseLocalOnHeapVector(new double[] { 2.0, 2.0 });
assertEquals(knnMdl.apply(firstVector), 1.0);
Vector secondVector = new DenseLocalOnHeapVector(new double[] { -2.0, -2.0 });
assertEquals(knnMdl.apply(secondVector), 2.0);
}
use of org.apache.ignite.ml.knn.models.KNNModel in project ignite by apache.
the class KNNClassificationTest method testBinaryClassificationFarPointsWithWeightedStrategy.
/**
*/
public void testBinaryClassificationFarPointsWithWeightedStrategy() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
double[][] mtx = new double[][] { { 10.0, 10.0 }, { 10.0, 20.0 }, { -1, -1 }, { -2, -2 }, { -1.0, -2.0 }, { -2.0, -1.0 } };
double[] lbs = new double[] { 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 };
LabeledDataset training = new LabeledDataset(mtx, lbs);
KNNModel knnMdl = new KNNModel(3, new EuclideanDistance(), KNNStrategy.WEIGHTED, training);
Vector vector = new DenseLocalOnHeapVector(new double[] { -1.01, -1.01 });
assertEquals(knnMdl.apply(vector), 1.0);
}
use of org.apache.ignite.ml.knn.models.KNNModel in project ignite by apache.
the class KNNClassificationTest method testPredictOnIrisDataset.
/**
*/
public void testPredictOnIrisDataset() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
LabeledDataset training = loadDatasetFromTxt(KNN_IRIS_TXT, false);
KNNModel knnMdl = new KNNModel(7, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
Vector vector = new DenseLocalOnHeapVector(new double[] { 5.15, 3.55, 1.45, 0.25 });
assertEquals(knnMdl.apply(vector), 1.0);
}
Aggregations