Python sklearn.svm.OneClassSVM() Examples

The following are 30 code examples of sklearn.svm.OneClassSVM(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.svm , or try the search function .
Example #1
Source File: dataanalysis.py    From aurum-datadiscovery with MIT License 7 votes vote down vote up
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 
Example #2
Source File: ocsvm.py    From Deep-SAD-PyTorch with MIT License 6 votes vote down vote up
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
        } 
Example #3
Source File: ocsvm.py    From user-behavior-anomaly-detector with MIT License 6 votes vote down vote up
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 
Example #4
Source File: classification_based.py    From kenchi with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #5
Source File: test_svm.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_immutable_coef_property():
    # Check that primal coef modification are not silently ignored
    svms = [
        svm.SVC(kernel='linear').fit(iris.data, iris.target),
        svm.NuSVC(kernel='linear').fit(iris.data, iris.target),
        svm.SVR(kernel='linear').fit(iris.data, iris.target),
        svm.NuSVR(kernel='linear').fit(iris.data, iris.target),
        svm.OneClassSVM(kernel='linear').fit(iris.data),
    ]
    for clf in svms:
        assert_raises(AttributeError, clf.__setattr__, 'coef_', np.arange(3))
        assert_raises((RuntimeError, ValueError),
                      clf.coef_.__setitem__, (0, 0), 0) 
Example #6
Source File: test_svm.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
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) 
Example #7
Source File: test_svm.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_oneclass():
    # Test OneClassSVM
    clf = svm.OneClassSVM()
    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.008], decimal=3)
    assert_array_almost_equal(clf.dual_coef_,
                              [[0.632, 0.233, 0.633, 0.234, 0.632, 0.633]],
                              decimal=3)
    assert_raises(AttributeError, lambda: clf.coef_) 
Example #8
Source File: test_sparse.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_sparse_oneclasssvm():
    """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]]
    kernels = ["linear", "poly", "rbf", "sigmoid"]
    for dataset in datasets:
        for kernel in kernels:
            clf = svm.OneClassSVM(kernel=kernel, random_state=0)
            sp_clf = svm.OneClassSVM(kernel=kernel, random_state=0)
            check_svm_model_equal(clf, sp_clf, *dataset) 
Example #9
Source File: test_sparse.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
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_true(sparse.issparse(sparse_svm.support_vectors_))
    assert_true(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_true(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) 
Example #10
Source File: test_svm.py    From pandas-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_objectmapper(self):
        df = pdml.ModelFrame([])
        self.assertIs(df.svm.SVC, svm.SVC)
        self.assertIs(df.svm.LinearSVC, svm.LinearSVC)
        self.assertIs(df.svm.NuSVC, svm.NuSVC)
        self.assertIs(df.svm.SVR, svm.SVR)
        self.assertIs(df.svm.NuSVR, svm.NuSVR)
        self.assertIs(df.svm.OneClassSVM, svm.OneClassSVM) 
Example #11
Source File: svm.py    From Deep-SVDD with MIT License 5 votes vote down vote up
def initialize_svm(self, loss, **kwargs):

        assert loss in ('SVC', 'OneClassSVM')

        if self.kernel in ('linear', 'poly', 'rbf', 'sigmoid'):
            kernel = self.kernel
        else:
            kernel = 'precomputed'

        if loss == 'SVC':
            self.svm = svm.SVC(kernel=kernel, C=Cfg.svm_C, **kwargs)
        if loss == 'OneClassSVM':
            self.svm = svm.OneClassSVM(kernel=kernel, nu=Cfg.svm_nu, **kwargs)
            self.cv_svm = svm.OneClassSVM(kernel=kernel, nu=Cfg.svm_nu, **kwargs) 
Example #12
Source File: test_sklearn_svm_converters.py    From sklearn-onnx with MIT License 5 votes vote down vote up
def test_convert_oneclasssvm(self):
        model, X = self._fit_one_class_svm(OneClassSVM())
        model_onnx = convert_sklearn(
            model, "OCSVM", [("input", FloatTensorType([None, X.shape[1]]))])
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinOneClassSVM",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " < StrictVersion('0.5.0')") 
Example #13
Source File: experiments.py    From AnomalyDetectionTransformations with MIT License 5 votes vote down vote up
def _raw_ocsvm_experiment(dataset_load_fn, dataset_name, single_class_ind):
    (x_train, y_train), (x_test, y_test) = dataset_load_fn()

    x_train = x_train.reshape((len(x_train), -1))
    x_test = x_test.reshape((len(x_test), -1))

    x_train_task = x_train[y_train.flatten() == single_class_ind]
    if dataset_name in ['cats-vs-dogs']:  # OC-SVM is quadratic on the number of examples, so subsample training set
        subsample_inds = np.random.choice(len(x_train_task), 5000, replace=False)
        x_train_task = x_train_task[subsample_inds]

    pg = ParameterGrid({'nu': np.linspace(0.1, 0.9, num=9),
                        'gamma': np.logspace(-7, 2, num=10, base=2)})

    results = Parallel(n_jobs=6)(
        delayed(_train_ocsvm_and_score)(d, x_train_task, y_test.flatten() == single_class_ind, x_test)
        for d in pg)

    best_params, best_auc_score = max(zip(pg, results), key=lambda t: t[-1])
    best_ocsvm = OneClassSVM(**best_params).fit(x_train_task)
    scores = best_ocsvm.decision_function(x_test)
    labels = y_test.flatten() == single_class_ind

    res_file_name = '{}_raw-oc-svm_{}_{}.npz'.format(dataset_name,
                                                     get_class_name_from_index(single_class_ind, dataset_name),
                                                     datetime.now().strftime('%Y-%m-%d-%H%M'))
    res_file_path = os.path.join(RESULTS_DIR, dataset_name, res_file_name)
    save_roc_pr_curve_data(scores, labels, res_file_path) 
Example #14
Source File: experiments.py    From AnomalyDetectionTransformations with MIT License 5 votes vote down vote up
def _train_ocsvm_and_score(params, xtrain, test_labels, xtest):
    return roc_auc_score(test_labels, OneClassSVM(**params).fit(xtrain).decision_function(xtest)) 
Example #15
Source File: features.py    From probreg with MIT License 5 votes vote down vote up
def init(self):
        self._clf = svm.OneClassSVM(nu=self._nu, kernel="rbf", gamma=self._gamma) 
Example #16
Source File: svm_gui.py    From sklearn_pydata2015 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def fit(self):
        print("fit the model")
        train = np.array(self.model.data)
        X = train[:, 0:2]
        y = train[:, 2]

        C = float(self.complexity.get())
        gamma = float(self.gamma.get())
        coef0 = float(self.coef0.get())
        degree = int(self.degree.get())
        kernel_map = {0: "linear", 1: "rbf", 2: "poly"}
        if len(np.unique(y)) == 1:
            clf = svm.OneClassSVM(kernel=kernel_map[self.kernel.get()],
                                  gamma=gamma, coef0=coef0, degree=degree)
            clf.fit(X)
        else:
            clf = svm.SVC(kernel=kernel_map[self.kernel.get()], C=C,
                          gamma=gamma, coef0=coef0, degree=degree)
            clf.fit(X, y)
        if hasattr(clf, 'score'):
            print("Accuracy:", clf.score(X, y) * 100)
        X1, X2, Z = self.decision_surface(clf)
        self.model.clf = clf
        self.model.set_surface((X1, X2, Z))
        self.model.surface_type = self.surface_type.get()
        self.fitted = True
        self.model.changed("surface") 
Example #17
Source File: svm.py    From safekit with MIT License 5 votes vote down vote up
def sample_hyps_svm(kern, nu_, deg, shrink):
    """
    :return: A OneClassSVM object with randomly sampled hyperparams, used to detect anomaly.
    """

    kernel = kern #random.choice(['rbf', 'linear', 'poly', 'sigmoid', 'precomputed'])
    nu = nu_ #randrange_float(0.0, 0.9999, 0.05)
    degree = deg #random.randint(1,10)
    gamma = 'auto'  # default. uses 1/n_features
    coef0 = 0.0  # default. No suggested values given in documentation
    shrinking = shrink #random.choice([True, False])

    model = OneClassSVM(kernel=kernel, nu=nu,
                        degree=degree, gamma=gamma,
                        coef0=coef0, shrinking=True)

    resultsfile = open('model_OneClassSVM' +
                       '__kernel_' + str(kernel) +
                       '__nu_' + str(nu) +
                       '__degree_' + str(degree) +
                       '__gamma_' + str(gamma) +
                       '__coef0_' + str(coef0) +
                       '__shrinking_' + str(shrinking),
                       'w')

    return model 
Example #18
Source File: test_skl_to_pmml_UnitTest.py    From nyoka with Apache License 2.0 5 votes vote down vote up
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)) 
Example #19
Source File: _validateSchema.py    From nyoka with Apache License 2.0 5 votes vote down vote up
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) 
Example #20
Source File: testScoreWithAdapaSklearn.py    From nyoka with Apache License 2.0 5 votes vote down vote up
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) 
Example #21
Source File: OneVsRest.py    From mHTM with MIT License 5 votes vote down vote up
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)) 
Example #22
Source File: test_svm.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
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_) 
Example #23
Source File: test_svm.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
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) 
Example #24
Source File: test_svm.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
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_) 
Example #25
Source File: test_sparse.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
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) 
Example #26
Source File: test_sparse.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
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) 
Example #27
Source File: test_svm_one_class.py    From monasca-analytics with Apache License 2.0 5 votes vote down vote up
def test_learn_structure(self):
        data = self.get_testing_data()
        clf = self.svm.learn_structure(data)
        self.assertIsInstance(clf, svm.OneClassSVM) 
Example #28
Source File: svm_one_class.py    From monasca-analytics with Apache License 2.0 5 votes vote down vote up
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 
Example #29
Source File: experiments.py    From AnomalyDetectionTransformations with MIT License 4 votes vote down vote up
def _cae_ocsvm_experiment(dataset_load_fn, dataset_name, single_class_ind, gpu_q):
    gpu_to_use = gpu_q.get()
    os.environ["CUDA_VISIBLE_DEVICES"] = gpu_to_use

    (x_train, y_train), (x_test, y_test) = dataset_load_fn()

    n_channels = x_train.shape[get_channels_axis()]
    input_side = x_train.shape[2]  # channel side will always be at shape[2]
    enc = conv_encoder(input_side, n_channels)
    dec = conv_decoder(input_side, n_channels)
    x_in = Input(shape=x_train.shape[1:])
    x_rec = dec(enc(x_in))
    cae = Model(x_in, x_rec)
    cae.compile('adam', 'mse')

    x_train_task = x_train[y_train.flatten() == single_class_ind]
    x_test_task = x_test[y_test.flatten() == single_class_ind]  # This is just for visual monitoring
    cae.fit(x=x_train_task, y=x_train_task, batch_size=128, epochs=200, validation_data=(x_test_task, x_test_task))

    x_train_task_rep = enc.predict(x_train_task, batch_size=128)
    if dataset_name in ['cats-vs-dogs']:  # OC-SVM is quadratic on the number of examples, so subsample training set
        subsample_inds = np.random.choice(len(x_train_task_rep), 2500, replace=False)
        x_train_task_rep = x_train_task_rep[subsample_inds]

    x_test_rep = enc.predict(x_test, batch_size=128)
    pg = ParameterGrid({'nu': np.linspace(0.1, 0.9, num=9),
                        'gamma': np.logspace(-7, 2, num=10, base=2)})

    results = Parallel(n_jobs=6)(
        delayed(_train_ocsvm_and_score)(d, x_train_task_rep, y_test.flatten() == single_class_ind, x_test_rep)
        for d in pg)

    best_params, best_auc_score = max(zip(pg, results), key=lambda t: t[-1])
    print(best_params)
    best_ocsvm = OneClassSVM(**best_params).fit(x_train_task_rep)
    scores = best_ocsvm.decision_function(x_test_rep)
    labels = y_test.flatten() == single_class_ind

    res_file_name = '{}_cae-oc-svm_{}_{}.npz'.format(dataset_name,
                                                     get_class_name_from_index(single_class_ind, dataset_name),
                                                     datetime.now().strftime('%Y-%m-%d-%H%M'))
    res_file_path = os.path.join(RESULTS_DIR, dataset_name, res_file_name)
    save_roc_pr_curve_data(scores, labels, res_file_path)

    gpu_q.put(gpu_to_use) 
Example #30
Source File: ocsvm.py    From pyod with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def fit(self, X, y=None, sample_weight=None, **params):
        """Fit detector. y is ignored in unsupervised methods.

        Parameters
        ----------
        X : numpy array of shape (n_samples, n_features)
            The input samples.

        y : Ignored
            Not used, present for API consistency by convention.

        sample_weight : array-like, shape (n_samples,)
            Per-sample weights. Rescale C per sample. Higher weights
            force the classifier to put more emphasis on these points.

        Returns
        -------
        self : object
            Fitted estimator.
        """
        # validate inputs X and y (optional)
        X = check_array(X)
        self._set_n_classes(y)

        self.detector_ = OneClassSVM(kernel=self.kernel,
                                     degree=self.degree,
                                     gamma=self.gamma,
                                     coef0=self.coef0,
                                     tol=self.tol,
                                     nu=self.nu,
                                     shrinking=self.shrinking,
                                     cache_size=self.cache_size,
                                     verbose=self.verbose,
                                     max_iter=self.max_iter)
        self.detector_.fit(X=X, y=y, sample_weight=sample_weight,
                           **params)

        # invert decision_scores_. Outliers comes with higher outlier scores
        self.decision_scores_ = invert_order(
            self.detector_.decision_function(X))
        self._process_decision_scores()
        return self