Exemplo n.º 1
0
def test_perceptron():
    X = [[0, 1, 0],
         [0, 1, 0],
         [1, 0, 0],
         [0, 1, 0],
         [1, 0, 0],
         [0, 0, 1],
         [0, 0, 1],
         [0, 1, 0],
         [1, 0, 0],
         [1, 0, 0]]

    y = [0, 0, 0, 0, 0, 1, 1, 0, 2, 2]

    clf = StructuredPerceptron(verbose=False, random_state=37, max_iter=15)
    clf.fit(X, y, [len(y)])
    assert_array_equal(y, clf.predict(X))

    # Try again with string labels and sparse input.
    y_str = np.array(["eggs", "ham", "spam"])[y]

    clf = clone(clf)
    clf.fit(csc_matrix(X), y_str, [len(y_str)])
    assert_array_equal(y_str, clf.predict(coo_matrix(X)))

    X2 = np.vstack([X, X])
    y2 = np.hstack([y_str, y_str])
    assert_array_equal(y2, clf.predict(X2, lengths=[len(y), len(y)]))

    # Train with Viterbi, test with best-first to make StructuredPerceptron
    # behave a bit more like a linear model.
    clf.fit(X, y, [len(y)])
    clf.set_params(decode="bestfirst")
    y_linearmodel = np.dot(X, clf.coef_.T).argmax(axis=1)
    assert_array_equal(clf.predict(X), y_linearmodel)