原文:One-Class Classification Alogrithms for Imbalanced Datasets
In this tutorial, you will discover how to use one-class classification algorithms for datasets with severely skewed class distribution.
After completing this tutorial, you will know:
- One-class classification is a field of machine learning that provides techniques for outlier and anomaly detection.
- How to adapt one-class classification algorithms for imbalanced classification with a severely skewed class distribution.
- How to fit and evaluate one-class classification algorithms such as SVM, isolation forest, elliptic envelope, and local outlier factor.
目录
- One-Class classification for Imbalanced Data;
- One-Class Support Vector Machines;
1. One-Class classification for Imbalanced Data;
The presence of outliers can cause problems. For example, a single variable may have an outlier far from the mass of examples, which can skew summary statistics such as the mean and variance.
One-Class classification (OCC) involves fitting a model on the “normal” data and predicting whether new data is normal or an anomaly.
“A one-class classifier aims at caputring characteristics of training instances, in order to be able to distinguish between them and potential outliers to appera.” — Page 139, Learning from Imbalanced Data Sets, 2018.
We will use the make_classification() function to create 9990 examples in majority class and 10 in the minority class.
# Generate and plot a synthetic imbalanced classification dataset
from collections import Counter
from sklearn.datasets import make_classification
from matplotlib import pyplot
from numpy import where
# define dataset
X, y = make_classification(n_samples=10000, n_features=2, n_redundant=0,
n_clusters_per_class=1, weights=[0.999], flip_y=0, random_state=4)
# summarize class distribution
counter = Counter(y)
print(counter)
# scatter plot of examples by class label
for label, _ in counter.items():
row_ix = where(y == label)[0]
pyplot.scatter(X[row_ix, 0], X[row_ix, 1], label=str(label))
pyplot.legend()
pyplot.show()
2. One-Class Support Vector Machines
The main difference from a standard SVM is that it is fit in an unsupervised manner and does not provide the normal hyperparameters for tuning the margin.
from sklearn.svm import OneClassSVM
model = OneClassSVM(gamma='scale', nu=0.01)
trainX = X[y==0]
model.git(trainX)
testX = X[y==1]
print(model.predict(testX)
array([-1, -1, -1, -1, 1, -1, -1, 1, -1, -1])