gpt4 book ai didi

java - 使用 weka.jar 将 CSV 文件转换为 ARFF

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:01 25 4
gpt4 key购买 nike

对于我的项目,我使用 weka.jar。我正在使用以下代码将 CSV 文件转换为 ARFF:

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

import java.io.File;

public class CsvArffConverter
{

public static void Convert(String sourcepath,String destpath) throws Exception
{
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(sourcepath));
Instances data = loader.getDataSet();

// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File(destpath));
saver.setDestination(new File(destpath));
saver.writeBatch();
}
public static void main(String args[]) throws Exception
{
Convert("C:\\ad\\BSEIT.csv", "C:\\ad\\test.arff");

}
}

但是,在执行时,出现以下错误:

Cannot create a new output file. Standard out is used.
Exception in thread "main" java.io.IOException: Cannot create a new output file (Reason: java.io.IOException: File already exists.). Standard out is used.
at `enter code here`weka.core.converters.AbstractFileSaver.setDestination(AbstractFileSaver.java:421)
at Predictor.CsvArffConverter.Convert(CsvArffConverter.java:29)
at Predictor.CsvArffConverter.main(CsvArffConverter.java:34)

最佳答案

根据 weka mail list ,这个错误是文件问题,可能是权限问题。其他邮箱建议使用Java I/O approch保存 arff 文件。

This error is coming from the CSVSaver and indicates that it is unable 
to create the directory and/or file that you've specified. More than
likely it is something to do with permissions on where it is trying to
write to.

尝试以下代码。

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

import java.io.File;

public class CsvArffConverter
{
public static void Convert(String sourcepath,String destpath) throws Exception
{
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(sourcepath));
Instances dataSet = loader.getDataSet();

// save ARFF
BufferedWriter writer = new BufferedWriter(new FileWriter(destpath));
writer.write(dataSet.toString());
writer.flush();
writer.close();
}

public static void main(String args[]) throws Exception
{
Convert("BSEIT.csv", "test.arff");
}
}

如您所见,我使用相对路径。由于绝对路径写入可能会因为权限问题而被阻塞。

关于java - 使用 weka.jar 将 CSV 文件转换为 ARFF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29166975/

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