Пример #1
0
def main2(gdname, gdfunction, training, regression, learningrate):
    '''Perform gradient descent with a learner and analyze the results.'''

    # learning rate
    try:
        initiallr, reducelr = learningrate
        suffix = 'dynamic' + str(initiallr)
    except TypeError:
        initiallr = learningrate
        reducelr = lambda x: x
        suffix = initiallr

    # learn
    weights = GradientDescent.loop(
        len(training[0].features) * [0.0],
        training,
        regression.model,
        regression.gradient,
        initiallr,
        reducelr,
        gdfunction,
        0.99)

    # test
    terror = stats.rmse(
        [regression.model(weights, dp.features) for dp in testing],
        [dp.label for dp in testing])
    print INDENT * 2 + 'Testing RMSE:', terror

    ## produce a result set
    results = [resultset.DataResult(dp.label,
                                    regression.model(weights, dp.features)) \
               for dp in testing]

##    ## find a good operating point
##    op = resultset.minerrop(results)
##    print INDENT * 2 + 'Operating Point:', op

##    ## assign predictions
##    results = resultset.applyop(op, results)

    ## output roc data
    roc = resultset.rocdata(results)
    auc = resultset.auc(roc)
    with open('{}-{}_lambda={}_auc={}'.format(regression.__name__, gdname,
                                       suffix, auc).lower(),
              mode='wb') as fd:
        for fpr, tpr in roc:
            fd.write('{}, {}\n'.format(fpr, tpr))
Пример #2
0
        # curry the stump to a classifier function [dp --> -1 or 1]
        curried = (lambda s: lambda dp: Stump.query(s, dp))(stump)
        #
        # boost new datapoint weights
        wv = boost.round(curried)
        #
        # output status
        print mistakect(training, H) / float(len(training)),
        testerr = mistakect(testing, H) / float(len(testing))
        print testerr,
        #
        # calculate auc
        rv = [
            resultset.DataResult(int(dp.label > 0), boost.model(dp))
            for dp in testing
        ]
        print resultset.auc(resultset.rocdata(rv)),
        #
        # done
        print
    print 'Boosting time:', str(datetime.datetime.now() - start)
    rocfn = 'boostROC-{}.log'.format(datetime.datetime.now()).replace(':', '-')
    print 'Writing ROC data to "{}"'.format(rocfn)
    with open(rocfn, mode='wb') as fd:
        for fpr, tpr in resultset.rocdata(rv):
            fd.write('{} {}\n'.format(fpr, tpr))

    #raw_input('[Press Enter to Quit]')

###############################################################################
Пример #3
0
        stump = svpick(wv)
        print stump[0],
        #
        # curry the stump to a classifier function [dp --> -1 or 1]
        curried = (lambda s: lambda dp: Stump.query(s, dp))(stump)
        #
        # boost new datapoint weights
        wv = boost.round(curried)
        #
        # output status
        print mistakect(training, H) / float(len(training)),
        testerr = mistakect( testing, H) / float(len( testing))
        print testerr,
        #
        # calculate auc
        rv = [resultset.DataResult(int(dp.label > 0), boost.model(dp)) for dp in testing]
        print resultset.auc(resultset.rocdata(rv)),
        #
        # done
        print
    print 'Boosting time:', str(datetime.datetime.now() - start)
    rocfn = 'boostROC-{}.log'.format(datetime.datetime.now()).replace(':', '-')
    print 'Writing ROC data to "{}"'.format(rocfn)
    with open(rocfn, mode='wb') as fd:
        for fpr, tpr in resultset.rocdata(rv):
            fd.write('{} {}\n'.format(fpr, tpr))
    
    #raw_input('[Press Enter to Quit]')

###############################################################################