liveBook · Manning

Labels_

As explained in Outlier Detection in Python

labels_ is a fitted PyOD estimator attribute containing binary inlier or outlier labels for observations.

labels_ is a fitted PyOD estimator attribute containing one binary inlier-or-outlier label for each training-data row. It returns assignments rather than numeric outlier scores. section 6.1

Use labels_ when fitting and inspecting the same data; use prediction methods for separate test data. Numeric detectors still require encoded categorical features, filled missing values, and usually scaled numeric features. 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()

A standard PyOD KNN workflow provides a practical setting for inspecting fitted-detector outputs alongside preprocessing and related prediction APIs. section 6.1

How Outlier Detection in Python covers it

How deep it goes
Defines the attribute and its training-data scope, but leaves assignment mechanics to the fitted detector.
Examples
Uses PyOD KNN on synthetic clustered data, contrasting binary predictions with training and testing outlier scores.
What it assumes
Requires a fitted PyOD detector and prepared numeric inputs with encoded categories, filled missing values, and usually scaled features.
What you can do after
Evaluate fitted-detector assignments for training observations before interpreting numeric outlier scores or separate test predictions.
How it connects
Connects fitted estimators with training data, decision scores, prediction, and outlier detection.

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.