本文整理汇总了Python中sklearn.multiclass.OneVsOneClassifier类的典型用法代码示例。如果您正苦于以下问题:Python OneVsOneClassifier类的具体用法?Python OneVsOneClassifier怎么用?Python OneVsOneClassifier使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OneVsOneClassifier类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:lurke,项目名称:DependencyParsing,代码行数:25,代码来源:master.py
示例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
开发者ID:akhilbatra898,项目名称:SentimentAnalysisOfTwitter,代码行数:7,代码来源:unigramSVM.py
示例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")
开发者ID:vladimir-paramuzov,项目名称:MGC-Project,代码行数:30,代码来源:svc.py
示例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
开发者ID:AvinashKalivarapu,项目名称:SentimentAnalysisOfTwitter,代码行数:7,代码来源:svm_classifier.py
示例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])
开发者ID:jaguila,项目名称:cert,代码行数:31,代码来源:test_multiclass.py
示例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)
开发者ID:Anuragch,项目名称:scikit-learn,代码行数:8,代码来源:test_multiclass.py
示例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))
开发者ID:Anuragch,项目名称:scikit-learn,代码行数:8,代码来源:test_multiclass.py
示例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
开发者ID:lurke,项目名称:DependencyParsing,代码行数:8,代码来源:nate.py
示例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))
开发者ID:jaguila,项目名称:cert,代码行数:9,代码来源:test_multiclass.py
示例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
开发者ID:piotrchmiel,项目名称:ziwm_hypertension,代码行数:10,代码来源:dynamic_OvO.py
示例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)
开发者ID:yikuizhai,项目名称:scikit-learn,代码行数:10,代码来源:test_multiclass.py
示例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
开发者ID:Nivekul,项目名称:facialexpressionprediction,代码行数:10,代码来源:ovo.py
示例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)
开发者ID:jaguila,项目名称:cert,代码行数:11,代码来源:test_multiclass.py
示例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
开发者ID:AlexisMignon,项目名称:scikit-learn,代码行数:53,代码来源:gpc.py
示例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
开发者ID:Aphaniteja,项目名称:Computational-Sustainability,代码行数:13,代码来源:svm.py
示例16: test_multicluster
def test_multicluster(self):
c = BinaryTiloClassifier(PinchRatioCutStrategy(),
similarity.Gaussian())
##c = BinaryTiloClassifier(similarity.KNN())
##mcc = OneVsRestClassifier(c)
mcc = OneVsOneClassifier(c)
data = self.three_class_pts
classes = self.three_class_labels
peturbed_data = data + 0.01 * np.random.random(data.shape)
fitted = mcc.fit(peturbed_data, classes)
guesses = fitted.predict(peturbed_data)
assert_array_equal(guesses, classes)
开发者ID:rsbowman,项目名称:sklearn-prc,代码行数:13,代码来源:tests.py
示例17: test_pairwise_indices
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])
开发者ID:dsquareindia,项目名称:scikit-learn,代码行数:13,代码来源:test_multiclass.py
示例18: fit
class ClassifierOvOAsFeatures:
"""
A transformation that esentially implement a form of dimensionality
reduction.
This class uses a fast SGDClassifier configured like a linear SVM to produce
a vector of decision functions separating target classes in a
one-versus-rest fashion.
It's useful to reduce the dimension bag-of-words feature-set into features
that are richer in information.
"""
def fit(self, X, y):
"""
`X` is expected to be an array-like or a sparse matrix.
`y` is expected to be an array-like containing the classes to learn.
"""
self.classifier = OneVsOneClassifier(SGDClassifier(),n_jobs=-1).fit(X,numpy.array(y))
return self
def transform(self, X, y=None):
"""
`X` is expected to be an array-like or a sparse matrix.
It returns a dense matrix of shape (n_samples, m_features) where
m_features = (n_classes * (n_classes - 1)) / 2
"""
return self.classifier.decision_function(X)
开发者ID:EspenAlbert,项目名称:sentimentAnalysisMovieReviews,代码行数:25,代码来源:transformations.py
示例19: multiclassSVC
def multiclassSVC(classifier, sz=2000):
mnsize = sz
df = hw6u.load_mnist_features(mnsize)
data = utils.pandas_to_data(df)
k = 10
all_folds = hw3u.partition_folds(data, k)
kf_train, kf_test = dl.get_train_and_test(all_folds, 0)
y, X = hw4u.split_truth_from_data(kf_train, replace_zeros=False)
y, X = np.asarray(y), np.asarray(X)
y_test, X_test = hw4u.split_truth_from_data(kf_test, replace_zeros=False)
y_test, X_test = np.asarray(y_test), np.asarray(X_test)
print 'Beginning analysis: {}'.format(X.shape)
#clf = OneVsRestClassifier(classifier, n_jobs=4).fit(X, y)
clf = OneVsOneClassifier(classifier).fit(X, y)
#clf = OutputCodeClassifier(LinearSVC(random_state=0), code_size=10, random_state=0).fit(np.asarray(X), y)
y_pred = clf.predict(X)
print 'train acc: {} test acc: {}'.format(accuracy_score(fix_y(y_pred), fix_y(y)), accuracy_score(fix_y(y_test), fix_y(clf.predict(X_test))))
print 'train acc: {} test acc: {}'.format(accuracy_score(fix_y(clf.predict(X)), fix_y(y)), accuracy_score(fix_y(y_test), fix_y(clf.predict(X_test))))
开发者ID:alliemacleay,项目名称:MachineLearning_CS6140,代码行数:19,代码来源:hw6.py
示例20: trainOneVsOne2
def trainOneVsOne2( histograms ):
xAll = convertToSvmFormatFeature(histograms)
scaleParam = computeScaleParameters(xAll)
scaleFeatureData(xAll,scaleParam)
xAll = np.array(xAll)
yAll = [ x['label'] for x in histograms ]
yAll = np.array(yAll)
# svm = OneVsOneClassifier(LinearSVC(random_state=0,dual=svm_conf['dual'],C=svm_conf['C']))
gammaBase = 1.0/kmeans_conf['K']
# svm = OneVsOneClassifier(sklearn.svm.SVC(C=100, gamma=10*gammaBase,kernel='rbf'))
svm = OneVsOneClassifier(sklearn.svm.SVC(C=1000, gamma=gammaBase,kernel='sigmoid'))
svm.fit(xAll,yAll)
out = {'scaleParam':scaleParam,'svm':svm}
return out
开发者ID:lessthanoptimal,项目名称:bow,代码行数:19,代码来源:classifySiftSvmOneVsOne.py
注:本文中的sklearn.multiclass.OneVsOneClassifier类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论