gpt4 book ai didi

java - 使用 weka jar 将 csv 转换为 arff 时出错

转载 作者:行者123 更新时间:2023-12-01 11:37:16 25 4
gpt4 key购买 nike

我想将 csv 文件转换为 arff 文件,并且我正在使用 weka jar 来执行操作。但是它抛出了一个 nullpointerException。这是代码

    /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Model;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;

/**
*
* @author sanketh
*/
public class testClass {
public void createArff(String Filename) {
try {
String path = Filename.substring(0, Filename.length() - 3);
System.out.println("new path:" + path);
String[] args = new String[2];
args[0] = path + "csv";
args[1] = path + "arff";
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(args[0]));
Instances data = loader.getDataSet();
System.out.println(data);
// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
//saver.setFile(new File(args[1]));
saver.setFile(new File("h:\\abc.arff"));
saver.writeBatch();

} catch (IOException ex) {
//System.out.println("Please try with some other test data!");
// attribMap.resultLog="Please try with some other test data!";
Logger.getLogger(GenerateFile.class.getName()).log(Level.SEVERE, null, ex);

}

}

public static void main(String[] args) throws Exception {
String nepath="H:\\file.csv";
testClass t1=new testClass();
t1.createArff(nepath);

}

}

这是堆栈跟踪它在保存 arff 时给我带来了一个错误......帮我看看我哪里出错了?它从 CSV 读取数据,因为我能够显示该数据

    Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.<init>(Unknown Source)
at java.io.PrintWriter.<init>(Unknown Source)
at java.io.PrintWriter.<init>(Unknown Source)
at weka.core.converters.ArffSaver.writeBatch(ArffSaver.java:187)
at Model.testClass.createArff(testClass.java:39)
at Model.testClass.main(testClass.java:65)

最佳答案

向您的保护程序对象添加一行。

saver.setDestination(new File("F:\\abc.arff"));

它将帮助您创建文件。

关于java - 使用 weka jar 将 csv 转换为 arff 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29848467/

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