當前位置: 首頁>>代碼示例>>Python>>正文


Python svm.OneClassSVM方法代碼示例

本文整理匯總了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
        } 
開發者ID:lukasruff,項目名稱:Deep-SAD-PyTorch,代碼行數:24,代碼來源:ocsvm.py

示例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 
開發者ID:morrigan,項目名稱:user-behavior-anomaly-detector,代碼行數:19,代碼來源:ocsvm.py

示例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 
開發者ID:Y-oHr-N,項目名稱:kenchi,代碼行數:22,代碼來源:classification_based.py

示例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 
開發者ID:mitdbg,項目名稱:aurum-datadiscovery,代碼行數:22,代碼來源:dataanalysis.py

示例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 
開發者ID:openstack,項目名稱:monasca-analytics,代碼行數:14,代碼來源:svm_one_class.py

示例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) 
開發者ID:openstack,項目名稱:monasca-analytics,代碼行數:6,代碼來源:test_svm_one_class.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:33,代碼來源:test_sparse.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:14,代碼來源:test_sparse.py

示例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_) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:15,代碼來源:test_svm.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:30,代碼來源:test_svm.py

示例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_) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:7,代碼來源:test_svm.py

示例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)) 
開發者ID:tehtechguy,項目名稱:mHTM,代碼行數:34,代碼來源:OneVsRest.py

示例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) 
開發者ID:nyoka-pmml,項目名稱:nyoka,代碼行數:29,代碼來源:testScoreWithAdapaSklearn.py

示例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) 
開發者ID:nyoka-pmml,項目名稱:nyoka,代碼行數:17,代碼來源:_validateSchema.py

示例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)) 
開發者ID:nyoka-pmml,項目名稱:nyoka,代碼行數:34,代碼來源:test_skl_to_pmml_UnitTest.py


注:本文中的sklearn.svm.OneClassSVM方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。