A common API provides a consistent interface for using different detector implementations. In PyOD, the usual workflow is to create a detector, fit the data, and obtain scores or labels. section 6.1
The standard operations are:
- fit() trains the detector on supplied data. section 6.1
- predict() returns binary labels for test records. section 6.1
- decision_function() returns numeric outlier scores. section 6.1
- predict_proba() rescales scores, but its outputs are not true probabilities. section 6.1
KNN detector workflow
Create a KNN detector, fit it with data, and read decision_scores_ to obtain scores for the training records. The same pattern also applies to PyOD's KDE detector. section 6.1
Listing 6.3 Example of the standard PyOD API using KNN
import pandas as pd
import numpy as np
from pyod.models.knn import KNN
from sklearn.preprocessing import RobustScaler
import matplotlib.pyplot as plt
import seaborn as sns
np.random.seed(42)
x_data = np.random.normal(loc=10, scale=1.0, size=1000) #1
y_data = np.random.normal(loc=10, scale=1.0, size=1000)
df1 = pd.DataFrame({'A': x_data, 'B':y_data})
df1['Ground Truth'] = 0
x_data = np.random.normal(loc=8, scale=1.0, size=5) #2
y_data = np.random.normal(loc=8, scale=1.0, size=5)
df2 = pd.DataFrame({'A': x_data, 'B':y_data})
df2['Ground Truth'] = 1
x_data = np.random.normal(loc=1, scale=3.0, size=5) #3
y_data = np.random.normal(loc=1, scale=3.0, size=5)
df3 = pd.DataFrame({'A': x_data, 'B':y_data})
df3['Ground Truth'] = 1
df = pd.concat([df1, df2, df3])
df = df.reset_index()
df = pd.DataFrame(RobustScaler().fit_transform(df), columns=df.columns)
fig, ax = plt.subplots(nrows=1, ncols=4, sharey=True, figsize=(10, 3))
sns.scatterplot(data=df[df['Ground Truth'] == False], x='A', y='B',
alpha=0.1, ax=ax[0]) #4
s = sns.scatterplot(data=df[df['Ground Truth'] == True], x='A', y='B',
alpha=1.0, s=100, marker='*', ax=ax[0])
s.set_title('Ground Truth')
clf = KNN() #5
clf.fit(df[['A', 'B']])
df['KNN Binary Prediction'] = clf.predict(df[['A', 'B']]) #6
sns.scatterplot(data=df[df['KNN Binary Prediction'] == False], x='A',
y='B', alpha=0.1, ax=ax[1])
s = sns.scatterplot(data=df[df['KNN Binary Prediction'] == True], x='A',
y='B', alpha=1.0, s=100, marker='*', ax=ax[1])
s.set_title('Binary Predictions')
df['KNN Decision Scores'] = clf.decision_scores_
s = sns.scatterplot(data=df, x='A', y='B', hue='KNN Decision Scores',
size='KNN Decision Scores', ax=ax[2])
s.get_legend().remove()
s.set_title('Training Decision \nScores')
df['KNN Decision Function'] = clf.decision_function(df[['A', 'B']])
s = sns.scatterplot(data=df, x='A', y='B', hue='KNN Decision Function',
size='KNN Decision Function', ax=ax[3])
s.get_legend().remove()
s.set_title('Decision Function \nScores')
plt.tight_layout()
plt.show()
Shows the standard PyOD API with synthetic inliers and outliers, including fitting, scoring, prediction, and visualization. section 6.1
