Python sklearn.multiclass.OneVsOneClassifier() Examples
The following are 30
code examples of sklearn.multiclass.OneVsOneClassifier().
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 | 6 votes |
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 #2
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 6 votes |
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 #3
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_ovo_ties(): # Test that ties are broken using the decision function, # not defaulting to the smallest label X = np.array([[1, 2], [2, 1], [-2, 1], [-2, -1]]) y = np.array([2, 0, 1, 2]) multi_clf = OneVsOneClassifier(Perceptron(shuffle=False, max_iter=4, tol=None)) ovo_prediction = multi_clf.fit(X, y).predict(X) ovo_decision = multi_clf.decision_function(X) # Classifiers are in order 0-1, 0-2, 1-2 # Use decision_function to compute the votes and the normalized # sum_of_confidences, which is used to disambiguate when there is a tie in # votes. votes = np.round(ovo_decision) normalized_confidences = ovo_decision - votes # For the first point, there is one vote per class assert_array_equal(votes[0, :], 1) # For the rest, there is no tie and the prediction is the argmax assert_array_equal(np.argmax(votes[1:], axis=1), ovo_prediction[1:]) # For the tie, the prediction is the class with the highest score assert_equal(ovo_prediction[0], normalized_confidences[0].argmax())
Example #4
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_pairwise_attribute(): clf_precomputed = svm.SVC(kernel='precomputed') clf_notprecomputed = svm.SVC() for MultiClassClassifier in [OneVsRestClassifier, OneVsOneClassifier]: ovr_false = MultiClassClassifier(clf_notprecomputed) assert_false(ovr_false._pairwise) ovr_true = MultiClassClassifier(clf_precomputed) assert_true(ovr_true._pairwise)
Example #5
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_pairwise_indices(): clf_precomputed = svm.SVC(kernel='precomputed') X, y = iris.data, iris.target ovr_false = OneVsOneClassifier(clf_precomputed) linear_kernel = np.dot(X, X.T) ovr_false.fit(linear_kernel, y) n_estimators = len(ovr_false.estimators_) precomputed_indices = ovr_false.pairwise_indices_ for idx in precomputed_indices: assert_equal(idx.shape[0] * n_estimators / (n_estimators - 1), linear_kernel.shape[0])
Example #6
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_ovo_one_class(): # Test error for OvO with one class X = np.eye(4) y = np.array(['a'] * 4) ovo = OneVsOneClassifier(LinearSVC()) assert_raise_message(ValueError, "when only one class", ovo.fit, X, y)
Example #7
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_ovo_string_y(): # Test that the OvO doesn't mess up the encoding of string labels X = np.eye(4) y = np.array(['a', 'b', 'c', 'd']) ovo = OneVsOneClassifier(LinearSVC()) ovo.fit(X, y) assert_array_equal(y, ovo.predict(X))
Example #8
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_ovo_ties2(): # test that ties can not only be won by the first two labels X = np.array([[1, 2], [2, 1], [-2, 1], [-2, -1]]) y_ref = np.array([2, 0, 1, 2]) # cycle through labels so that each label wins once for i in range(3): y = (y_ref + i) % 3 multi_clf = OneVsOneClassifier(Perceptron(shuffle=False, max_iter=4, tol=None)) ovo_prediction = multi_clf.fit(X, y).predict(X) assert_equal(ovo_prediction[0], i % 3)
Example #9
Source File: komd.py From MKLpy with GNU General Public License v3.0 | 5 votes |
def _one_vs_one(self,X,Y): self.cls = OneVsOneClassifier(KOMD(**self.get_params())).fit(X,Y) self.is_fitted = True return self
Example #10
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_ovo_gridsearch(): ovo = OneVsOneClassifier(LinearSVC(random_state=0)) Cs = [0.1, 0.5, 0.8] cv = GridSearchCV(ovo, {'estimator__C': Cs}) cv.fit(iris.data, iris.target) best_C = cv.best_estimator_.estimators_[0].C assert_true(best_C in Cs)
Example #11
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_ovo_fit_predict(): # A classifier which implements decision_function. ovo = OneVsOneClassifier(LinearSVC(random_state=0)) ovo.fit(iris.data, iris.target).predict(iris.data) assert_equal(len(ovo.estimators_), n_classes * (n_classes - 1) / 2) # A classifier which implements predict_proba. ovo = OneVsOneClassifier(MultinomialNB()) ovo.fit(iris.data, iris.target).predict(iris.data) assert_equal(len(ovo.estimators_), n_classes * (n_classes - 1) / 2)
Example #12
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_ovo_fit_on_list(): # Test that OneVsOne fitting works with a list of targets and yields the # same output as predict from an array ovo = OneVsOneClassifier(LinearSVC(random_state=0)) prediction_from_array = ovo.fit(iris.data, iris.target).predict(iris.data) iris_data_list = [list(a) for a in iris.data] prediction_from_list = ovo.fit(iris_data_list, list(iris.target)).predict(iris_data_list) assert_array_equal(prediction_from_array, prediction_from_list)
Example #13
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_ovo_exceptions(): ovo = OneVsOneClassifier(LinearSVC(random_state=0)) assert_raises(ValueError, ovo.predict, [])
Example #14
Source File: test_multiclass.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_pairwise_cross_val_score(): clf_precomputed = svm.SVC(kernel='precomputed') clf_notprecomputed = svm.SVC(kernel='linear') X, y = iris.data, iris.target for MultiClassClassifier in [OneVsRestClassifier, OneVsOneClassifier]: ovr_false = MultiClassClassifier(clf_notprecomputed) ovr_true = MultiClassClassifier(clf_precomputed) linear_kernel = np.dot(X, X.T) score_precomputed = cross_val_score(ovr_true, linear_kernel, y) score_linear = cross_val_score(ovr_false, X, y) assert_array_equal(score_precomputed, score_linear)
Example #15
Source File: ABuMLCreater.py From abu with GNU General Public License v3.0 | 5 votes |
def onevsone_classifier(self, assign=False, **kwargs): """ 封装有监督学习分类器,使用OneVsOneClassifier进行多label的 分类器二次封装,即: OneVsOneClassifier(self.clf, **kwargs) :param assign: 是否保存实例后的二次封装分类器对象,与其它构造器不同, 默认False,即默认不保存在类中替换原始分类器 :param kwargs: 透传OneVsOneClassifier做为构造关键字参数 :return: OneVsOneClassifier对象 """ onevsone = OneVsOneClassifier(self.clf, **kwargs) if assign: self.clf = onevsone return onevsone
Example #16
Source File: test_multiclass.py From pandas-ml with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_objectmapper(self): df = pdml.ModelFrame([]) self.assertIs(df.multiclass.OneVsRestClassifier, multiclass.OneVsRestClassifier) self.assertIs(df.multiclass.OneVsOneClassifier, multiclass.OneVsOneClassifier) self.assertIs(df.multiclass.OutputCodeClassifier, multiclass.OutputCodeClassifier)
Example #17
Source File: linear_model.py From scikit-lego with MIT License | 5 votes |
def __new__(cls, *args, multi_class="ovr", n_jobs=1, **kwargs): multiclass_meta = {"ovr": OneVsRestClassifier, "ovo": OneVsOneClassifier}[ multi_class ] return multiclass_meta( _EqualOpportunityClassifier(*args, **kwargs), n_jobs=n_jobs )
Example #18
Source File: test_multiclass.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_ovo_exceptions(): ovo = OneVsOneClassifier(LinearSVC(random_state=0)) assert_raises(ValueError, ovo.predict, [])
Example #19
Source File: training.py From OpenCV-3-x-with-Python-By-Example with MIT License | 5 votes |
def __init__(self, X, label_words): # Encoding the labels (words to numbers) self.le = preprocessing.LabelEncoder() # Initialize One vs One Classifier using a linear kernel self.clf = OneVsOneClassifier(LinearSVC(random_state=0)) y = self._encodeLabels(label_words) X = np.asarray(X) self.clf.fit(X, y) # Predict the output class for the input datapoint
Example #20
Source File: OneVsRest.py From mHTM with MIT License | 5 votes |
def main(): """ Use a linear SVM for multi-class classification. One vs the rest : 77.61% Default : 77.61% One vs one : 85.07% """ seed = 123456789 np.random.seed(seed) ntrain, ntest = 800, 200 (tr_x, tr_y), (te_x, te_y) = load_mnist() x, y = np.vstack((tr_x, te_x)), np.hstack((tr_y, te_y)) cv = MNISTCV(tr_y, te_y, ntrain, ntest, 1, seed) for tr, te in cv: clf = OneVsRestClassifier(LinearSVC(random_state=seed), -1) clf.fit(x[tr], y[tr]) print clf.score(x[te], y[te]) clf = LinearSVC(random_state=seed) clf.fit(x[tr], y[tr]) print clf.score(x[te], y[te]) clf = OneVsOneClassifier(LinearSVC(random_state=seed), -1) clf.fit(x[tr], y[tr]) print clf.score(x[te], y[te])
Example #21
Source File: test_multiclass.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_pairwise_cross_val_score(): clf_precomputed = svm.SVC(kernel='precomputed') clf_notprecomputed = svm.SVC(kernel='linear') X, y = iris.data, iris.target for MultiClassClassifier in [OneVsRestClassifier, OneVsOneClassifier]: ovr_false = MultiClassClassifier(clf_notprecomputed) ovr_true = MultiClassClassifier(clf_precomputed) linear_kernel = np.dot(X, X.T) score_precomputed = cross_val_score(ovr_true, linear_kernel, y) score_linear = cross_val_score(ovr_false, X, y) assert_array_equal(score_precomputed, score_linear)
Example #22
Source File: test_multiclass.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_pairwise_attribute(): clf_precomputed = svm.SVC(kernel='precomputed') clf_notprecomputed = svm.SVC() for MultiClassClassifier in [OneVsRestClassifier, OneVsOneClassifier]: ovr_false = MultiClassClassifier(clf_notprecomputed) assert not ovr_false._pairwise ovr_true = MultiClassClassifier(clf_precomputed) assert ovr_true._pairwise
Example #23
Source File: test_multiclass.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_pairwise_indices(): clf_precomputed = svm.SVC(kernel='precomputed') X, y = iris.data, iris.target ovr_false = OneVsOneClassifier(clf_precomputed) linear_kernel = np.dot(X, X.T) ovr_false.fit(linear_kernel, y) n_estimators = len(ovr_false.estimators_) precomputed_indices = ovr_false.pairwise_indices_ for idx in precomputed_indices: assert_equal(idx.shape[0] * n_estimators / (n_estimators - 1), linear_kernel.shape[0])
Example #24
Source File: test_multiclass.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_ovo_one_class(): # Test error for OvO with one class X = np.eye(4) y = np.array(['a'] * 4) ovo = OneVsOneClassifier(LinearSVC()) assert_raise_message(ValueError, "when only one class", ovo.fit, X, y)
Example #25
Source File: linear_model.py From scikit-lego with MIT License | 5 votes |
def __new__(cls, *args, multi_class="ovr", n_jobs=1, **kwargs): multiclass_meta = {"ovr": OneVsRestClassifier, "ovo": OneVsOneClassifier}[ multi_class ] return multiclass_meta( _DemographicParityClassifer(*args, **kwargs), n_jobs=n_jobs )
Example #26
Source File: test_multiclass.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_ovo_string_y(): # Test that the OvO doesn't mess up the encoding of string labels X = np.eye(4) y = np.array(['a', 'b', 'c', 'd']) ovo = OneVsOneClassifier(LinearSVC()) ovo.fit(X, y) assert_array_equal(y, ovo.predict(X))
Example #27
Source File: test_multiclass.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_ovo_ties2(): # test that ties can not only be won by the first two labels X = np.array([[1, 2], [2, 1], [-2, 1], [-2, -1]]) y_ref = np.array([2, 0, 1, 2]) # cycle through labels so that each label wins once for i in range(3): y = (y_ref + i) % 3 multi_clf = OneVsOneClassifier(Perceptron(shuffle=False, max_iter=4, tol=None)) ovo_prediction = multi_clf.fit(X, y).predict(X) assert_equal(ovo_prediction[0], i % 3)
Example #28
Source File: test_multiclass.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_ovo_ties(): # Test that ties are broken using the decision function, # not defaulting to the smallest label X = np.array([[1, 2], [2, 1], [-2, 1], [-2, -1]]) y = np.array([2, 0, 1, 2]) multi_clf = OneVsOneClassifier(Perceptron(shuffle=False, max_iter=4, tol=None)) ovo_prediction = multi_clf.fit(X, y).predict(X) ovo_decision = multi_clf.decision_function(X) # Classifiers are in order 0-1, 0-2, 1-2 # Use decision_function to compute the votes and the normalized # sum_of_confidences, which is used to disambiguate when there is a tie in # votes. votes = np.round(ovo_decision) normalized_confidences = ovo_decision - votes # For the first point, there is one vote per class assert_array_equal(votes[0, :], 1) # For the rest, there is no tie and the prediction is the argmax assert_array_equal(np.argmax(votes[1:], axis=1), ovo_prediction[1:]) # For the tie, the prediction is the class with the highest score assert_equal(ovo_prediction[0], normalized_confidences[0].argmax()) # 0.23. warning about tol not having its correct default value.
Example #29
Source File: test_multiclass.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_ovo_gridsearch(): ovo = OneVsOneClassifier(LinearSVC(random_state=0)) Cs = [0.1, 0.5, 0.8] cv = GridSearchCV(ovo, {'estimator__C': Cs}) cv.fit(iris.data, iris.target) best_C = cv.best_estimator_.estimators_[0].C assert best_C in Cs # 0.23. warning about tol not having its correct default value.
Example #30
Source File: test_multiclass.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_ovo_fit_on_list(): # Test that OneVsOne fitting works with a list of targets and yields the # same output as predict from an array ovo = OneVsOneClassifier(LinearSVC(random_state=0)) prediction_from_array = ovo.fit(iris.data, iris.target).predict(iris.data) iris_data_list = [list(a) for a in iris.data] prediction_from_list = ovo.fit(iris_data_list, list(iris.target)).predict(iris_data_list) assert_array_equal(prediction_from_array, prediction_from_list)