public String fulltraining(Pair dataset) { StringBuilder sb = new StringBuilder(); ArrayList<Data> dataTable = dataset.getDataSet(); reset(); init(dataset); count(dataset, +1); sb.append(printModel()); sb.append(printProbabilisticModel()); int correct = 0; for (int i = 0; i < dataTable.size(); i++) { String result = test(dataTable.get(i)); if (result.equals(dataTable.get(i).getAttributes().get(nAttr - 1))) { correct++; } } sb.append(printConfusion()); sb.append("\n--- Full Training Set ---\n"); sb.append("Correct Answer\t: ").append(correct).append("\n"); sb.append("Wrong Answer \t: ").append(dataTable.size() - correct).append("\n"); sb.append("Total \t: ").append(dataTable.size()).append("\n"); sb.append("Accuracy \t: ").append(correct * 100.0 / dataTable.size()).append(" %\n"); return sb.toString(); }
public String ten_fold(Pair dataset) { StringBuilder sb = new StringBuilder(); ArrayList<Data> dataTable = dataset.getDataSet(); reset(); init(dataset); count(dataset, +1); sb.append(printModel()); sb.append(printProbabilisticModel()); int sizeMin = dataTable.size() / 10, mod = dataTable.size() % 10; int offset = 0; int correct = 0; for (int i = 0; i < 10; i++) { ArrayList datas = new ArrayList(); int to = offset + sizeMin; if (mod > 0) { to++; mod--; } if (to > dataTable.size()) to = dataTable.size(); for (int j = offset; j < to; j++) { datas.add(dataTable.get(j)); } Pair datatest = new Pair(dataset.getAttrLib(), datas); count(datatest, -1); for (int j = offset; j < to; j++) { String result = test(dataTable.get(j)); if (result.equals(dataTable.get(i).getAttributes().get(nAttr - 1))) correct++; } count(datatest, +1); offset = to; } sb.append(printConfusion()); sb.append("\n--- Cross Validation 10-fold ---\n"); sb.append("Correct Answer\t: ").append(correct).append("\n"); sb.append("Wrong Answer \t: ").append(dataTable.size() - correct).append("\n"); sb.append("Total \t: ").append(dataTable.size()).append("\n"); sb.append("Accuracy \t: ").append(correct * 100.0 / dataTable.size()).append(" %\n"); return sb.toString(); }
public void count(Pair dataset, int change) { ArrayList<Data> dataTable = dataset.getDataSet(); for (int i = 0; i < dataTable.size(); i++) { ArrayList<String> dataValue = dataTable.get(i).getAttributes(); int classId = domainAttr[nAttr - 1].indexOf(dataValue.get(dataValue.size() - 1)); if (classId < -1) continue; for (int j = 0; j < nAttr; j++) { int id = domainAttr[j].indexOf(dataValue.get(j)); if (id < 0) { id = domainAttr[j].size(); domainAttr[j].add(dataValue.get(j)); countTable[j].add(new ArrayList()); for (int k = 0; k < domainAttr[nAttr - 1].size(); k++) countTable[j].get(id).add(0); } int bef = countTable[j].get(id).get(classId); countTable[j].get(id).set(classId, bef + change); bef = sumTable[j].get(classId); sumTable[j].set(classId, bef + change); } nAll += change; } }