1

As it stands I have a data set in the form of a .csv file which you can find here. Also there is some brief documentation on it which you can find here. What I am attempting to do is to manipulate the data set so that I can work with some machine learning algorithms but as it stands I can't seem to print the outputted data to the console

ImageMatrix.java

import java.util.Arrays;


public class ImageMatrix {
    public static int[] data;
    public int classCode;
    public ImageMatrix(int[] data, int classCode) {
        assert data.length == 64;

    }

    public String toString() {
        return "Class Code: " + classCode + " DataSet:" + Arrays.toString(data) + "\n";
    }

    public int[] getData() {
        return data;
    }

    public int getClassCode() {
        return classCode;
    }

}

ImageMatrixDB.java

import java.io.*;
import java.util.*;




public class ImageMatrixDB implements Iterable<ImageMatrix> {
    List<ImageMatrix> list = new ArrayList<ImageMatrix>();

    public static ImageMatrixDB load(String f) throws IOException {
        ImageMatrixDB result = new ImageMatrixDB();
        try (FileReader fr = new FileReader(f);
             BufferedReader br = new BufferedReader(fr)) {
            for (String line; null != (line = br.readLine()); ) {
                int lastComma = line.lastIndexOf(',');
                int classCode = Integer.parseInt(line.substring(1 + lastComma));
                int[] data = Arrays.stream(line.substring(0, lastComma).split(","))
                                   .mapToInt(Integer::parseInt)
                                   .toArray();
                result.list.add(new ImageMatrix(data, classCode));
            }
            System.out.println(ImageMatrix.data.toString());
        }
        return result;
    }



    public Iterator<ImageMatrix> iterator() {
        return this.list.iterator();
    }
    public static void main(String[] args){
        ImageMatrixDB i = new ImageMatrixDB();
        i.load("dataset1.csv"); // <<< ERROR IS HERE
    }
}

The error is within my main function on the line i.load(... I know I must be missing something or have made a mistake somewhere, I have tried altering the data from static but it just throws more errors and I can't figure it out. Any ideas?

6
  • Why do you create an ImageMatrixDB in main, then in your ImageMatrixDB.load() method create another instance of ImageMatrixDB? Commented Mar 9, 2016 at 3:55
  • What would you recommend, I see you point. Commented Mar 9, 2016 at 3:57
  • Well in the instance you create in main you call load on that instance. in the load method, just set the list on this instead of result and return this if you want (this would be the current instance). Also can you provide the ImageMatrix class? Commented Mar 9, 2016 at 3:59
  • Nevermind, I see the ImageMatrix class. On a side note, you should not make fields public like data in ImageMatrix. Make them private and provide public getter/setter methods. Commented Mar 9, 2016 at 4:05
  • What's the error msg? Commented Mar 9, 2016 at 4:10

2 Answers 2

1

Your issue is in the ImageMatrix class. You never set the int[] data in the constructor. You have:

    public ImageMatrix(int[] data, int classCode) {
        assert data.length == 64;

    }

You need:

    public ImageMatrix(int[] data, int classCode) {
        assert data.length == 64;
        this.data = data;
        this.classCode = classCode;
    }

Here is your updated/complete/working code:

ImageMatrix:

import java.util.*;

public class ImageMatrix {
    private int[] data;
    private int classCode;

public ImageMatrix(int[] data, int classCode) {
    assert data.length == 64;
    this.data = data;
    this.classCode = classCode;
}

    public String toString() {
        return "Class Code: " + classCode + " DataSet:" + Arrays.toString(data) + "\n";
    }

    public int[] getData() {
        return data;
    }

    public int getClassCode() {
        return classCode;
    }

}

ImageMatrixDB:

    import java.util.*;
    import java.io.*;

    public class ImageMatrixDB implements Iterable<ImageMatrix> {
        private List<ImageMatrix> list = new ArrayList<ImageMatrix>();

        public ImageMatrixDB load(String f) throws IOException {
            try (
                FileReader fr = new FileReader(f);
                BufferedReader br = new BufferedReader(fr)) {
                String line = null;

                while((line = br.readLine()) != null) {
                    int lastComma = line.lastIndexOf(',');
                    int classCode = Integer.parseInt(line.substring(1 + lastComma));
                    int[] data = Arrays.stream(line.substring(0, lastComma).split(","))
                                       .mapToInt(Integer::parseInt)
                                       .toArray();
                    ImageMatrix matrix = new ImageMatrix(data, classCode);
                    list.add(matrix);
                }
            }
            return this;
        }

        public void printResults(){
            for(ImageMatrix matrix: list){
                System.out.println(matrix);
            }
        }


        public Iterator<ImageMatrix> iterator() {
            return this.list.iterator();
        }
        public static void main(String[] args){
            ImageMatrixDB i = new ImageMatrixDB();
            try{
                i.load("cw2DataSet1.csv"); 
                i.printResults();
            }
            catch(Exception ex){
                ex.printStackTrace();
            }
        }

    }
Sign up to request clarification or add additional context in comments.

5 Comments

Added your suggestions still recieving Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type IOException
Check the answer, I have updated your code, including handling the exception. Because you throw IOException in your load() method, you must catch it in your main, where you call ImageMatrixDB.load().
Ahh I see where the issue was, Thanks for clearing it up will really help. Now onto trying to implement a kNN algorithm :) Thanks again.
one issue I see, the data set being outputted to the terminal is the same one repeated :/
Yea, I found it too, the ImageMatrix.data property was static. Well, there you go, a concrete example of why to avoid static variables unless absolutely necessary. I have updated the answer to correct the issue.
1

Your load method can throw an IOException. You need to catch it in order to successfully compile

public static void main(String[] args){
    ImageMatrixDB i = new ImageMatrixDB();
    try{
        i.load("dataset1.csv"); // <<< ERROR IS HERE
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.