SmoothedDataHistogram.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.math4.neuralnet.twod.util;

  18. import java.util.List;
  19. import org.apache.commons.math4.neuralnet.internal.NeuralNetException;
  20. import org.apache.commons.math4.neuralnet.DistanceMeasure;
  21. import org.apache.commons.math4.neuralnet.MapRanking;
  22. import org.apache.commons.math4.neuralnet.Neuron;
  23. import org.apache.commons.math4.neuralnet.twod.NeuronSquareMesh2D;

  24. /**
  25.  * Visualization of high-dimensional data projection on a 2D-map.
  26.  * The method is described in
  27.  * <blockquote>
  28.  *  <em>Using Smoothed Data Histograms for Cluster Visualization in Self-Organizing Maps</em>
  29.  *  <br>
  30.  *  by Elias Pampalk, Andreas Rauber and Dieter Merkl.
  31.  * </blockquote>
  32.  * @since 3.6
  33.  */
  34. public class SmoothedDataHistogram implements MapDataVisualization {
  35.     /** Smoothing parameter. */
  36.     private final int smoothingBins;
  37.     /** Distance. */
  38.     private final DistanceMeasure distance;
  39.     /** Normalization factor. */
  40.     private final double membershipNormalization;

  41.     /**
  42.      * @param smoothingBins Number of bins.
  43.      * @param distance Distance.
  44.      */
  45.     public SmoothedDataHistogram(int smoothingBins,
  46.                                  DistanceMeasure distance) {
  47.         this.smoothingBins = smoothingBins;
  48.         this.distance = distance;

  49.         double sum = 0;
  50.         for (int i = 0; i < smoothingBins; i++) {
  51.             sum += smoothingBins - i;
  52.         }

  53.         this.membershipNormalization = 1d / sum;
  54.     }

  55.     /**
  56.      * {@inheritDoc}
  57.      *
  58.      * @throws IllegalArgumentException if the size of the {@code map} is
  59.      * smaller than the number of {@link #SmoothedDataHistogram(int,DistanceMeasure)
  60.      * smoothing bins}.
  61.      */
  62.     @Override
  63.     public double[][] computeImage(NeuronSquareMesh2D map,
  64.                                    Iterable<double[]> data) {
  65.         final int nR = map.getNumberOfRows();
  66.         final int nC = map.getNumberOfColumns();

  67.         final int mapSize = nR * nC;
  68.         if (mapSize < smoothingBins) {
  69.             throw new NeuralNetException(NeuralNetException.TOO_SMALL,
  70.                                          mapSize, smoothingBins);
  71.         }

  72.         final LocationFinder finder = new LocationFinder(map);
  73.         final MapRanking rank = new MapRanking(map.getNetwork(), distance);

  74.         // Histogram bins.
  75.         final double[][] histo = new double[nR][nC];

  76.         for (final double[] sample : data) {
  77.             final List<Neuron> sorted = rank.rank(sample);
  78.             for (int i = 0; i < smoothingBins; i++) {
  79.                 final LocationFinder.Location loc = finder.getLocation(sorted.get(i));
  80.                 final int row = loc.getRow();
  81.                 final int col = loc.getColumn();
  82.                 histo[row][col] += (smoothingBins - i) * membershipNormalization;
  83.             }
  84.         }

  85.         return histo;
  86.     }
  87. }