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

问题描述 投票:3回答:3

我很难用其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文件的照片:“在此处输入图像描述”

我什至没有尝试连接到数据库,而是试图读取我拥有的csv文件。有人知道如何通过weka加载多个csv文件吗?

java weka
3个回答
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

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://


0
投票

验证您导入的DataSource它必须是weka.core.converters.ConverterUtils.DataSource;而不是javax.sql.Datasource

© www.soinside.com 2019 - 2024. All rights reserved.