首页 > 解决方案 > 如何规范化列表的每个元素?

问题描述

我正在开发这个程序,但在第 57 行遇到了问题:TODO: normalize each element of reals and add it to normalized. 我得到了一个名为的文件SimpleNormalize.java,该文件是该文件 ( NormalizedIris.java) 的扩展来源。

这是文件SimpleNormalize.java

package javaai.ann.input;

import org.encog.util.arrayutil.NormalizationAction;
import org.encog.util.arrayutil.NormalizedField;

public class SimpleNormalize {
    public static void main(String[] args) {

        // Normalize values with an actual range of (0 to 100) to (-1 to 1)
        NormalizedField norm = new NormalizedField(NormalizationAction.Normalize,
                null,100,0,1,-1);

        double x = 5;
        double y = norm.normalize(x);

        System.out.println( x + " normals is " + y);

        double z = norm.deNormalize(y);

        System.out.println( y + " denormalized is " + z);
    }
}

这是我正在处理的文件NormalizedIris.java

package javaai.ann.input;

import javaai.util.Helper;
import org.encog.util.arrayutil.NormalizationAction;
import org.encog.util.arrayutil.NormalizedField;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * This class normalizes the real-world iris data and gives its report.
 */
public class NormalizedIris extends RealWorld {
    /** The high range index */
    public final static int HI = 1;

    /** The low range index */
    public final static int LO = 0;

    /** Normalized data stored here; reals are instantiated by RealWorld. */
    protected static HashMap<String, List<Double>> normals = new HashMap<>();

    /**
     * Launches the program.
     * @param args Command line arguments -- not used
     */
    public static void main(String[] args) {
        // Load the iris data into reals
        load();

        // Get the titles of each column
        Set<String> titles = reals.keySet();

        // Normalize each column by title
        for(String title: titles) {
            // Get the column data for this title
            List<Double> list = reals.get(title);

            // Get range for this column using elementary form of unsupervised learning
            double[] range = getRange(list);

            // Summarize range for this column for its title
            System.out.printf("%-12s: %6.2f - %5.2f\n", title, range[LO], range[HI]);

            // create a NormalizedField instance using the hi-lo range.
            // For reference see Encog's SimpleNormalize.java.
            // Normalize values with an actual range of (0 to 100) to (-1 to 1)
            NormalizedField realsField = new NormalizedField(NormalizationAction.Normalize,
                    null,100,0,1,-1);

            // List will contain normalized iris data for this column.
            List<Double> normalized = new ArrayList<>();

            // TODO: normalize each element of reals and add it to normalized.


            // Add normalized data to the normals for this title.
            normals.put(title, normalized);
        }

        // Write rest of the report

        // This is the column header
        System.out.printf("%3s ","#");

        for(String key: titles)
            System.out.printf("%15s ",key);

        System.out.println();

        // Now write the row by row data -- it should line up right-justified
        int size =  Helper.getRowCount();

        for(int k=0; k < size; k++) {
            System.out.printf("%3d ",k);

            for(String key: titles) {
                Double real = reals.get(key).get(k);

                Double normal = normals.get(key).get(k);

                System.out.printf("%6.2f => %5.2f ",real,normal);
            }

            System.out.println();
        }

    }

    /**
     * Gets hi-lo range using an elementary form of unsupervised learning.
     * @param list List
     * @return 2-tuple of doubles for low and high range
     */
    protected static double[] getRange(List<Double> list) {
        // Initial low and high values
        double[] range = {Double.MAX_VALUE, -Double.MAX_VALUE};

        // Go through each value in the list
        for(Double value: list) {
            // if value greater than range[HI], update range[HI].
            if (value > range[HI]) {
                range[HI] = value;
            }
            // if value less than range[LO], update range[LO].
            if (value < range[LO]) {
                range[LO] = value;
            }
        }

        return range;
    }
}

请让我知道如何规范化此列表的每个元素,然后将其添加到规范化列表中。

仅供参考:输出应如下所示:

Petal.Width : 0.10 - 2.50
Sepal.Length: 4.30 - 7.90
...
# Petal.Width Sepal.Length Sepal.Width Petal.Length
0 0.60 => -0.58 5.00 => -0.61 3.50 => 0.25 1.60 => -0.80
1 1.40 => 0.08 6.80 => 0.39 2.80 => -0.33 4.80 => 0.29
2 0.20 => -0.92 5.00 => -0.61 3.30 => 0.08 1.40 => -0.86
...

提前致谢。

标签: java

解决方案


推荐阅读