001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.ml.neuralnet.twod.util;
019
020import org.apache.commons.math3.ml.neuralnet.MapUtils;
021import org.apache.commons.math3.ml.neuralnet.Neuron;
022import org.apache.commons.math3.ml.neuralnet.Network;
023import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D;
024import org.apache.commons.math3.ml.distance.DistanceMeasure;
025import org.apache.commons.math3.util.Pair;
026
027/**
028 * Computes the topographic error histogram.
029 * Each bin will contain the number of data for which the first and
030 * second best matching units are not adjacent in the map.
031 * @since 3.6
032 */
033public class TopographicErrorHistogram implements MapDataVisualization {
034    /** Distance. */
035    private final DistanceMeasure distance;
036    /** Whether to compute relative bin counts. */
037    private final boolean relativeCount;
038
039    /**
040     * @param relativeCount Whether to compute relative bin counts.
041     * If {@code true}, the data count in each bin will be divided by the total
042     * number of samples mapped to the neuron represented by that bin.
043     * @param distance Distance.
044     */
045    public TopographicErrorHistogram(boolean relativeCount,
046                                     DistanceMeasure distance) {
047        this.relativeCount = relativeCount;
048        this.distance = distance;
049    }
050
051    /** {@inheritDoc} */
052    public double[][] computeImage(NeuronSquareMesh2D map,
053                                   Iterable<double[]> data) {
054        final int nR = map.getNumberOfRows();
055        final int nC = map.getNumberOfColumns();
056
057        final Network net = map.getNetwork();
058        final LocationFinder finder = new LocationFinder(map);
059
060        // Hit bins.
061        final int[][] hit = new int[nR][nC];
062        // Error bins.
063        final double[][] error = new double[nR][nC];
064
065        for (double[] sample : data) {
066            final Pair<Neuron, Neuron> p = MapUtils.findBestAndSecondBest(sample, map, distance);
067            final Neuron best = p.getFirst();
068
069            final LocationFinder.Location loc = finder.getLocation(best);
070            final int row = loc.getRow();
071            final int col = loc.getColumn();
072            hit[row][col] += 1;
073
074            if (!net.getNeighbours(best).contains(p.getSecond())) {
075                // Increment count if first and second best matching units
076                // are not neighbours.
077                error[row][col] += 1;
078            }
079        }
080
081        if (relativeCount) {
082            for (int r = 0; r < nR; r++) {
083                for (int c = 0; c < nC; c++) {
084                    error[r][c] /= hit[r][c];
085                }
086            }
087        }
088
089        return error;
090    }
091}