예제 #1
0
 def model(weights, features):
     '''Calculate regression for features given weights.
     [number] [number] --> number
     '''
     return stats.dotprod(weights, features)
예제 #2
0
    del data

    # invert negative examples; put them in positive set
    for d in neg:
        pos.append([-f for f in d])
    assert len(pos) == count
    del neg

    # initialize the weight vector
    weights = len(pos) * [0.0]

    # iterate
    iteration = 0
    mistakes = 1
    while mistakes: # should be while iteration < (m * |v|^2) / (d^2)
        iteration += 1
        mistakes = 0
        for d in pos:
            if stats.dotprod(weights, d) > 0:
                pass # good!
            else:
                weights = [w + f for w, f in zip(weights, d)]
                mistakes += 1
        print 'Iteration {}, total mistakes {}'.format(iteration, mistakes)

    print 'Classifier weights: {}'.format(
        ' '.join([str(w) for w in weights]))

    print 'Normalized with threshold: {}'.format(
        ' '.join([str(w/weights[0]) for w in weights[1:]]))