コード例 #1
0
ファイル: crossvalidation.py プロジェクト: zhangwj0101/nut
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)
コード例 #2
0
ファイル: crossvalidation.py プロジェクト: BigR-Lab/nut
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)