package org.deeplearning4j.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.IOUtils;
import org.deeplearning4j.berkeley.Pair;

import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.factory.Nd4j;
import org.springframework.core.io.ClassPathResource;



public class IrisUtils {


    public static List<DataSet> loadIris(int from,int to) throws IOException {
        ClassPathResource resource = new ClassPathResource("/iris.dat");
        @SuppressWarnings("unchecked")
        List<String> lines = IOUtils.readLines(resource.getInputStream());
        List<DataSet> list = new ArrayList<>();
        INDArray ret = Nd4j.ones(to, 4);
        List<String> outcomeTypes = new ArrayList<>();
        double[][] outcomes = new double[lines.size()][3];
        for(int i = from; i < to; i++) {
            String line = lines.get(i);
            String[] split = line.split(",");

            addRow(ret,i,split);

            String outcome = split[split.length - 1];
            if(!outcomeTypes.contains(outcome))
                outcomeTypes.add(outcome);
            double[] rowOutcome = new double[3];
            rowOutcome[outcomeTypes.indexOf(outcome)] = 1;
            outcomes[i] = rowOutcome;
        }

        for(int i = 0; i < ret.rows(); i++)
            list.add(new DataSet(ret.getRow(i), Nd4j.create(outcomes[i])));



        return list;
    }







    private static void addRow(INDArray ret,int row,String[] line) {
        double[] vector = new double[4];
        for(int i = 0; i < 4; i++)
            vector[i] = Double.parseDouble(line[i]);

        ret.putRow(row,Nd4j.create(vector));
    }
}

@@ -72,21 +72,16 @@
 va.util.
-Array
 List;%0Aim
@@ -99,42 +99,52 @@
 til.
-Collections;%0Aimport java.util
+Map;%0A%0Aimport com.google.common.collect
 .List
+s
 ;%0Aim
@@ -152,26 +152,39 @@
 ort 
-java.util.Random;%0A
+com.google.common.collect.Maps;
 %0Aimp
@@ -222,48 +222,8 @@
 ls;%0A
-import org.deeplearning4j.berkeley.Pair;
 %0A%0Aim
@@ -397,18 +397,16 @@
 ource;%0A%0A
-%0A%0A
 public c
@@ -422,17 +422,359 @@
 Utils %7B%0A
-%0A
+    private static final int NUM_FEATURES = 4;%0A    private static final int NUM_LABELS = 3;%0A%0A    /**%0A     * A loader function for the Fischer Iris dataset.%0A     * @param from beginning row%0A     * @param to ending row%0A     * @return a collection of dataset objects between the rows %7B@code from%7D and %7B@code to%7D%0A     * @throws IOException%0A     */
 %0A    pub
@@ -816,16 +816,17 @@
 nt from,
+ 
 int to) 
@@ -846,16 +846,42 @@
 ption %7B%0A
+        assert from %3C to;%0A
         
@@ -949,47 +949,8 @@
 %22);%0A
-        @SuppressWarnings(%22unchecked%22)%0A
     
@@ -1049,20 +1049,25 @@
  list = 
+Lists.
 new
- 
 ArrayLis
@@ -1059,34 +1059,32 @@
 sts.newArrayList
-%3C%3E
 ();%0A        INDA
@@ -1110,11 +1110,29 @@
 s(to
-, 4
+ - from, NUM_FEATURES
 );%0A 
@@ -1138,27 +1138,35 @@
         
-List
+Map
 %3CString
+, Integer
 %3E outcom
@@ -1178,23 +1178,23 @@
 s = 
-new ArrayList%3C%3E
+Maps.newHashMap
 ();%0A
@@ -1248,19 +1248,59 @@
 size()%5D%5B
-3%5D;
+NUM_LABELS%5D;%0A        int outcomeIndex = 0;%0A
 %0A       
@@ -1419,17 +1419,16 @@
 t(%22,%22);%0A
-%0A
         
@@ -1446,18 +1446,20 @@
 ret,
+ 
 i,
+ 
 split);%0A
 %0A   
@@ -1454,17 +1454,16 @@
 split);%0A
-%0A
         
@@ -1545,16 +1545,19 @@
 contains
+Key
 (outcome
@@ -1558,16 +1558,18 @@
 utcome))
+ %7B
 %0A       
@@ -1594,19 +1594,19 @@
 pes.
-add
+put
 (outcome
 );%0A 
@@ -1601,19 +1601,49 @@
 (outcome
+, outcomeIndex++
 );%0A
+            %7D%0A
         
@@ -1679,17 +1679,26 @@
  double%5B
-3
+NUM_LABELS
 %5D;%0A     
@@ -1732,15 +1732,11 @@
 pes.
-indexOf
+get
 (out
@@ -1917,18 +1917,16 @@
 i%5D)));%0A%0A
-%0A%0A
         
@@ -1945,22 +1945,16 @@
 %0A    %7D%0A%0A
-%0A%0A%0A%0A%0A%0A
     priv
@@ -1993,16 +1993,17 @@
 ret,
+ 
 int row,
 Stri
@@ -1998,16 +1998,17 @@
 int row,
+ 
 String%5B%5D
@@ -2053,17 +2053,28 @@
  double%5B
-4
+NUM_FEATURES
 %5D;%0A     
@@ -2095,17 +2095,28 @@
  0; i %3C 
-4
+NUM_FEATURES
 ; i++)%0A 
@@ -2167,17 +2167,16 @@
 ne%5Bi%5D);%0A
-%0A
         

Fix loadIris