Continuous contour plot

73 Views Asked by At

This is my code to find outliers. I would like to plot a continuous contour line over no outliers. What I'm getting is two isolated contour lines. So, I want to get only one contour (connected contour) no matter the shape. Thanks. `

import numpy as np
from sklearn.svm import OneClassSVM
import matplotlib.pyplot as plt

ss = [[9.0, 1.0],[1.78, 2.14],[7.36, 2.67],[5.95, 2.5 ],[2.59, 2.87],[1.76, 2.45],[1.87, 2.45],[2.15, 2.61],[1.64, 2.17],[1.35, 2.27],
      [2.16, 2.3 ],[1.48, 2.32],[1.73, 2.41],[1.73, 2.39],[3.87, 1.38],[1.81, 2.7 ],[1.92, 2.72],[1.57, 2.62],[1.59, 2.48],[3.1 , 2.56]]
ssa = np.asarray(ss)
X1 = ssa
# Learn a frontier for outlier detection with several classifiers
xx1, yy1 = np.meshgrid(np.linspace(0, 10, 500), np.linspace(0, 6, 500))
model_1 = OneClassSVM(nu=0.25, gamma=0.35)
model_1.fit(X1) # clf.fit(X1)

Z1 = model_1.decision_function(np.c_[xx1.ravel(), yy1.ravel()]) # Z1 = clf.decision_function(np.c_[xx1.ravel(), yy1.ravel()])
Z1 = Z1.reshape(xx1.shape)

plt.figure(1)
plt.contour(xx1, yy1, Z1, levels=[0], linewidths=2, colors='r') #'''
plt.scatter(X1[:, 0], X1[:, 1], color="black")
plt.show()

` enter image description here

I would like to get a plot like this

enter image description here

1

There are 1 best solutions below

0
YaOzI On

The key is not about how to let matplotlib draw, is about how to let the model (OneClassSVM) generate what we want.

And since you are using unsupervised learning technique, sometimes we have to adjust the model's parameters to get what we think is the best (as a outside "supervisor").

So in your case, by using OneClassSVM with default kernel RBF, you can play around nu, gamma, tol etc:

model_1 = OneClassSVM(nu=0.25, gamma=0.12)

enter image description here