Hello. My conclusion is that you need to have labeled data. It works
if you prelabel the data in the Test set (with arbitrary values). I
cannot get it to work if there are not values for the class variable
in the Test set.
On Thu, Apr 26, 2012 at 10:19 PM, Alexander Brooks
<
alexander.brooks10@...> wrote:
> Hello. I am trying the following code. I want to label unlabeled data
> based on a classifier. I tried to follow the instructions in the wiki
> but I am not getting labels in the resulting new file. I will post the
> code here and below that the training and testing data and resulting
> file. Thanks for any advice.
>
> --
> Alex
>
> import weka.classifiers.Evaluation;
> import weka.classifiers.trees.J48;
> import weka.core.Instances;
> import weka.experiment.InstanceQuery;
>
> import java.io.BufferedReader;
> import java.io.FileReader;
>
> import java.sql.Connection;
> import java.sql.DriverManager;
> import java.sql.PreparedStatement;
> import java.sql.SQLException;
>
> import java.text.DateFormat;
> import java.text.SimpleDateFormat;
> import java.util.Calendar;
>
> import finance.google.*;
> import java.util.ArrayList;
>
>
> import java.io.BufferedWriter;
>
> import java.io.FileWriter;
>
>
> public class unlabeledTest1 {
>
> @SuppressWarnings("unchecked")
> public static void main(String[] args) throws Exception {
>
> BufferedReader reader = new BufferedReader(
> new
> FileReader("C:/Users/Alex/Desktop/chatvestor/Diamond/src/weather.nominalTrain.arff"));
> Instances data = new Instances(reader);
> reader.close();
> // setting class attribute
> data.setClassIndex(data.numAttributes() - 1);
>
>
> String[] options = new String[1];
> options[0] = "-U"; // unpruned tree
> J48 tree = new J48(); // new instance of tree
> tree.setOptions(options); // set the options
> tree.buildClassifier(data); // build classifier
>
> // load unlabeled data
> Instances unlabeled = new Instances(
> new BufferedReader(
> new
> FileReader("C:/Users/Alex/Desktop/chatvestor/Diamond/src/weather.nominalTest.arff")));
>
>
> // set class attribute
> unlabeled.setClassIndex(unlabeled.numAttributes() - 1);
>
> // create copy
> Instances labeled = new Instances(unlabeled);
>
> // label instances
> for (int i = 0; i < unlabeled.numInstances(); i++) {
> double clsLabel = tree.classifyInstance(unlabeled.instance(i));
> labeled.instance(i).setClassValue(clsLabel);
> }
> // save labeled data
> BufferedWriter writer = new BufferedWriter(
> new
> FileWriter("C:/Users/Alex/Desktop/unlabeledTest/labeled.arff"));
> writer.write(labeled.toString());
> writer.newLine();
> writer.flush();
> writer.close();
>
>
> }
>
> }
>
>
> weather.nominalTrain.arff:
>
> @relation weather.symbolic
>
> @attribute outlook {sunny, overcast, rainy}
> @attribute temperature {hot, mild, cool}
> @attribute humidity {high, normal}
> @attribute windy {TRUE, FALSE}
> @attribute play {yes, no}
>
> @data
> rainy,cool,normal,TRUE,no
> overcast,cool,normal,TRUE,yes
> sunny,mild,high,FALSE,no
> sunny,cool,normal,FALSE,yes
> rainy,mild,normal,FALSE,yes
> sunny,mild,normal,TRUE,yes
> overcast,mild,high,TRUE,yes
> overcast,hot,normal,FALSE,yes
> rainy,mild,high,TRUE,no
> sunny,hot,high,FALSE,no
> sunny,hot,high,TRUE,no
> overcast,hot,high,FALSE,yes
> rainy,mild,high,FALSE,yes
> rainy,cool,normal,FALSE,yes
>
>
> Weather.nominalTest.arff
>
> @relation weather.symbolic
>
> @attribute outlook {sunny, overcast, rainy}
> @attribute temperature {hot, mild, cool}
> @attribute humidity {high, normal}
> @attribute windy {TRUE, FALSE}
>
>
> @data
> rainy,cool,normal,TRUE
> overcast,cool,normal,TRUE
> sunny,mild,high,FALSE
> sunny,cool,normal,FALSE
>
>
> labeled.arff
>
> @relation weather.symbolic
>
> @attribute outlook {sunny,overcast,rainy}
> @attribute temperature {hot,mild,cool}
> @attribute humidity {high,normal}
> @attribute windy {TRUE,FALSE}
>
> @data
> rainy,cool,normal,FALSE
> overcast,cool,normal,TRUE
> sunny,mild,high,FALSE
> sunny,cool,normal,TRUE
--
Alex
_______________________________________________
Wekalist mailing list
Send posts to:
Wekalist@...
List info and subscription status:
https://list.scms.waikato.ac.nz/mailman/listinfo/wekalistList etiquette:
http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html