Search in sources :

Example 1 with ManhattanDistance

use of org.apache.ignite.ml.math.distances.ManhattanDistance in project ignite by apache.

the class KNNRegressionExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) throws InterruptedException {
    System.out.println(">>> kNN regression example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), KNNRegressionExample.class.getSimpleName(), () -> {
            try {
                // Prepare path to read
                File file = IgniteUtils.resolveIgnitePath(KNN_CLEARED_MACHINES_TXT);
                if (file == null)
                    throw new RuntimeException("Can't find file: " + KNN_CLEARED_MACHINES_TXT);
                Path path = file.toPath();
                // Read dataset from file
                LabeledDataset dataset = LabeledDatasetLoader.loadFromTxtFile(path, SEPARATOR, false, false);
                // Normalize dataset
                Normalizer.normalizeWithMiniMax(dataset);
                // Random splitting of iris data as 80% train and 20% test datasets
                LabeledDatasetTestTrainPair split = new LabeledDatasetTestTrainPair(dataset, 0.2);
                System.out.println("\n>>> Amount of observations in train dataset: " + split.train().rowSize());
                System.out.println("\n>>> Amount of observations in test dataset: " + split.test().rowSize());
                LabeledDataset test = split.test();
                LabeledDataset train = split.train();
                // Builds weighted kNN-regression with Manhattan Distance
                KNNMultipleLinearRegression knnMdl = new KNNMultipleLinearRegression(7, new ManhattanDistance(), KNNStrategy.WEIGHTED, train);
                // Clone labels
                final double[] labels = test.labels();
                // Save predicted classes to test dataset
                LabellingMachine.assignLabels(test, knnMdl);
                // Calculate mean squared error (MSE)
                double mse = 0.0;
                for (int i = 0; i < test.rowSize(); i++) mse += Math.pow(test.label(i) - labels[i], 2.0);
                mse = mse / test.rowSize();
                System.out.println("\n>>> Mean squared error (MSE) " + mse);
                // Calculate mean absolute error (MAE)
                double mae = 0.0;
                for (int i = 0; i < test.rowSize(); i++) mae += Math.abs(test.label(i) - labels[i]);
                mae = mae / test.rowSize();
                System.out.println("\n>>> Mean absolute error (MAE) " + mae);
                // Calculate R^2 as 1 - RSS/TSS
                double avg = 0.0;
                for (int i = 0; i < test.rowSize(); i++) avg += test.label(i);
                avg = avg / test.rowSize();
                double detCf = 0.0;
                double tss = 0.0;
                for (int i = 0; i < test.rowSize(); i++) {
                    detCf += Math.pow(test.label(i) - labels[i], 2.0);
                    tss += Math.pow(test.label(i) - avg, 2.0);
                }
                detCf = 1 - detCf / tss;
                System.out.println("\n>>> R^2 " + detCf);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("\n>>> Unexpected exception, check resources: " + e);
            } finally {
                System.out.println("\n>>> kNN regression example completed.");
            }
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : Path(java.nio.file.Path) KNNMultipleLinearRegression(org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression) IOException(java.io.IOException) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) LabeledDatasetTestTrainPair(org.apache.ignite.ml.structures.LabeledDatasetTestTrainPair) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) File(java.io.File) ManhattanDistance(org.apache.ignite.ml.math.distances.ManhattanDistance)

Example 2 with ManhattanDistance

use of org.apache.ignite.ml.math.distances.ManhattanDistance 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;
}
Also used : KNNMultipleLinearRegression(org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression) LabeledDatasetTestTrainPair(org.apache.ignite.ml.structures.LabeledDatasetTestTrainPair) KNNModel(org.apache.ignite.ml.knn.models.KNNModel) IgniteThread(org.apache.ignite.thread.IgniteThread) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) ManhattanDistance(org.apache.ignite.ml.math.distances.ManhattanDistance)

Aggregations

KNNMultipleLinearRegression (org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression)2 ManhattanDistance (org.apache.ignite.ml.math.distances.ManhattanDistance)2 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)2 LabeledDatasetTestTrainPair (org.apache.ignite.ml.structures.LabeledDatasetTestTrainPair)2 IgniteThread (org.apache.ignite.thread.IgniteThread)2 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Ignite (org.apache.ignite.Ignite)1 KNNModel (org.apache.ignite.ml.knn.models.KNNModel)1