I am trying to get a learning curve for an automated weka experiment. I currently have the following java code.
public static void EvaluateModel(AbstractClassifier cl, String datapath, String outfile) throws Exception {
Experiment exp = new Experiment();
ClassifierSplitEvaluator se = new ClassifierSplitEvaluator();
se.setClassifier(cl);
Classifier sec = ((ClassifierSplitEvaluator) se).getClassifier();
CrossValidationResultProducer cvrp = new CrossValidationResultProducer();
cvrp.setNumFolds(10);
cvrp.setSplitEvaluator(se);
PropertyNode[] propertyPath = new PropertyNode[2];
try {
propertyPath[0] = new PropertyNode(
se,
new PropertyDescriptor("splitEvaluator",
CrossValidationResultProducer.class),
CrossValidationResultProducer.class);
propertyPath[1] = new PropertyNode(sec,
new PropertyDescriptor("classifier", se.getClass()),
se.getClass());
} catch (IntrospectionException e) {
e.printStackTrace();
}
exp.setResultProducer(cvrp);
exp.setPropertyPath(propertyPath);
exp.setPropertyArray(new Classifier[]{cl});
DefaultListModel model = new DefaultListModel();
model.addElement(new File(datapath));
exp.setDatasets(model);
InstancesResultListener irl = new InstancesResultListener();
irl.setOutputFile(new File(outfile));
exp.setResultListener(irl);
System.out.println("Initializing...");
exp.initialize();
System.out.println("Running...");
exp.runExperiment();
System.out.println("Finishing...");
exp.postProcess();
System.out.println("Evaluating...");
PairedTTester tester = new PairedCorrectedTTester();
FileReader reader = new FileReader(irl.getOutputFile());
Instances result = new Instances(reader);
tester.setInstances(result);
tester.setSortColumn(-1);
tester.setRunColumn(result.attribute("Key_Run").index());
tester.setFoldColumn(result.attribute("Key_Fold").index());
tester.setDatasetKeyColumns(
new Range(
""
+ (result.attribute("Key_Dataset").index() + 1)));
tester.setResultsetKeyColumns(
new Range(
""
+ (result.attribute("Key_Scheme").index() + 1)
+ ","
+ (result.attribute("Key_Scheme_options").index() + 1)
+ ","
+ (result.attribute("Key_Scheme_version_ID").index() + 1)));
tester.setResultMatrix(new ResultMatrixPlainText());
tester.setDisplayedResultsets(null);
tester.setSignificanceLevel(0.05);
tester.setShowStdDevs(true);
// fill result matrix (but discarding the output)
tester.multiResultsetFull(0, result.attribute("Percent_correct").index());
// output results for reach dataset
System.out.println("\nResult:");
ResultMatrix matrix = tester.getResultMatrix();
for (int i = 0; i < matrix.getColCount(); i++) {
System.out.println(matrix.getColName(i));
System.out.println(" Perc. correct: " + matrix.getMean(i, 0));
System.out.println(" StdDev: " + matrix.getStdDev(i, 0));
}
}
What I would like to do is either save or display the learning curve in this method. I cannot find info for how to do this programmatically.
Related
I have trouble finding which part of the code is duplicated and how to fix it ?
try {
String template = new String(sourceTemplate);
// Substitute for %CODE%
int templateSplitBegin = template.indexOf("%CODE%");
int templateSplitEnd = templateSplitBegin + 6;
String templatePartOne = new String(
template.substring(0, templateSplitBegin));
String templatePartTwo = new String(
template.substring(templateSplitEnd, template.length()));
code = new String(reqId);
template = new String(templatePartOne + code + templatePartTwo);
// Substitute for %ALTCODE%
templateSplitBegin = template.indexOf("%ALTCODE%");
templateSplitEnd = templateSplitBegin + 9;
templatePartOne = new String(
template.substring(0, templateSplitBegin));
templatePartTwo = new String(
template.substring(templateSplitEnd, template.length()));
altcode = code.substring(0, 5) + "-" + code.substring(5, 8);
out.print(templatePartOne + altcode + templatePartTwo);
} catch (Exception e) {
System.out.println("Error in substitute()");
}
Try making a method with (String template, String splitBy, int offset) as parameters.
also, you can try using stringBegin = template.split(splitBy)[0] and stringEnd = template.split(splitBy)[1] instead of using indexOf() and then substring()
this two parts are duplicated. you can make a method to process the string
try {
String template = new String(sourceTemplate);
// Substitute for %CODE%
code = new String(reqId);
template = processString(template, 6, "%CODE%", code);
// Substitute for %ALTCODE%
altcode = code.substring(0, 5) + "-" + code.substring(5, 8);
template = processString(template, 9, "%ALTCODE%", altcode);
out.print(template);
} catch (Exception e) {
}
private String processString(String template, int length, String code, String mid) {
int templateSplitBegin = template.indexOf(code);
int templateSplitEnd = templateSplitBegin + len;
String templatePartOne = new String(
template.substring(0, templateSplitBegin));
String templatePartTwo = new String(
template.substring(templateSplitEnd, template.length()));
return new String(templatePartOne + mid + templatePartTwo);
}
I have the training and test "labeled.arff" files. Then I build a classifier and write to a "modelFile.model" file.
I have a "unlabeled.arff" file with the last attribute in each row "?".
How can I make the prediction in Java or C#?
I have some code but it is not right, always gives me the same prediction.
Thank you
// Write to Model
public static void Classify()
{
Instances train = new Instances(new java.io.FileReader(dirTrain + "labeled.arff"));
Instances test = new Instances(new java.io.FileReader(dirTest + "labeled.arff"));
train.setClassIndex(train.numAttributes() - 1);
test.setClassIndex(test.numAttributes() - 1);
// train Classifier
Classifier cl = new J48();
// Randomize the order of the instances in the dataset
weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
myRandom.setInputFormat(train);
train = weka.filters.Filter.useFilter(train, myRandom);
// Build the classifier
cl.buildClassifier(train);
// evaluate classifier and print some statistics
Evaluation eval = new Evaluation(train);
eval.evaluateModel(cl, test);
Console.WriteLine(eval.toSummaryString("\nResults Decision Tree\n======\n", false));
SerializationHelper.write(dirModel + "modelFile.model", cl);
}
// Make predictions
public void Predictions()
{
Classifier cl = (Classifier)SerializationHelper.read(dirModel + "modelFile.model");
// load unlabeled data
Instances unlabeled = new Instances(new java.io.FileReader(pathFeatures + "unlabeled.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 = cl.classifyInstance(unlabeled.instance(i));
labeled.instance(i).setClassValue(clsLabel);
}
int numCorrect = 0;
for (int i = 0; i < unlabeled.numInstances(); i++)
{
double pred = cl.classifyInstance(unlabeled.instance(i));
Console.Write("ID: " + unlabeled.instance(i).value(i));
//Console.Write(", actual: " + unlabeled.classAttribute().value((int)unlabeled.instance(i).classValue()));
Console.WriteLine(", predicted: " + unlabeled.classAttribute().value((int)pred));
}
Console.WriteLine("Correct predictions: " + numCorrect);
}
i have problem with remove data from line chart. I wrote a program drawing graphs that after the action click on the button completes the chart data.
dataSeries1.getData().removeAll(); <- doesn't work.
Code:
NumberAxis xAxis = new NumberAxis();
xAxis.setLabel("Oś Y");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("Oś X");
final LineChart lineChart = new LineChart(xAxis, yAxis);
final XYChart.Series dataSeries1 = new XYChart.Series();
lineChart.setCreateSymbols(false);
lineChart.getData().add(dataSeries1);
Button action:
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
int lewy = Integer.parseInt(kresLewy.getText());
int prawy = Integer.parseInt(kresPrawy.getText());
String rownanie = field.getText();
try {
String tekst = lewy + "; " + prawy + "; " + rownanie;
StringReader tekstReader = new StringReader(tekst);
parsery.interpreter.parser parser_obj
= new parsery.interpreter.parser(new parsery.interpreter.MyLexer(tekstReader));
Object result = parser_obj.parse().value;
String sWynik = result.toString();
ZmiennaX zX = new ZmiennaX();
ArrayList<Double> xArr = new ArrayList<Double>();
for (double i = lewy; i <= prawy + 0.001; i = i + zX.getDokladnosc()) // +0.001 dla bledow zaokraglenia
{
xArr.add(zX.round2(i));
}
String sX = xArr.toString();
String wartosciX = sX.substring(1, sX.length() - 1);
String wartosciY = sWynik.substring(1, sWynik.length() - 1);
String XbezSpacji = wartosciX.replace(" ", "");
String YbezSpacji = wartosciY.replace(" ", "");
String[] splitX = XbezSpacji.split(",");
String[] splitY = YbezSpacji.split(",");
dataSeries1.getData().removeAll();
for(int i=0; i<splitX.length; i++){
double x = Double.parseDouble(splitX[i]);
double y = Double.parseDouble(splitY[i]);
dataSeries1.getData().add(new XYChart.Data(x, y));
}
} catch (Exception e) {
System.out.println("Podczs obliczenia wystapil blad. (" + e.getMessage() + ")");
} catch (Error error) {
System.out.println("Podczs obliczenia wystapil blad. (" + error.getMessage() + ")");
}
}
});
Can anyone help me to remove data after drawing a new chart?
removeAll requires elements to be removed passed in its parameter. Since you provided none - nothing gets removed:
dataSeries1.getData().removeAll();
You want to use clear() instead in the line above.
public void clear()
Removes all of the elements from this list (optional operation). The list will be empty after this call returns.
You can use Collections.singleton to remove all data:
dataSeries1.getData().removeAll(Collections.singleton(barChart.getData().setAll()));
See Oracle documentation for Collections.singleton https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
I am trying to use I/O to give a report on the stock that I need (if the stock is below 8).
It tells me it requires an int for myShop.listLowStockToFile());; when I add a number it tells me that 'void is not allowed here'. How can I fix this?
public void listLowStockToFile(int threshhold)
{
System.out.println("****The Stock that is getting low is: " + " Minimum " +threshhold + " Report for Bob Shaw****\n");
for (Item nextItem : items)
{
if(nextItem.getNuminStock() < threshhold)
{
System.out.println(nextItem);
}
}
}
public class Report {
public static void main(String[] args) {
Shop myShop = new Shop();
CD cd1 = new CD("Abba Gold", "Abba", 15);
myShop.addItem(cd1);
Game game1 = new Game("Chess", 2, 39.95);
myShop.addItem(game1);
ElectronicGame eg1 = new ElectronicGame("Shrek", "PS2", 1, 79.50);
myShop.addItem(eg1);
ElectronicGame eg2 = new ElectronicGame("Doom", "PC", 2, 30.20);
myShop.addItem(eg2);
ElectronicGame eg3 = new ElectronicGame("AFL", "PS2", 2, 49.95);
myShop.addItem(eg3);
cd1.receiveStock(3);
game1.receiveStock(5);
eg1.receiveStock(10);
eg2.receiveStock(1);
cd1.receiveStock(7);
cd1.sellCopy(true);
cd1.sellCopy(true);
eg2.sellCopy(true);
myShop.listItems();
myShop.listLowStockToFile(8);
myShop.listGamesByPlatform("PS2");
myShop.calcTotalSales();
Game game2 = new Game("Chess", 2, 39.95);
myShop.addItem(game2);
eg2.sellCopy(false);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("LowStock.txt"));
writer.write("Report dated" + new Date() + "\n");
writer.write(myShop.listLowStockToFile()); // This line.
writer.close();
System.out.println("Report finished");
} catch (Exception ex) {
System.out.println("File I/O error" + ex);
}
}
}
You need listLowStockToFile to return a String:
public String listLowStockToFile(int threshhold) {
String rtn = "****The Stock that is getting low is: " + " Minimum " +threshhold + " Report for Bob Shaw****\n";
for (Item nextItem : items) {
if(nextItem.getNuminStock() < threshhold) {
rtn += nextItem.toString() + "\n";
}
}
System.out.print(rtn);
return rtn;
}
The reason is that BufferedWritter.write takes a String as an argument.
try {
final List<String> ar = new ArrayList<String>();
final PRIvariable pri = new PRIvariable();
final BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream("C:/cdr2.csv")));
while (reader.ready()) {
final String line = reader.readLine();
final String[] values = line.split(",");
pri.dateText = values[2] + " " + values[4];
pri.count = pri.count + 1;
pri.sum = pri.sum + Integer.parseInt(values[7]);
System.out.println(pri.dateText + " " + pri.sum + " " + pri.count);
ar.add(pri);
}
final String[] columnNames = { "Date", "TOTAL", "COUNTS" };
final String[][] cells = new String[ar.size()][3];
for (int i = 0; i < ar.size(); i++) {
cells[i][0] = ((PRIvariable) ar.get(i)).dateText;
cells[i][1] = "" + ((PRIvariable) ar.get(i)).sum;
cells[i][2] = "" + ((PRIvariable) ar.get(i)).count;
}
table = new JTable(cells, columnNames);
table.setSize(400, 400);
table.setVisible(true);
final JScrollPane js = new JScrollPane();
js.setViewportView(table);
js.setSize(400, 400);
js.setVisible(true);
add(js, java.awt.BorderLayout.CENTER);
} catch (final Exception e) {
System.out.println(e);
}
This is my code. Here i want to Read text file and put that data to Jtable. But in this code it shows every row of the Jtable filled with same data that contain in arraylist(ar) last row. ( i think there is problem in my arraylist). How can i solve this......
The problem is with the variable pri. It has to be created inside the while loop.
Like this
String line = null;
while ((line = reader.readLine()) != null) {
PRIvariable pri = new PRIvariable();
String[] values = line.split(",");
pri.dateText = values[2] + " " + values[4];
pri.count = pri.count + 1;
pri.sum = pri.sum + Integer.parseInt(values[7]);
System.out.println(pri.dateText + " " + pri.sum + " " + pri.count);
ar.add(pri);
}
In your code your want to create a separate instance of PRIvariable for every line in the file, but you are creating only once instance of PRIvariable at the beginning then you are always using that instance by overriding the previous value.