本文整理汇总了Python中sklearn.multiclass.OneVsOneClassifier.fit方法的典型用法代码示例。如果您正苦于以下问题:Python OneVsOneClassifier.fit方法的具体用法?Python OneVsOneClassifier.fit怎么用?Python OneVsOneClassifier.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.multiclass.OneVsOneClassifier
的用法示例。
在下文中一共展示了OneVsOneClassifier.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: svm_training
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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
示例2: test_ovo_partial_fit_predict
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
def test_ovo_partial_fit_predict():
X, y = shuffle(iris.data, iris.target)
ovo1 = OneVsOneClassifier(MultinomialNB())
ovo1.partial_fit(X[:100], y[:100], np.unique(y))
ovo1.partial_fit(X[100:], y[100:])
pred1 = ovo1.predict(X)
ovo2 = OneVsOneClassifier(MultinomialNB())
ovo2.fit(X, y)
pred2 = ovo2.predict(X)
assert_equal(len(ovo1.estimators_), n_classes * (n_classes - 1) / 2)
assert_greater(np.mean(y == pred1), 0.65)
assert_almost_equal(pred1, pred2)
# Test when mini-batches don't have all target classes
ovo1 = OneVsOneClassifier(MultinomialNB())
ovo1.partial_fit(iris.data[:60], iris.target[:60], np.unique(iris.target))
ovo1.partial_fit(iris.data[60:], iris.target[60:])
pred1 = ovo1.predict(iris.data)
ovo2 = OneVsOneClassifier(MultinomialNB())
pred2 = ovo2.fit(iris.data, iris.target).predict(iris.data)
assert_almost_equal(pred1, pred2)
assert_equal(len(ovo1.estimators_), len(np.unique(iris.target)))
assert_greater(np.mean(iris.target == pred1), 0.65)
示例3: gen_svc
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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
示例4: train_classifier
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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_fit_on_list
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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)
示例6: test_ovo_string_y
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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))
示例7: gen_svc
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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
示例8: test_ovo_string_y
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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))
示例9: test_ovo_fit_predict
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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)
示例10: OneVsOne
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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
示例11: svm
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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
示例12: test_pairwise_indices
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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])
示例13: test_ovo_partial_fit_predict
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
def test_ovo_partial_fit_predict():
temp = datasets.load_iris()
X, y = temp.data, temp.target
ovo1 = OneVsOneClassifier(MultinomialNB())
ovo1.partial_fit(X[:100], y[:100], np.unique(y))
ovo1.partial_fit(X[100:], y[100:])
pred1 = ovo1.predict(X)
ovo2 = OneVsOneClassifier(MultinomialNB())
ovo2.fit(X, y)
pred2 = ovo2.predict(X)
assert_equal(len(ovo1.estimators_), n_classes * (n_classes - 1) / 2)
assert_greater(np.mean(y == pred1), 0.65)
assert_almost_equal(pred1, pred2)
# Test when mini-batches have binary target classes
ovo1 = OneVsOneClassifier(MultinomialNB())
ovo1.partial_fit(X[:60], y[:60], np.unique(y))
ovo1.partial_fit(X[60:], y[60:])
pred1 = ovo1.predict(X)
ovo2 = OneVsOneClassifier(MultinomialNB())
pred2 = ovo2.fit(X, y).predict(X)
assert_almost_equal(pred1, pred2)
assert_equal(len(ovo1.estimators_), len(np.unique(y)))
assert_greater(np.mean(y == pred1), 0.65)
ovo = OneVsOneClassifier(MultinomialNB())
X = np.random.rand(14, 2)
y = [1, 1, 2, 3, 3, 0, 0, 4, 4, 4, 4, 4, 2, 2]
ovo.partial_fit(X[:7], y[:7], [0, 1, 2, 3, 4])
ovo.partial_fit(X[7:], y[7:])
pred = ovo.predict(X)
ovo2 = OneVsOneClassifier(MultinomialNB())
pred2 = ovo2.fit(X, y).predict(X)
assert_almost_equal(pred, pred2)
# raises error when mini-batch does not have classes from all_classes
ovo = OneVsOneClassifier(MultinomialNB())
error_y = [0, 1, 2, 3, 4, 5, 2]
message_re = escape("Mini-batch contains {0} while "
"it must be subset of {1}".format(np.unique(error_y),
np.unique(y)))
assert_raises_regexp(ValueError, message_re, ovo.partial_fit, X[:7],
error_y, np.unique(y))
# test partial_fit only exists if estimator has it:
ovr = OneVsOneClassifier(SVC())
assert_false(hasattr(ovr, "partial_fit"))
示例14: test_ovo_ties
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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])
示例15: svm_classification
# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import fit [as 别名]
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")