就目前而言,我有一个.csv文件形式的数据集,您可以找到here。也有一些简短的文档,您可以找到here。我试图做的是操纵数据集,以便可以使用某些机器学习算法,但就目前而言,我似乎无法将输出的数据打印到控制台

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


该错误在我的主要函数中,在i.load(...行上。弄清楚,有什么想法吗?

最佳答案

您的问题在ImageMatrix类中。
您永远不会在构造函数中设置int[] data。你有:

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

    }


你需要:

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


这是您的更新/完成/工作代码:

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();
            }
        }

    }

10-01 07:10