Example #1
0
def test_d3_3a_nb():
    global x_tr_pruned, y_tr

    theta_nb = naive_bayes.estimate_nb(x_tr_pruned, y_tr, 0.1)

    y_hat, scores = clf_base.predict(x_tr_pruned[55], theta_nb, labels)
    assert_almost_equals(scores['2000s'], -1840.5064690929203, places=3)
    eq_(y_hat, '1980s')

    y_hat, scores = clf_base.predict(x_tr_pruned[155], theta_nb, labels)
    assert_almost_equals(scores['1980s'], -2153.0199277981355, places=3)
    eq_(y_hat, '2000s')
Example #2
0
def perceptron_update(x, y, weights, labels):
    '''
    compute the perceptron update for a single instance

    :param x: instance, a counter of base features and weights
    :param y: label, a string
    :param weights: a weight vector, represented as a dict
    :param labels: set of possible labels
    :returns: updates to weights, which should be added to weights
    :rtype: defaultdict

    '''

    weights_update = defaultdict(float)

    pred, _ = predict(x, weights, labels)
    predf = make_feature_vector(x, pred)
    true_pred = make_feature_vector(x, y)

    if pred != y:

        weights_update.update(true_pred)

        for features, value in predf.items():
            predf[features] = -value
        weights_update.update(predf)

    return weights_update
Example #3
0
def perceptron_update(x,y,weights,labels):
    '''
    compute the perceptron update for a single instance

    :param x: instance, a counter of base features and weights
    :param y: label, a string
    :param weights: a weight vector, represented as a dict
    :param labels: set of possible labels
    :returns: updates to weights, which should be added to weights
    :rtype: defaultdict

    '''
    ypred, scores = predict(x, weights, labels)
    orig_feature = make_feature_vector(x, y)
    if(ypred ==y):
      return defaultdict(float)
    else:
      pred_feature = make_feature_vector(x, ypred)
      res = {}
      for key in orig_feature.keys():
        if key in pred_feature.keys():
          res[key] = orig_feature[key]-pred_feature[key]
        else:
          res[key] = orig_feature[key]
      for key in pred_feature.keys():
        if key not in orig_feature.keys():
          res[key] = -pred_feature[key]
      output = defaultdict(float, res)
      return output
Example #4
0
def test_d2_2_predict():
    global x_tr_pruned, x_dv_pruned, y_dv

    y_hat, scores = clf_base.predict(x_tr_pruned[0], hand_weights.theta_hand,
                                     labels)
    eq_(scores['pre-1980'], 0.1)
    assert_almost_equals(scores['2000s'], 1.3, places=5)
    eq_(y_hat, '2000s')
    eq_(scores['1980s'], 0.0)

    y_hat = clf_base.predict_all(x_dv_pruned, hand_weights.theta_hand, labels)
    assert_almost_equals(evaluation.acc(y_hat, y_dv), .3422222, places=5)
Example #5
0
    def classify(words, all_tags):
        """This nested function should return a list of tags, computed using a classifier with the weights passed as arguments to make_classifier_tagger and using basefeatures for each token (just the token and the offset)

        :param words: list of words
        :param all_tags: all possible tags
        :returns: list of tags
        :rtype: list

        """
        
        tags = []
        
        for word in words: 
            label, score = clf_base.predict({word:1},weights,list(all_tags))
            tags.append(label)
        
        return tags
Example #6
0
    def classify(words, all_tags):
        """This nested function should return a list of tags, computed using a classifier with the weights passed as arguments to make_classifier_tagger and using basefeatures for each token (just the token and the offset)

        :param words: list of words
        :param all_tags: all possible tags
        :returns: list of tags
        :rtype: list

        """
        mylist = []
        for word in words:
            myCounter = Counter()
            myCounter[word] = 1
            #print(myCounter)
            ypred, pred_wt = clf_base.predict(myCounter, weights, all_tags)
            mylist.append(ypred)
        #print(mylist)
        return mylist