WEKA JDBC在尝试读取CSV文件时抱怨

时间:2014-12-17 14:16:55

标签: java weka

我很难使用其k-Means方法为WEKA找到一个好的教程。这是代码:

    System.out.println("loading training data");
    String path = System.getProperty("user.dir");

    // Read all the instances in the file (ARFF, CSV, XRFF, ...)
    Instances instances = null;
    try {
        DataSource source = new DataSource(path+"/data/train/class1_1.csv");
        instances = source.getDataSet();
    } catch (Exception e) {
        e.printStackTrace();

        return;
    }
    System.out.println("training data loaded");

    // Make the last attribute be the class
    instances.setClassIndex(instances.numAttributes() - 1);

    // Print header and instances.
    System.out.println("\ndataset:\n");
    System.out.println(instances);

我收到错误:

loading training data
---Registering Weka Editors---
Trying to add JDBC driver: RmiJdbc.RJDriver - Error, not in CLASSPATH?
Trying to add JDBC driver: jdbc.idbDriver - Error, not in CLASSPATH?
Trying to add JDBC driver: org.gjt.mm.mysql.Driver - Error, not in CLASSPATH?
Trying to add JDBC driver: com.mckoi.JDBCDriver - Error, not in CLASSPATH?
Trying to add JDBC driver: org.hsqldb.jdbcDriver - Error, not in CLASSPATH?
java.io.IOException: Stream closed
    at java.io.BufferedReader.ensureOpen(BufferedReader.java:122)
    at java.io.BufferedReader.read(BufferedReader.java:179)
    at java.io.StreamTokenizer.read(StreamTokenizer.java:500)
    at java.io.StreamTokenizer.nextToken(StreamTokenizer.java:544)
    at weka.core.converters.ConverterUtils.getFirstToken(Unknown Source)
    at weka.core.converters.CSVLoader.getInstance(Unknown Source)
    at weka.core.converters.CSVLoader.getDataSet(Unknown Source)
    at weka.core.converters.ConverterUtils$DataSource.getDataSet(Unknown Source)
    at hmm.HMM.run(HMM.java:49)
    at hmm.HMM.main(HMM.java:19)
training data loadedException in thread "main" java.lang.NullPointerException
    at hmm.HMM.run(HMM.java:58)
    at hmm.HMM.main(HMM.java:19)

这是csv文件的照片: enter image description here

我甚至没有尝试连接到数据库,我正在尝试读取我拥有的csv文件。有人知道如何使用weka加载多个csv文件吗?

2 个答案:

答案 0 :(得分:1)

首先,JDBC警告消息无需担心,请阅读here

以下代码读取csv文件并将其内容输出到控制台,请参阅github

package wekaExamples.loadDatasetExamples;

import weka.core.Instances;
import weka.core.converters.*;

import java.io.*;


/**
 * Created by atilla.ozgur on 17.12.2014.
 */
public class LoadCsvExample {

    public static void main(String[] args) {
        System.out.println("loading training data");

        Instances instances = null;
        try {
            String fileName = "./data/deneme1.csv";

            CSVLoader loader = new CSVLoader();
            loader.setSource(new File(fileName));
            instances = loader.getDataSet();

        } catch (Exception e) {
            e.printStackTrace();

            return;
        }
        System.out.println("training data loaded");

        // Make the last attribute be the class
        instances.setClassIndex(instances.numAttributes() - 1);

        // Print header and instances.
        System.out.println("\ndataset:\n");
        System.out.println(instances);
    }

}

输出如下:

loading training data
training data loaded

dataset:

@relation deneme1

@attribute x numeric
@attribute y numeric

@data
1,2
2,3
1,4

答案 1 :(得分:0)

根据ConverterUtils.DataSource文档:

Tries to load the data from the file. Can be either a regular file or a web location (http://, https://, ftp:// or file://).

所以我相信你错过了路径中的file://