Reputation: 600
Given a text file with the following contents (say weather.txt):
outlook = sunny
| humidity <= 75: yes (2.0)
| humidity > 75: no (3.0)
outlook = overcast: yes (4.0)
outlook = rainy
| windy = TRUE: no (2.0)
| windy = FALSE: yes (3.0)
Is there a way in Java to read this text as a J48 Object.
Currently I have a data file "data.txt"( in arff format ):
@relation weather
@attribute outlook {sunny, overcast, rainy}
@attribute temperature numeric
@attribute humidity numeric
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}
@data
sunny,85,85,FALSE,no
sunny,80,90,TRUE,no
overcast,83,86,FALSE,yes
rainy,70,96,FALSE,yes
rainy,68,80,FALSE,yes
rainy,65,70,TRUE,no
overcast,64,65,TRUE,yes
sunny,72,95,FALSE,no
sunny,69,70,FALSE,yes
rainy,75,80,FALSE,yes
sunny,75,70,TRUE,yes
overcast,72,90,TRUE,yes
overcast,81,75,FALSE,yes
rainy,71,91,TRUE,no
My current code is (as below) printing the above tree:
J48 cls = new J48();
Instances inst = new Instances(new BufferedReader(new FileReader("data.txt")));
inst.setClassIndex(inst.numAttributes() - 1);
cls.buildClassifier(inst);
weka.core.SerializationHelper.write("j48.model", cls);
J48 cls2 = (J48) weka.core.SerializationHelper.read("j48.model");
System.out.println(cls2);
My objective is to save these trees in text files. and read them back. How to read them in java?
Upvotes: 0
Views: 116