WEKA JDBC complains when trying to read a CSV file
I am having a hard time finding a good tutorial for WEKA using his k-Means methods. Here is the code:
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);
I am getting the error:
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)
Here is a photo of the csv file:
I am not even trying to connect to the database, I am trying to read the csv file I have. Does anyone know how to upload multiple csv files using weka?
+3
source to share
2 answers
First of all, JDBC warning messages are nothing to worry about, read here .
The following code reads the csv files and prints its contents to the console, see 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);
}
}
The output looks like this:
loading training data
training data loaded
dataset:
@relation deneme1
@attribute x numeric
@attribute y numeric
@data
1,2
2,3
1,4
+1
source to share
According to the ConverterUtils.DataSource docs:
Tries to load the data from the file. Can be either a regular file or a web location (http://, https://, ftp:// or file://).
So, I believe what you are missing file://
along the way.
0
source to share