import matplotlib.font_manager
import matplotlib.lines as mlines
import matplotlib.pyplot as plt
from sklearn.inspection import DecisionBoundaryDisplay
_ , ax = plt . subplots ()
xx , yy = np . meshgrid ( np . linspace ( - 5 , 5 , 10 ), np . linspace ( - 5 , 5 , 10 ))
X = np . concatenate ([ xx . reshape ( - 1 , 1 ), yy . reshape ( - 1 , 1 )], axis = 1 )
DecisionBoundaryDisplay . from_estimator (
clf ,
X ,
response_method = "decision_function" ,
plot_method = "contourf" ,
ax = ax ,
cmap = "PuBu" ,
)
DecisionBoundaryDisplay . from_estimator (
clf ,
X ,
response_method = "decision_function" ,
plot_method = "contourf" ,
ax = ax ,
levels = [ 0 , 10000 ],
colors = "palevioletred" ,
)
DecisionBoundaryDisplay . from_estimator (
clf ,
X ,
response_method = "decision_function" ,
plot_method = "contour" ,
ax = ax ,
levels = [ 0 ],
colors = "darkred" ,
linewidths = 2 ,
)
s = 40
b1 = ax . scatter ( X_train [:, 0 ], X_train [:, 1 ], c = "white" , s = s , edgecolors = "k" )
b2 = ax . scatter ( X_test [:, 0 ], X_test [:, 1 ], c = "blueviolet" , s = s , edgecolors = "k" )
c = ax . scatter ( X_outliers [:, 0 ], X_outliers [:, 1 ], c = "gold" , s = s , edgecolors = "k" )
plt . legend (
[ mlines . Line2D ([], [], color = "darkred" ), b1 , b2 , c ],
[
"learned frontier" ,
"training observations" ,
"new regular observations" ,
"new abnormal observations" ,
],
loc = "upper left" ,
prop = matplotlib . font_manager . FontProperties ( size = 11 ),
)
ax . set (
xlabel = (
f "error train: { n_error_train } /200 ; errors novel regular: { n_error_test } /40 ;"
f " errors novel abnormal: { n_error_outliers } /40"
),
title = "Novelty Detection" ,
xlim = ( - 5 , 5 ),
ylim = ( - 5 , 5 ),
)
plt . show ()