本文整理汇总了Python中sklearn.svm.OneClassSVM方法的典型用法代码示例。如果您正苦于以下问题:Python svm.OneClassSVM方法的具体用法?Python svm.OneClassSVM怎么用?Python svm.OneClassSVM使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.svm
的用法示例。
在下文中一共展示了svm.OneClassSVM方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def __init__(self, kernel='rbf', nu=0.1, hybrid=False):
"""Init OCSVM instance."""
self.kernel = kernel
self.nu = nu
self.rho = None
self.gamma = None
self.model = OneClassSVM(kernel=kernel, nu=nu)
self.hybrid = hybrid
self.ae_net = None # autoencoder network for the case of a hybrid model
self.linear_model = None # also init a model with linear kernel if hybrid approach
self.results = {
'train_time': None,
'test_time': None,
'test_auc': None,
'test_scores': None,
'train_time_linear': None,
'test_time_linear': None,
'test_auc_linear': None
}
示例2: train_with_scikit
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def train_with_scikit(self, trainX, testX):
settings = self.settings
if (settings['load_parameters'] == True):
parameters = self.load_parameters()
clf = svm.OneClassSVM(parameters)
else:
clf = svm.OneClassSVM(nu=settings['nu'], kernel=settings['kernel'], gamma=settings['gamma'], verbose=settings['verbose'])
clf.fit(trainX)
y_pred_train = clf.predict(trainX)
y_pred_test = clf.predict(testX)
n_error_train = y_pred_train[y_pred_train == -1].size
n_error_test = y_pred_test[y_pred_test == -1].size
return y_pred_train, y_pred_test, n_error_train, n_error_test
示例3: _fit
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def _fit(self, X):
self.estimator_ = OneClassSVM(
cache_size = self.cache_size,
gamma = self.gamma,
max_iter = self.max_iter,
nu = self.nu,
shrinking = self.shrinking,
tol = self.tol
).fit(X)
l, = self.support_.shape
self.nu_l_ = self.nu * l
Q = rbf_kernel(
self.support_vectors_, gamma=self.estimator_._gamma
)
c2 = (self.dual_coef_ @ Q @ self.dual_coef_.T)[0, 0]
self.R2_ = c2 + 2. * self.intercept_[0] + 1.
return self
示例4: get_dist
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def get_dist(data_list, method):
Xnumpy = np.asarray(data_list)
X = Xnumpy.reshape(-1, 1)
dist = None
if method == "raw":
dist = data_list # raw column data
if method == "kd":
kde = KernelDensity(
kernel=C.kd["kernel"],
bandwidth=C.kd["bandwidth"]
).fit(X)
dist = kde.score_samples(X)
elif method == "odsvm":
svmachine = svm.OneClassSVM(
nu=C.odsvm["nu"],
kernel=C.odsvm["kernel"],
gamma=C.odsvm["gamma"]
)
dist = svmachine.fit(X)
return dist
示例5: learn_structure
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def learn_structure(self, samples):
X_train, X_test = self._generate_train_test_sets(samples, 0.75)
logger.info("Training with " + str(len(X_train)) +
"samples; testing with " + str(len(X_test)) + " samples.")
svm_detector = svm.OneClassSVM(nu=0.95 * OUTLIERS_FRACTION + 0.05,
kernel="rbf", gamma=0.1)
svm_detector.fit(X_train)
Y_test = svm_detector.predict(X_test)
num_anomalies = Y_test[Y_test == -1].size
logger.info("Found " + str(num_anomalies) +
" anomalies in testing set")
return svm_detector
示例6: test_learn_structure
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def test_learn_structure(self):
data = self.get_testing_data()
clf = self.svm.learn_structure(data)
self.assertIsInstance(clf, svm.OneClassSVM)
示例7: check_svm_model_equal
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def check_svm_model_equal(dense_svm, sparse_svm, X_train, y_train, X_test):
dense_svm.fit(X_train.toarray(), y_train)
if sparse.isspmatrix(X_test):
X_test_dense = X_test.toarray()
else:
X_test_dense = X_test
sparse_svm.fit(X_train, y_train)
assert sparse.issparse(sparse_svm.support_vectors_)
assert sparse.issparse(sparse_svm.dual_coef_)
assert_array_almost_equal(dense_svm.support_vectors_,
sparse_svm.support_vectors_.toarray())
assert_array_almost_equal(dense_svm.dual_coef_,
sparse_svm.dual_coef_.toarray())
if dense_svm.kernel == "linear":
assert sparse.issparse(sparse_svm.coef_)
assert_array_almost_equal(dense_svm.coef_, sparse_svm.coef_.toarray())
assert_array_almost_equal(dense_svm.support_, sparse_svm.support_)
assert_array_almost_equal(dense_svm.predict(X_test_dense),
sparse_svm.predict(X_test))
assert_array_almost_equal(dense_svm.decision_function(X_test_dense),
sparse_svm.decision_function(X_test))
assert_array_almost_equal(dense_svm.decision_function(X_test_dense),
sparse_svm.decision_function(X_test_dense))
if isinstance(dense_svm, svm.OneClassSVM):
msg = "cannot use sparse input in 'OneClassSVM' trained on dense data"
else:
assert_array_almost_equal(dense_svm.predict_proba(X_test_dense),
sparse_svm.predict_proba(X_test), 4)
msg = "cannot use sparse input in 'SVC' trained on dense data"
if sparse.isspmatrix(X_test):
assert_raise_message(ValueError, msg, dense_svm.predict, X_test)
示例8: test_sparse_oneclasssvm
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def test_sparse_oneclasssvm(datasets_index, kernel):
# Check that sparse OneClassSVM gives the same result as dense OneClassSVM
# many class dataset:
X_blobs, _ = make_blobs(n_samples=100, centers=10, random_state=0)
X_blobs = sparse.csr_matrix(X_blobs)
datasets = [[X_sp, None, T], [X2_sp, None, T2],
[X_blobs[:80], None, X_blobs[80:]],
[iris.data, None, iris.data]]
dataset = datasets[datasets_index]
clf = svm.OneClassSVM(gamma=1, kernel=kernel)
sp_clf = svm.OneClassSVM(gamma=1, kernel=kernel)
check_svm_model_equal(clf, sp_clf, *dataset)
示例9: test_oneclass
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def test_oneclass():
# Test OneClassSVM
clf = svm.OneClassSVM(gamma='scale')
clf.fit(X)
pred = clf.predict(T)
assert_array_equal(pred, [1, -1, -1])
assert_equal(pred.dtype, np.dtype('intp'))
assert_array_almost_equal(clf.intercept_, [-1.218], decimal=3)
assert_array_almost_equal(clf.dual_coef_,
[[0.750, 0.750, 0.750, 0.750]],
decimal=3)
assert_raises(AttributeError, lambda: clf.coef_)
示例10: test_oneclass_decision_function
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def test_oneclass_decision_function():
# Test OneClassSVM decision function
clf = svm.OneClassSVM()
rnd = check_random_state(2)
# Generate train data
X = 0.3 * rnd.randn(100, 2)
X_train = np.r_[X + 2, X - 2]
# Generate some regular novel observations
X = 0.3 * rnd.randn(20, 2)
X_test = np.r_[X + 2, X - 2]
# Generate some abnormal novel observations
X_outliers = rnd.uniform(low=-4, high=4, size=(20, 2))
# fit the model
clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1)
clf.fit(X_train)
# predict things
y_pred_test = clf.predict(X_test)
assert_greater(np.mean(y_pred_test == 1), .9)
y_pred_outliers = clf.predict(X_outliers)
assert_greater(np.mean(y_pred_outliers == -1), .9)
dec_func_test = clf.decision_function(X_test)
assert_array_equal((dec_func_test > 0).ravel(), y_pred_test == 1)
dec_func_outliers = clf.decision_function(X_outliers)
assert_array_equal((dec_func_outliers > 0).ravel(), y_pred_outliers == 1)
示例11: test_oneclass_score_samples
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def test_oneclass_score_samples():
X_train = [[1, 1], [1, 2], [2, 1]]
clf = svm.OneClassSVM(gamma=1).fit(X_train)
assert_array_equal(clf.score_samples([[2., 2.]]),
clf.decision_function([[2., 2.]]) + clf.offset_)
示例12: main2
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def main2():
"""
Use one class SVM for multi-class classification
Accuracy = 71.45%
"""
# Initializations
seed = 123456789
np.random.seed(seed)
ntrain, ntest = 800, 200
(tr_x, tr_y), (te_x, te_y) = load_mnist()
tr, te = [], []
for i in xrange(10):
tr.append(np.random.permutation(tr_x[tr_y == i])[:ntrain])
te.append(np.random.permutation(te_x[te_y == i])[:ntest])
# Train the classifiers and get their results
clfs = []
for i in xrange(10):
clf = OneClassSVM(kernel='linear', nu=0.1, random_state=seed)
clf.fit(tr[i])
clfs.append(clf)
# Test the classifiers
te_x = np.vstack(te)
te_y = np.hstack([np.array([i] * ntest) for i in xrange(10)])
results = np.zeros((10, len(te_y)))
for i in xrange(10):
results[i] = clfs[i].decision_function(te_x).flatten() + \
np.random.uniform(0.1, 0.2, len(te_y))
print np.sum(np.argmax(results, 0) == te_y) / float(len(te_y))
示例13: test_36_one_class_svm
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def test_36_one_class_svm(self):
print("\ntest 36 (One Class SVM\n")
detection_map = {
'true': -1,
'false': 1
}
df = pd.read_csv("nyoka/tests/train_ocsvm.csv")
df_test = pd.read_csv("nyoka/tests/test_ocsvm.csv")
features = df.columns
model = OneClassSVM(nu=0.1)
pipeline_obj = Pipeline([
("model", model)
])
pipeline_obj.fit(df)
file_name = 'test36sklearn.pmml'
skl_to_pmml(pipeline_obj, features, '', file_name)
model_pred = pipeline_obj.predict(df_test)
model_scores = pipeline_obj.decision_function(df_test)
model_name = self.adapa_utility.upload_to_zserver(file_name)
z_predictions = self.adapa_utility.score_in_zserver(model_name,'nyoka/tests/test_ocsvm.csv','ANOMALY')
cnt = 0
for idx, value in enumerate(z_predictions):
score, is_anomaly = value.split(",")
score = float(score)
if "{:.6f}".format(score) != "{:.6f}".format(model_scores[idx]) or model_pred[idx] != detection_map[is_anomaly]:
cnt += 1
self.assertEqual(cnt,0)
示例14: test_validate_ocsvm
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def test_validate_ocsvm(self):
iris = datasets.load_iris()
X = iris.data
y = iris.target
features = iris.feature_names
model = OneClassSVM()
pipeline = Pipeline([
('standard_scaler',StandardScaler()),
('Imputer',Imputer()),
('model',model)
])
pipeline.fit(X,y)
file_name = model.__class__.__name__+'.pmml'
skl_to_pmml(pipeline, features ,pmml_f_name= file_name)
self.assertEqual(self.schema.is_valid(file_name), True)
示例15: test_sklearn_27
# 需要导入模块: from sklearn import svm [as 别名]
# 或者: from sklearn.svm import OneClassSVM [as 别名]
def test_sklearn_27(self):
irisdata = datasets.load_iris()
iris = pd.DataFrame(irisdata.data, columns=irisdata.feature_names)
iris['Species'] = irisdata.target
feature_names = iris.columns.drop('Species')
X = iris[iris.columns.drop(['Species'])]
model = OneClassSVM(gamma=0.25)
pipeline_obj = Pipeline([
('standard_scaler', StandardScaler()),
('Imputer', Imputer()),
('model', model)
])
pipeline_obj.fit(X)
skl_to_pmml(pipeline_obj, feature_names, pmml_f_name="one_class_svm.pmml")
pmml_obj = pml.parse("one_class_svm.pmml", True)
# 1
self.assertEqual(os.path.isfile("one_class_svm.pmml"), True)
# 2
svm_tab = pmml_obj.AnomalyDetectionModel[0].SupportVectorMachineModel
self.assertEqual(model.gamma, svm_tab.RadialBasisKernelType.gamma)
# 3
self.assertEqual(model.intercept_[0], svm_tab.SupportVectorMachine[0].Coefficients.absoluteValue)
# 4
for model_val, pmml_val in zip(model.dual_coef_[0], svm_tab.SupportVectorMachine[0].Coefficients.Coefficient):
self.assertEqual("{:.16f}".format(model_val), "{:.16f}".format(pmml_val.value))