| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void trainC(ClassificationDataSet dataSet, ExecutorService threadPool) |
| 239 | { |
| 240 | final int models = baseClassifiers.size(); |
| 241 | final int C = dataSet.getClassSize(); |
| 242 | weightsPerModel = C == 2 ? 1 : C; |
| 243 | ClassificationDataSet metaSet = new ClassificationDataSet(weightsPerModel*models, new CategoricalData[0], dataSet.getPredicting()); |
| 244 | |
| 245 | List<ClassificationDataSet> dataFolds = dataSet.cvSet(folds); |
| 246 | //iterate in the order of the folds so we get the right dataum weights |
| 247 | for(ClassificationDataSet cds : dataFolds) |
| 248 | for(int i = 0; i < cds.getSampleSize(); i++) |
| 249 | metaSet.addDataPoint(new DenseVector(weightsPerModel*models), cds.getDataPointCategory(i), cds.getDataPoint(i).getWeight()); |
| 250 | |
| 251 | //create the meta training set |
| 252 | for(int c = 0; c < baseClassifiers.size(); c++) |
| 253 | { |
| 254 | Classifier cl = baseClassifiers.get(c); |
| 255 | int pos = 0; |
| 256 | for(int f = 0; f < dataFolds.size(); f++) |
| 257 | { |
| 258 | ClassificationDataSet train = ClassificationDataSet.comineAllBut(dataFolds, f); |
| 259 | ClassificationDataSet test = dataFolds.get(f); |
| 260 | if(threadPool == null) |
| 261 | cl.trainC(train); |
| 262 | else |
| 263 | cl.trainC(train, threadPool); |
| 264 | for(int i = 0; i < test.getSampleSize(); i++)//evaluate and mark each point in the held out fold. |
| 265 | { |
| 266 | CategoricalResults pred = cl.classify(test.getDataPoint(i)); |
| 267 | if(C == 2) |
| 268 | metaSet.getDataPoint(pos).getNumericalValues().set(c, pred.getProb(0)*2-1); |
| 269 | else |
| 270 | { |
| 271 | Vec toSet = metaSet.getDataPoint(pos).getNumericalValues(); |
| 272 | for(int j = weightsPerModel*c; j < weightsPerModel*(c+1); j++) |
| 273 | toSet.set(j, pred.getProb(j-weightsPerModel*c)); |
| 274 | } |
| 275 | |
| 276 | pos++; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | //train the meta model |
| 282 | if(threadPool == null) |
| 283 | aggregatingClassifier.trainC(metaSet); |
| 284 | else |
| 285 | aggregatingClassifier.trainC(metaSet, threadPool); |
| 286 | |
| 287 | //train the final classifiers, unless folds=1. In that case they are already trained |
| 288 | if(folds != 1) |
| 289 | { |
| 290 | for(Classifier cl : baseClassifiers) |
| 291 | if(threadPool == null) |
| 292 | cl.trainC(dataSet); |
| 293 | else |
| 294 | cl.trainC(dataSet, threadPool); |