本文整理汇总了Python中sklearn.linear_model.PassiveAggressiveClassifier方法的典型用法代码示例。如果您正苦于以下问题:Python linear_model.PassiveAggressiveClassifier方法的具体用法?Python linear_model.PassiveAggressiveClassifier怎么用?Python linear_model.PassiveAggressiveClassifier使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model
的用法示例。
在下文中一共展示了linear_model.PassiveAggressiveClassifier方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_main
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_main(self):
categories, documents = get_docs_categories()
clean_function = lambda text: '' if text.startswith('[') else text
entity_types = set(['GPE'])
term_doc_mat = (
TermDocMatrixFactory(
category_text_iter=zip(categories, documents),
clean_function=clean_function,
nlp=_testing_nlp,
feats_from_spacy_doc=FeatsFromSpacyDoc(entity_types_to_censor=entity_types)
).build()
)
clf = PassiveAggressiveClassifier()
fdc = FeatsFromDoc(term_doc_mat._term_idx_store,
clean_function=clean_function,
feats_from_spacy_doc=FeatsFromSpacyDoc(
entity_types_to_censor=entity_types)).set_nlp(_testing_nlp)
tfidf = TfidfTransformer(norm='l1')
X = tfidf.fit_transform(term_doc_mat._X)
clf.fit(X, term_doc_mat._y)
X_to_predict = fdc.feats_from_doc('Did sometimes march UNKNOWNWORD')
pred = clf.predict(tfidf.transform(X_to_predict))
dec = clf.decision_function(X_to_predict)
示例2: passive_aggressive_train
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def passive_aggressive_train(self):
'''Trains passive aggressive classifier
'''
self._clf = PassiveAggressiveClassifier(n_iter=50, C=0.2, n_jobs=-1, random_state=0)
self._clf.fit(self._term_doc_matrix._X, self._term_doc_matrix._y)
y_dist = self._clf.decision_function(self._term_doc_matrix._X)
pos_ecdf = ECDF(y_dist[y_dist >= 0])
neg_ecdf = ECDF(y_dist[y_dist <= 0])
def proba_function(distance_from_hyperplane):
if distance_from_hyperplane > 0:
return pos_ecdf(distance_from_hyperplane) / 2. + 0.5
elif distance_from_hyperplane < 0:
return pos_ecdf(distance_from_hyperplane) / 2.
return 0.5
self._proba = proba_function
return self
示例3: test_partial_fit
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_partial_fit():
est = PassiveAggressiveClassifier(random_state=0, shuffle=False,
max_iter=5, tol=None)
transformer = SelectFromModel(estimator=est)
transformer.partial_fit(data, y,
classes=np.unique(y))
old_model = transformer.estimator_
transformer.partial_fit(data, y,
classes=np.unique(y))
new_model = transformer.estimator_
assert old_model is new_model
X_transform = transformer.transform(data)
transformer.fit(np.vstack((data, data)), np.concatenate((y, y)))
assert_array_almost_equal(X_transform, transformer.transform(data))
# check that if est doesn't have partial_fit, neither does SelectFromModel
transformer = SelectFromModel(estimator=RandomForestClassifier())
assert not hasattr(transformer, "partial_fit")
示例4: test_learning_curve_batch_and_incremental_learning_are_equal
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_learning_curve_batch_and_incremental_learning_are_equal():
X, y = make_classification(n_samples=30, n_features=1, n_informative=1,
n_redundant=0, n_classes=2,
n_clusters_per_class=1, random_state=0)
train_sizes = np.linspace(0.2, 1.0, 5)
estimator = PassiveAggressiveClassifier(max_iter=1, tol=None,
shuffle=False)
train_sizes_inc, train_scores_inc, test_scores_inc = \
learning_curve(
estimator, X, y, train_sizes=train_sizes,
cv=3, exploit_incremental_learning=True)
train_sizes_batch, train_scores_batch, test_scores_batch = \
learning_curve(
estimator, X, y, cv=3, train_sizes=train_sizes,
exploit_incremental_learning=False)
assert_array_equal(train_sizes_inc, train_sizes_batch)
assert_array_almost_equal(train_scores_inc.mean(axis=1),
train_scores_batch.mean(axis=1))
assert_array_almost_equal(test_scores_inc.mean(axis=1),
test_scores_batch.mean(axis=1))
示例5: test_classifier_accuracy
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_classifier_accuracy():
for data in (X, X_csr):
for fit_intercept in (True, False):
for average in (False, True):
clf = PassiveAggressiveClassifier(
C=1.0, max_iter=30, fit_intercept=fit_intercept,
random_state=1, average=average, tol=None)
clf.fit(data, y)
score = clf.score(data, y)
assert_greater(score, 0.79)
if average:
assert hasattr(clf, 'average_coef_')
assert hasattr(clf, 'average_intercept_')
assert hasattr(clf, 'standard_intercept_')
assert hasattr(clf, 'standard_coef_')
# 0.23. warning about tol not having its correct default value.
示例6: test_classifier_partial_fit
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_classifier_partial_fit():
classes = np.unique(y)
for data in (X, X_csr):
for average in (False, True):
clf = PassiveAggressiveClassifier(
C=1.0, fit_intercept=True, random_state=0,
average=average, max_iter=5)
for t in range(30):
clf.partial_fit(data, y, classes)
score = clf.score(data, y)
assert_greater(score, 0.79)
if average:
assert hasattr(clf, 'average_coef_')
assert hasattr(clf, 'average_intercept_')
assert hasattr(clf, 'standard_intercept_')
assert hasattr(clf, 'standard_coef_')
# 0.23. warning about tol not having its correct default value.
示例7: test_class_weights
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_class_weights():
# Test class weights.
X2 = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0],
[1.0, 1.0], [1.0, 0.0]])
y2 = [1, 1, 1, -1, -1]
clf = PassiveAggressiveClassifier(C=0.1, max_iter=100, class_weight=None,
random_state=100)
clf.fit(X2, y2)
assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([1]))
# we give a small weights to class 1
clf = PassiveAggressiveClassifier(C=0.1, max_iter=100,
class_weight={1: 0.001},
random_state=100)
clf.fit(X2, y2)
# now the hyperplane should rotate clock-wise and
# the prediction on this point should shift
assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([-1]))
# 0.23. warning about tol not having its correct default value.
示例8: test_equal_class_weight
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_equal_class_weight():
X2 = [[1, 0], [1, 0], [0, 1], [0, 1]]
y2 = [0, 0, 1, 1]
clf = PassiveAggressiveClassifier(
C=0.1, max_iter=1000, tol=None, class_weight=None)
clf.fit(X2, y2)
# Already balanced, so "balanced" weights should have no effect
clf_balanced = PassiveAggressiveClassifier(
C=0.1, max_iter=1000, tol=None, class_weight="balanced")
clf_balanced.fit(X2, y2)
clf_weighted = PassiveAggressiveClassifier(
C=0.1, max_iter=1000, tol=None, class_weight={0: 0.5, 1: 0.5})
clf_weighted.fit(X2, y2)
# should be similar up to some epsilon due to learning rate schedule
assert_almost_equal(clf.coef_, clf_weighted.coef_, decimal=2)
assert_almost_equal(clf.coef_, clf_balanced.coef_, decimal=2)
# 0.23. warning about tol not having its correct default value.
示例9: test_model_passive_aggressive_classifier_binary_class
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_model_passive_aggressive_classifier_binary_class(self):
model, X = fit_classification_model(
PassiveAggressiveClassifier(random_state=42), 2)
model_onnx = convert_sklearn(
model,
"scikit-learn PassiveAggressiveClassifier binary",
[("input", FloatTensorType([None, X.shape[1]]))],
target_opset=TARGET_OPSET
)
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X,
model,
model_onnx,
basename="SklearnPassiveAggressiveClassifierBinary-Out0",
allow_failure="StrictVersion(onnx.__version__)"
" < StrictVersion('1.2') or "
"StrictVersion(onnxruntime.__version__)"
" <= StrictVersion('0.2.1')",
)
示例10: test_model_passive_aggressive_classifier_multi_class
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_model_passive_aggressive_classifier_multi_class(self):
model, X = fit_classification_model(
PassiveAggressiveClassifier(random_state=42), 5)
model_onnx = convert_sklearn(
model,
"scikit-learn PassiveAggressiveClassifier multi-class",
[("input", FloatTensorType([None, X.shape[1]]))],
target_opset=TARGET_OPSET
)
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X,
model,
model_onnx,
basename="SklearnPassiveAggressiveClassifierMulti-Out0",
allow_failure="StrictVersion(onnx.__version__)"
" < StrictVersion('1.2') or "
"StrictVersion(onnxruntime.__version__)"
" <= StrictVersion('0.2.1')",
)
示例11: test_model_passive_aggressive_classifier_binary_class_int
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_model_passive_aggressive_classifier_binary_class_int(self):
model, X = fit_classification_model(
PassiveAggressiveClassifier(random_state=42), 2, is_int=True)
model_onnx = convert_sklearn(
model,
"scikit-learn PassiveAggressiveClassifier binary",
[("input", Int64TensorType([None, X.shape[1]]))],
target_opset=TARGET_OPSET
)
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X,
model,
model_onnx,
basename="SklearnPassiveAggressiveClassifierBinaryInt-Out0",
allow_failure="StrictVersion(onnx.__version__)"
" < StrictVersion('1.2') or "
"StrictVersion(onnxruntime.__version__)"
" <= StrictVersion('0.2.1')",
)
示例12: test_model_passive_aggressive_classifier_multi_class_int
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_model_passive_aggressive_classifier_multi_class_int(self):
model, X = fit_classification_model(
PassiveAggressiveClassifier(random_state=42), 5, is_int=True)
model_onnx = convert_sklearn(
model,
"scikit-learn PassiveAggressiveClassifier multi-class",
[("input", Int64TensorType([None, X.shape[1]]))],
target_opset=TARGET_OPSET
)
self.assertIsNotNone(model_onnx)
dump_data_and_model(
X,
model,
model_onnx,
basename="SklearnPassiveAggressiveClassifierMultiInt-Out0",
allow_failure="StrictVersion(onnx.__version__)"
" < StrictVersion('1.2') or "
"StrictVersion(onnxruntime.__version__)"
" <= StrictVersion('0.2.1')",
)
示例13: test_partial_fit
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_partial_fit():
est = PassiveAggressiveClassifier(random_state=0, shuffle=False,
max_iter=5, tol=None)
transformer = SelectFromModel(estimator=est)
transformer.partial_fit(data, y,
classes=np.unique(y))
old_model = transformer.estimator_
transformer.partial_fit(data, y,
classes=np.unique(y))
new_model = transformer.estimator_
assert_true(old_model is new_model)
X_transform = transformer.transform(data)
transformer.fit(np.vstack((data, data)), np.concatenate((y, y)))
assert_array_equal(X_transform, transformer.transform(data))
# check that if est doesn't have partial_fit, neither does SelectFromModel
transformer = SelectFromModel(estimator=RandomForestClassifier())
assert_false(hasattr(transformer, "partial_fit"))
示例14: test_classifier_correctness
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_classifier_correctness():
y_bin = y.copy()
y_bin[y != 1] = -1
for loss in ("hinge", "squared_hinge"):
clf1 = MyPassiveAggressive(
C=1.0, loss=loss, fit_intercept=True, n_iter=2)
clf1.fit(X, y_bin)
for data in (X, X_csr):
clf2 = PassiveAggressiveClassifier(
C=1.0, loss=loss, fit_intercept=True, max_iter=2,
shuffle=False, tol=None)
clf2.fit(data, y_bin)
assert_array_almost_equal(clf1.w, clf2.coef_.ravel(), decimal=2)
示例15: test_class_weights
# 需要导入模块: from sklearn import linear_model [as 别名]
# 或者: from sklearn.linear_model import PassiveAggressiveClassifier [as 别名]
def test_class_weights():
# Test class weights.
X2 = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0],
[1.0, 1.0], [1.0, 0.0]])
y2 = [1, 1, 1, -1, -1]
clf = PassiveAggressiveClassifier(C=0.1, max_iter=100, class_weight=None,
random_state=100)
clf.fit(X2, y2)
assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([1]))
# we give a small weights to class 1
clf = PassiveAggressiveClassifier(C=0.1, max_iter=100,
class_weight={1: 0.001},
random_state=100)
clf.fit(X2, y2)
# now the hyperplane should rotate clock-wise and
# the prediction on this point should shift
assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([-1]))