1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.neuralnet.twod.util;
19
20 import java.util.List;
21 import org.apache.commons.math4.neuralnet.internal.NeuralNetException;
22 import org.apache.commons.math4.neuralnet.DistanceMeasure;
23 import org.apache.commons.math4.neuralnet.MapRanking;
24 import org.apache.commons.math4.neuralnet.Neuron;
25 import org.apache.commons.math4.neuralnet.twod.NeuronSquareMesh2D;
26
27
28
29
30
31
32
33
34
35
36
37 public class SmoothedDataHistogram implements MapDataVisualization {
38
39 private final int smoothingBins;
40
41 private final DistanceMeasure distance;
42
43 private final double membershipNormalization;
44
45
46
47
48
49 public SmoothedDataHistogram(int smoothingBins,
50 DistanceMeasure distance) {
51 this.smoothingBins = smoothingBins;
52 this.distance = distance;
53
54 double sum = 0;
55 for (int i = 0; i < smoothingBins; i++) {
56 sum += smoothingBins - i;
57 }
58
59 this.membershipNormalization = 1d / sum;
60 }
61
62
63
64
65
66
67
68
69 @Override
70 public double[][] computeImage(NeuronSquareMesh2D map,
71 Iterable<double[]> data) {
72 final int nR = map.getNumberOfRows();
73 final int nC = map.getNumberOfColumns();
74
75 final int mapSize = nR * nC;
76 if (mapSize < smoothingBins) {
77 throw new NeuralNetException(NeuralNetException.TOO_SMALL,
78 mapSize, smoothingBins);
79 }
80
81 final LocationFinder finder = new LocationFinder(map);
82 final MapRanking rank = new MapRanking(map.getNetwork(), distance);
83
84
85 final double[][] histo = new double[nR][nC];
86
87 for (final double[] sample : data) {
88 final List<Neuron> sorted = rank.rank(sample);
89 for (int i = 0; i < smoothingBins; i++) {
90 final LocationFinder.Location loc = finder.getLocation(sorted.get(i));
91 final int row = loc.getRow();
92 final int col = loc.getColumn();
93 histo[row][col] += (smoothingBins - i) * membershipNormalization;
94 }
95 }
96
97 return histo;
98 }
99 }