Skip to content

Instantly share code, notes, and snippets.

@hlin117
Last active August 29, 2015 14:25
Show Gist options
  • Save hlin117/aadd90768fc09ba0e9d7 to your computer and use it in GitHub Desktop.
Save hlin117/aadd90768fc09ba0e9d7 to your computer and use it in GitHub Desktop.
scikit-learn bug #4942 test
from time import time
import numpy as np
from sklearn import svm
print("Starting the timer")
start = time()
xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500))
# Generate train data
X = 0.3 * np.random.randn(100000, 2)
X_train = np.r_[X + 2, X - 2]
# Generate some regular novel observations
X = 0.3 * np.random.randn(20, 2)
X_test = np.r_[X + 2, X - 2]
# Generate some abnormal novel observations
X_outliers = np.random.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)
y_pred_train = clf.predict(X_train)
y_pred_test = clf.predict(X_test)
y_pred_outliers = clf.predict(X_outliers)
n_error_train = y_pred_train[y_pred_train == -1].size
n_error_test = y_pred_test[y_pred_test == -1].size
n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size
# plot the line, the points, and the nearest vectors to the plane
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
print("Took {0} time to run this script".format(time() - start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment