def crossvalidation(ds, trainer, model, nfolds=10, verbose=1, shuffle=False, error=eval.errorrate, seed=None): n = ds.n ds.shuffle(seed=seed) folds = ds.split(nfolds) err = [] for foldidx in range(nfolds): if verbose > 1: print("--------------------") print("Fold-%d" % (foldidx + 1)) print("--------------------") lm = copy.deepcopy(model) t1 = time() dtest = folds[foldidx] trainidxs = range(nfolds) del trainidxs[foldidx] dtrain = MemoryDataset.merge(folds[trainidxs]) trainer.train(lm, dtrain, verbose=(verbose - 1), shuffle=shuffle) e = error(lm, dtest) if verbose > 0: fid = ("%d" % (foldidx + 1)).ljust(5) print("%s %s" % (fid, ("%.2f" % e).rjust(5))) err.append(e) if verbose > 1: print "Total time for fold-%d: %f" % (foldidx + 1, time() - t1) return np.array(err)
def crossvalidation(ds, trainer, model, nfolds=10, verbose=1, shuffle=False, error=eval.errorrate, seed=None): n = ds.n ds.shuffle(seed = seed) folds = ds.split(nfolds) err = [] for foldidx in range(nfolds): if verbose > 1: print("--------------------") print("Fold-%d" % (foldidx+1)) print("--------------------") lm = copy.deepcopy(model) t1 = time() dtest = folds[foldidx] trainidxs = range(nfolds) del trainidxs[foldidx] dtrain = MemoryDataset.merge(folds[trainidxs]) trainer.train(lm, dtrain, verbose = (verbose-1), shuffle = shuffle) e = error(lm,dtest) if verbose > 0: fid = ("%d" % (foldidx+1)).ljust(5) print("%s %s" % (fid , ("%.2f"%e).rjust(5))) err.append(e) if verbose > 1: print "Total time for fold-%d: %f" % (foldidx+1, time()-t1) return np.array(err)