本文整理汇总了Python中sklearn.multiclass.OneVsOneClassifier类的典型用法代码示例。如果您正苦于以下问题:Python OneVsOneClassifier类的具体用法?Python OneVsOneClassifier怎么用?Python OneVsOneClassifier使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OneVsOneClassifier类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gen_svc
def gen_svc(train_model):
'''Given a training model, generates the SVM (and DictVectorizer) for it
Args:
train_model: a training model object. should have 2 attributes:
feature_lists, a map from POS tag to a dictionary of features
(the ones used in the ith decision), and action_lists, a map from
POS tag to the action (Shift, Left, Right) chosen for the ith decision
Returns: dictionary mapping POS tag to a vectorizer, SVM tuple
Raises: None
'''
models = {}
for pos_tag in train_model.feature_lists:
vec = DictVectorizer()
feature_mat = vec.fit_transform(train_model.feature_lists[pos_tag])
trained_svc = OneVsOneClassifier(LinearSVC())
try:
trained_svc.fit(feature_mat, np.array(train_model.action_lists[pos_tag]))
except ValueError:
# occasionally we get the same action for everything with a
# particular POS, which raises an error. so in that case we just
# use a custom class that always predicts the same action
trained_svc = AlwaysPredict(train_model.feature_lists[pos_tag][0])
models[pos_tag] = (vec, trained_svc)
return models
示例2: svm_training
def svm_training(train_X,train_Y,kernel):
if kernel == False:
clf = OneVsOneClassifier(svm.LinearSVC(random_state=0))
else:
clf = OneVsOneClassifier(svm.SVC(kernel='rbf'))
clf.fit(train_X,train_Y)
return clf
示例3: svm_classification
def svm_classification(genres, features_type):
training_set_features = tf.read_features_from_files("../../music/training", genres, features_type)
testing_set_features = tf.read_features_from_files("../../music/testing", genres, features_type)
X = []
y = []
for feature in training_set_features:
(mean, cov_mat, genre_name) = feature
X.append(mean.tolist())
y.append(tf.get_genre_ID(genre_name))
training_data = np.array(X)
training_class = np.array(y)
X = []
y = []
for feature in testing_set_features:
(mean, cov_mat, genre_name) = feature
X.append(mean.tolist())
y.append(tf.get_genre_ID(genre_name))
testing_data = np.array(X)
testing_class = np.array(y)
clf = OneVsOneClassifier(SVC(kernel='linear'))
result_class = np.array(clf.fit(training_data, training_class).predict(testing_data))
rt.print_accuracy(list(testing_class), list(result_class), genres, features_type, "svm")
rt.write_accuracy_to_file("../../music/", list(testing_class), list(result_class), genres, features_type, "svm")
示例4: train_classifier
def train_classifier(clf,X_train,y_train,X_test,y_test):
clf = OneVsOneClassifier(clf)
clf.fit(X_train, y_train)
train_time = time() - t0
print("train time: %0.3fs" % train_time)
t0 = time()
return clf
示例5: test_ovo_ties
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())
ovo_prediction = multi_clf.fit(X, y).predict(X)
# recalculate votes to make sure we have a tie
predictions = np.vstack([clf.predict(X) for clf in multi_clf.estimators_])
scores = np.vstack([clf.decision_function(X)
for clf in multi_clf.estimators_])
# classifiers are in order 0-1, 0-2, 1-2
# aggregate votes:
votes = np.zeros((4, 3))
votes[np.arange(4), predictions[0]] += 1
votes[np.arange(4), 2 * predictions[1]] += 1
votes[np.arange(4), 1 + predictions[2]] += 1
# 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], 0)
# in the zero-one classifier, the score for 0 is greater than the score for
# one.
assert_greater(scores[0][0], scores[0][1])
# score for one is greater than score for zero
assert_greater(scores[2, 0] - scores[0, 0], scores[0, 0] + scores[1, 0])
# score for one is greater than score for two
assert_greater(scores[2, 0] - scores[0, 0], -scores[1, 0] - scores[2, 0])
示例6: test_ovo_fit_on_list
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)
prediction_from_list = ovo.fit(iris.data,
list(iris.target)).predict(iris.data)
assert_array_equal(prediction_from_array, prediction_from_list)
示例7: test_ovo_string_y
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))
示例8: gen_svc
def gen_svc(train_model):
'''Given a training model, generates the SVM (and DictVectorizer) for it'''
vec = DictVectorizer()
feature_mat = vec.fit_transform(train_model.feature_list)
# for some reason just SVC() seems to always suggest "Shift"
trained_svc = OneVsOneClassifier(LinearSVC())
trained_svc.fit(feature_mat, np.array(train_model.action_list))
return vec, trained_svc
示例9: test_ovo_string_y
def test_ovo_string_y():
"Test that the OvO doesn't screw the encoding of string labels"
X = np.eye(4)
y = np.array(['a', 'b', 'c', 'd'])
svc = LinearSVC()
ovo = OneVsOneClassifier(svc)
ovo.fit(X, y)
assert_array_equal(y, ovo.predict(X))
示例10: __init__
def __init__(self, estimator, n_jobs=-1, n_neighbors=18, radius=1.0,
algorithm='auto', leaf_size=30, metric='minkowski',
p=2, threshold=0.2, metric_params=None):
OneVsOneClassifier.__init__(self, estimator, n_jobs)
self.nbrs = NearestNeighbors(n_neighbors=n_neighbors, radius=radius, algorithm=algorithm,
leaf_size=leaf_size, metric=metric, p=p,
metric_params=metric_params, n_jobs=n_jobs)
self.n_neighbors = n_neighbors
self.threshold = threshold
self._fit_y = None
示例11: test_ovo_fit_predict
def test_ovo_fit_predict():
# A classifier which implements decision_function.
ovo = OneVsOneClassifier(LinearSVC())
pred = 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())
pred = ovo.fit(iris.data, iris.target).predict(iris.data)
assert_equal(len(ovo.estimators_), n_classes * (n_classes - 1) / 2)
示例12: OneVsOne
def OneVsOne(inputs_train, inputs_valid, target_train, target_valid):
name = "Multiclass One Vs One"
clf = OneVsOneClassifier(LinearSVC(random_state=0))
clf.fit(inputs_train, np.ravel(target_train))
prediction = clf.predict(inputs_valid)
correct = np.count_nonzero(np.ravel(target_valid) == prediction)
total = target_valid.shape[0]
correctRate = (float(correct)/total)*100
return name, correctRate
示例13: test_ovo_ties2
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())
ovo_prediction = multi_clf.fit(X, y).predict(X)
assert_equal(ovo_prediction[0], i % 3)
示例14: fit
def fit(self, X, y):
"""Fit Gaussian process classification model
Parameters
----------
X : array-like, shape = (n_samples, n_features)
Training data
y : array-like, shape = (n_samples,)
Target values, must be binary
Returns
-------
self : returns an instance of self.
"""
X, y = check_X_y(X, y, multi_output=False)
self.base_estimator_ = _BinaryGaussianProcessClassifierLaplace(
self.kernel, self.optimizer, self.n_restarts_optimizer,
self.max_iter_predict, self.warm_start, self.copy_X_train,
self.random_state)
self.classes_ = np.unique(y)
self.n_classes_ = self.classes_.size
if self.n_classes_ == 1:
raise ValueError("GaussianProcessClassifier requires 2 or more "
"distinct classes; got %d class (only class %s "
"is present)"
% (self.n_classes_, self.classes_[0]))
if self.n_classes_ > 2:
if self.multi_class == "one_vs_rest":
self.base_estimator_ = \
OneVsRestClassifier(self.base_estimator_,
n_jobs=self.n_jobs)
elif self.multi_class == "one_vs_one":
self.base_estimator_ = \
OneVsOneClassifier(self.base_estimator_,
n_jobs=self.n_jobs)
else:
raise ValueError("Unknown multi-class mode %s"
% self.multi_class)
self.base_estimator_.fit(X, y)
if self.n_classes_ > 2:
self.log_marginal_likelihood_value_ = np.mean(
[estimator.log_marginal_likelihood()
for estimator in self.base_estimator_.estimators_])
else:
self.log_marginal_likelihood_value_ = \
self.base_estimator_.log_marginal_likelihood()
return self
示例15: svm
def svm(X,Y):
X_train = np.array([x for i, x in enumerate(X) if i % 7 != 0], dtype = np.uint8)
y_train = np.array([z for i, z in enumerate(Y) if i % 7 != 0], dtype = np.uint8)
X_test = np.array([x for i, x in enumerate(X) if i % 10 == 0], dtype = np.uint8)
y_test = np.array([z for i, z in enumerate(Y) if i % 10 == 0], dtype = np.uint8)
clf = OneVsOneClassifier(LinearSVC(random_state=0))
clf.fit(X_train, y_train)
y_predicted = rf.predict(X_test)
results = [prediction == truth for prediction, truth in zip(y_predicted, y_test)]
accuracy = float(results.count(True)) / float(len(results))
print accuracy