|
Java example source code file (MapUtils.java)
The MapUtils.java Java example source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.ml.neuralnet; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Comparator; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.ml.distance.DistanceMeasure; import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D; import org.apache.commons.math3.util.Pair; /** * Utilities for network maps. * * @since 3.3 */ public class MapUtils { /** * Class contains only static methods. */ private MapUtils() {} /** * Finds the neuron that best matches the given features. * * @param features Data. * @param neurons List of neurons to scan. If the list is empty * {@code null} will be returned. * @param distance Distance function. The neuron's features are * passed as the first argument to {@link DistanceMeasure#compute(double[],double[])}. * @return the neuron whose features are closest to the given data. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the size of the input is not compatible with the neurons features * size. */ public static Neuron findBest(double[] features, Iterable<Neuron> neurons, DistanceMeasure distance) { Neuron best = null; double min = Double.POSITIVE_INFINITY; for (final Neuron n : neurons) { final double d = distance.compute(n.getFeatures(), features); if (d < min) { min = d; best = n; } } return best; } /** * Finds the two neurons that best match the given features. * * @param features Data. * @param neurons List of neurons to scan. If the list is empty * {@code null} will be returned. * @param distance Distance function. The neuron's features are * passed as the first argument to {@link DistanceMeasure#compute(double[],double[])}. * @return the two neurons whose features are closest to the given data. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the size of the input is not compatible with the neurons features * size. */ public static Pair<Neuron, Neuron> findBestAndSecondBest(double[] features, Iterable<Neuron> neurons, DistanceMeasure distance) { Neuron[] best = { null, null }; double[] min = { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }; for (final Neuron n : neurons) { final double d = distance.compute(n.getFeatures(), features); if (d < min[0]) { // Replace second best with old best. min[1] = min[0]; best[1] = best[0]; // Store current as new best. min[0] = d; best[0] = n; } else if (d < min[1]) { // Replace old second best with current. min[1] = d; best[1] = n; } } return new Pair<Neuron, Neuron>(best[0], best[1]); } /** * Creates a list of neurons sorted in increased order of the distance * to the given {@code features}. * * @param features Data. * @param neurons List of neurons to scan. If it is empty, an empty array * will be returned. * @param distance Distance function. * @return the neurons, sorted in increasing order of distance in data * space. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the size of the input is not compatible with the neurons features * size. * * @see #findBest(double[],Iterable,DistanceMeasure) * @see #findBestAndSecondBest(double[],Iterable,DistanceMeasure) * * @since 3.6 */ public static Neuron[] sort(double[] features, Iterable<Neuron> neurons, DistanceMeasure distance) { final List<PairNeuronDouble> list = new ArrayList Other Java examples (source code examples)Here is a short list of links related to this Java MapUtils.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.