0

I am working on this program and am having trouble with line 57: TODO: normalize each element of reals and add it to normalized. I was given a file called SimpleNormalize.java that is where this file (NormalizedIris.java) is extended from.

Here is the file 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);
    }
}

And here is the file I am working on 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;
    }
}

Please let me know how to normalize each element of this list and then add it to the normalized list.

FYI: Output should look something like this:

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
...

Thanks in advance.

  • `from (0 to 100) to (-1 to 1)` so `0` should be `50` no? so `0 -> -1, 50 -> 0, 100 -> 1` given a number `n`, you only need `(n - 50) / 50`... for a generalized version you can have a look to https://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio – Alberto Sinigaglia Sep 24 '20 at 20:14
  • I am mostly talking about the `normalize()` function (taken from `SimpleNormalize.java`) to be applied to all the elements of `reals`. (See `// TODO: normalize each element of reals and add it to normalized.`) – Hannah Youssef Sep 24 '20 at 20:18

0 Answers0