Python sklearn.multiclass.OneVsRestClassifier() Examples

The following are 30 code examples of sklearn.multiclass.OneVsRestClassifier(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.multiclass , or try the search function .
Example #1
Source File: test_multiclass.py    From Mastering-Elasticsearch-7.0 with MIT License 9 votes vote down vote up
def test_ovr_multilabel():
    # Toy dataset where features correspond directly to labels.
    X = np.array([[0, 4, 5], [0, 5, 0], [3, 3, 3], [4, 0, 6], [6, 0, 0]])
    y = np.array([[0, 1, 1],
                  [0, 1, 0],
                  [1, 1, 1],
                  [1, 0, 1],
                  [1, 0, 0]])

    for base_clf in (MultinomialNB(), LinearSVC(random_state=0),
                     LinearRegression(), Ridge(),
                     ElasticNet(), Lasso(alpha=0.5)):
        clf = OneVsRestClassifier(base_clf).fit(X, y)
        y_pred = clf.predict([[0, 4, 4]])[0]
        assert_array_equal(y_pred, [0, 1, 1])
        assert clf.multilabel_ 
Example #2
Source File: test_multiclass.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_ovr_multilabel_dataset():
    base_clf = MultinomialNB(alpha=1)
    for au, prec, recall in zip((True, False), (0.51, 0.66), (0.51, 0.80)):
        X, Y = datasets.make_multilabel_classification(n_samples=100,
                                                       n_features=20,
                                                       n_classes=5,
                                                       n_labels=2,
                                                       length=50,
                                                       allow_unlabeled=au,
                                                       random_state=0)
        X_train, Y_train = X[:80], Y[:80]
        X_test, Y_test = X[80:], Y[80:]
        clf = OneVsRestClassifier(base_clf).fit(X_train, Y_train)
        Y_pred = clf.predict(X_test)

        assert clf.multilabel_
        assert_almost_equal(precision_score(Y_test, Y_pred, average="micro"),
                            prec,
                            decimal=2)
        assert_almost_equal(recall_score(Y_test, Y_pred, average="micro"),
                            recall,
                            decimal=2) 
Example #3
Source File: test_multiclass.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_ovr_multiclass():
    # Toy dataset where features correspond directly to labels.
    X = np.array([[0, 0, 5], [0, 5, 0], [3, 0, 0], [0, 0, 6], [6, 0, 0]])
    y = ["eggs", "spam", "ham", "eggs", "ham"]
    Y = np.array([[0, 0, 1],
                  [0, 1, 0],
                  [1, 0, 0],
                  [0, 0, 1],
                  [1, 0, 0]])

    classes = set("ham eggs spam".split())

    for base_clf in (MultinomialNB(), LinearSVC(random_state=0),
                     LinearRegression(), Ridge(),
                     ElasticNet()):
        clf = OneVsRestClassifier(base_clf).fit(X, y)
        assert_equal(set(clf.classes_), classes)
        y_pred = clf.predict(np.array([[0, 0, 4]]))[0]
        assert_array_equal(y_pred, ["eggs"])

        # test input as label indicator matrix
        clf = OneVsRestClassifier(base_clf).fit(X, Y)
        y_pred = clf.predict([[0, 0, 4]])[0]
        assert_array_equal(y_pred, [0, 0, 1]) 
Example #4
Source File: test_multioutput.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_multiclass_multioutput_estimator():
    # test to check meta of meta estimators
    svc = LinearSVC(random_state=0)
    multi_class_svc = OneVsRestClassifier(svc)
    multi_target_svc = MultiOutputClassifier(multi_class_svc)

    multi_target_svc.fit(X, y)

    predictions = multi_target_svc.predict(X)
    assert_equal((n_samples, n_outputs), predictions.shape)

    # train the forest with each column and assert that predictions are equal
    for i in range(3):
        multi_class_svc_ = clone(multi_class_svc)  # create a clone
        multi_class_svc_.fit(X, y[:, i])
        assert_equal(list(multi_class_svc_.predict(X)),
                     list(predictions[:, i])) 
Example #5
Source File: test_multiclass.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_ovr_ovo_regressor():
    # test that ovr and ovo work on regressors which don't have a decision_
    # function
    ovr = OneVsRestClassifier(DecisionTreeRegressor())
    pred = ovr.fit(iris.data, iris.target).predict(iris.data)
    assert_equal(len(ovr.estimators_), n_classes)
    assert_array_equal(np.unique(pred), [0, 1, 2])
    # we are doing something sensible
    assert_greater(np.mean(pred == iris.target), .9)

    ovr = OneVsOneClassifier(DecisionTreeRegressor())
    pred = ovr.fit(iris.data, iris.target).predict(iris.data)
    assert_equal(len(ovr.estimators_), n_classes * (n_classes - 1) / 2)
    assert_array_equal(np.unique(pred), [0, 1, 2])
    # we are doing something sensible
    assert_greater(np.mean(pred == iris.target), .9) 
Example #6
Source File: test_multioutput.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_classifier_chain_vs_independent_models():
    # Verify that an ensemble of classifier chains (each of length
    # N) can achieve a higher Jaccard similarity score than N independent
    # models
    X, Y = generate_multilabel_dataset_with_correlations()
    X_train = X[:600, :]
    X_test = X[600:, :]
    Y_train = Y[:600, :]
    Y_test = Y[600:, :]

    ovr = OneVsRestClassifier(LogisticRegression())
    ovr.fit(X_train, Y_train)
    Y_pred_ovr = ovr.predict(X_test)

    chain = ClassifierChain(LogisticRegression())
    chain.fit(X_train, Y_train)
    Y_pred_chain = chain.predict(X_test)

    assert_greater(jaccard_score(Y_test, Y_pred_chain, average='samples'),
                   jaccard_score(Y_test, Y_pred_ovr, average='samples')) 
Example #7
Source File: test_multiclass.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_ovr_fit_predict():
    # A classifier which implements decision_function.
    ovr = OneVsRestClassifier(LinearSVC(random_state=0))
    pred = ovr.fit(iris.data, iris.target).predict(iris.data)
    assert_equal(len(ovr.estimators_), n_classes)

    clf = LinearSVC(random_state=0)
    pred2 = clf.fit(iris.data, iris.target).predict(iris.data)
    assert_equal(np.mean(iris.target == pred), np.mean(iris.target == pred2))

    # A classifier which implements predict_proba.
    ovr = OneVsRestClassifier(MultinomialNB())
    pred = ovr.fit(iris.data, iris.target).predict(iris.data)
    assert_greater(np.mean(iris.target == pred), 0.65)


# 0.23. warning about tol not having its correct default value. 
Example #8
Source File: test_multiclass.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_ovr_single_label_predict_proba():
    base_clf = MultinomialNB(alpha=1)
    X, Y = iris.data, iris.target
    X_train, Y_train = X[:80], Y[:80]
    X_test = X[80:]
    clf = OneVsRestClassifier(base_clf).fit(X_train, Y_train)

    # Decision function only estimator.
    decision_only = OneVsRestClassifier(svm.SVR(gamma='scale')
                                        ).fit(X_train, Y_train)
    assert not hasattr(decision_only, 'predict_proba')

    Y_pred = clf.predict(X_test)
    Y_proba = clf.predict_proba(X_test)

    assert_almost_equal(Y_proba.sum(axis=1), 1.0)
    # predict assigns a label if the probability that the
    # sample has the label is greater than 0.5.
    pred = np.array([l.argmax() for l in Y_proba])
    assert not (pred - Y_pred).any() 
Example #9
Source File: conv_sup_cc_lbp.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, f_train, y_train, X_test, a_test, f_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, f_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf', C = 1.0, gamma = 0.01, probability = True)).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, f_test, y_test);
    Pr = model.predict(Er);
    Or = model.predict_proba(Er); 
Example #10
Source File: conv_sup_cc_mllsll.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #11
Source File: conv_sup_cc_mllsll.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #12
Source File: conv_sup_cc.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #13
Source File: conv_sup_cc_lbp.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, f_train, y_train, X_test, a_test, f_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, f_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf', C = 1.0, gamma = 0.01, probability = True)).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, f_test, y_test);
    Pr = model.predict(Er);
    Or = model.predict_proba(Er); 
Example #14
Source File: conv_sup_cc_4ch.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #15
Source File: conv_sup_cc.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #16
Source File: conv_sup_cc_lbp.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_lbp_br(C, gamma, f_train, y_train, f_test, y_test):
    model = OneVsRestClassifier(SVC(kernel = 'rbf', C = C, gamma = gamma)).fit(f_train, y_train);
    y_predict = model.predict(f_test);
    #y_output = model.predict_proba(f_test);
    y_output = y_predict;
    return y_predict, y_output; 
Example #17
Source File: conv_sup_cc_mllsll.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #18
Source File: conv_sup_cc_4ch.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #19
Source File: conv_sup_cc.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #20
Source File: conv_sup_cc_lbp.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_lbp_br(C, gamma, f_train, y_train, f_test, y_test):
    model = OneVsRestClassifier(SVC(kernel = 'rbf', C = C, gamma = gamma)).fit(f_train, y_train);
    y_predict = model.predict(f_test);
    #y_output = model.predict_proba(f_test);
    y_output = y_predict;
    return y_predict, y_output; 
Example #21
Source File: conv_sup_cc_mllsll.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #22
Source File: conv_sup_cc.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #23
Source File: conv_sup_cc_lbp.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_lbp_br(C, gamma, f_train, y_train, f_test, y_test):
    model = OneVsRestClassifier(SVC(kernel = 'rbf', C = C, gamma = gamma)).fit(f_train, y_train);
    y_predict = model.predict(f_test);
    #y_output = model.predict_proba(f_test);
    y_output = y_predict;
    return y_predict, y_output; 
Example #24
Source File: conv_sup_cc_lbp.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, f_train, y_train, X_test, a_test, f_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, f_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf', C = 1.0, gamma = 0.01, probability = True)).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, f_test, y_test);
    Pr = model.predict(Er);
    Or = model.predict_proba(Er); 
Example #25
Source File: conv_sup_cc_mllsll.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #26
Source File: conv_sup_cc.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #27
Source File: conv_sup_cc_lbp.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_lbp_br(C, gamma, f_train, y_train, f_test, y_test):
    model = OneVsRestClassifier(SVC(kernel = 'rbf', C = C, gamma = gamma)).fit(f_train, y_train);
    y_predict = model.predict(f_test);
    #y_output = model.predict_proba(f_test);
    y_output = y_predict;
    return y_predict, y_output; 
Example #28
Source File: conv_sup_cc_lbp.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, f_train, y_train, X_test, a_test, f_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, f_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf', C = 1.0, gamma = 0.01, probability = True)).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, f_test, y_test);
    Pr = model.predict(Er);
    Or = model.predict_proba(Er); 
Example #29
Source File: conv_sup_cc_mllsll.py    From u24_lymphocyte with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def svm_br(classn, X_train, a_train, y_train, X_test, a_test, y_test):
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_train, a_train, y_train);
    model = OneVsRestClassifier(SVC(kernel = 'rbf')).fit(Er, Tr);
    _, _, _, Er, _, _, Tr = val_fn_epoch(classn, val_fn, X_test, a_test, y_test);
    Pr = model.predict(Er);
    print("[SVM] Hamming: {:.4f}\tAccuracy: {:.4f}".format(1 - hamming_loss(Tr, Pr), accuracy_score(Tr, Pr))); 
Example #30
Source File: multilabel.py    From modAL with MIT License 5 votes vote down vote up
def avg_confidence(classifier: OneVsRestClassifier, X_pool: modALinput,
                   n_instances: int = 1, random_tie_break: bool = False) -> Tuple[np.ndarray, modALinput]:
    """
    AvgConfidence query strategy for multilabel classification.

    For more details on this query strategy, see
    Esuli and Sebastiani., Active Learning Strategies for Multi-Label Text Classification
    (http://dx.doi.org/10.1007/978-3-642-00958-7_12)

    Args:
        classifier: The multilabel classifier for which the labels are to be queried.
        X_pool: The pool of samples to query from.
        random_tie_break: If True, shuffles utility scores to randomize the order. This
            can be used to break the tie when the highest utility score is not unique.

    Returns:
        The index of the instance from X_pool chosen to be labelled;
        the instance from X_pool chosen to be labelled.
    """

    classwise_confidence = classifier.predict_proba(X_pool)
    classwise_mean = np.mean(classwise_confidence, axis=1)

    if not random_tie_break:
        query_idx = multi_argmax(classwise_mean, n_instances)
    else:
        query_idx = shuffled_argmax(classwise_mean, n_instances)

    return query_idx, X_pool[query_idx]