gpt4 book ai didi

java - 从 csv 文件加载数据时出现问题 - Java

转载 作者:行者123 更新时间:2023-11-30 03:01:16 26 4
gpt4 key购买 nike

就目前情况而言,我有一个 .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;
}

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

图像矩阵:

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

}

图像矩阵数据库:

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

}

关于java - 从 csv 文件加载数据时出现问题 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35882507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com