From f107d004937100d1d49d5c19e3a3f9230604e582 Mon Sep 17 00:00:00 2001 From: nonunicorn Date: Fri, 16 Dec 2022 16:18:41 +0000 Subject: [PATCH] Using OneClassSVM for outliers detection example #1890 --- ...classsvm-for-outliers-detection-example.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 python-scikit-learn/using-oneclasssvm-for-outliers-detection-example.md diff --git a/python-scikit-learn/using-oneclasssvm-for-outliers-detection-example.md b/python-scikit-learn/using-oneclasssvm-for-outliers-detection-example.md new file mode 100644 index 000000000..35648247c --- /dev/null +++ b/python-scikit-learn/using-oneclasssvm-for-outliers-detection-example.md @@ -0,0 +1,34 @@ +# Using OneClassSVM for outliers detection example + +```python +from sklearn import svm + +X = [[0.32], [0.31], [0], [0.32], [0.31], [1], [0.32], [0.31]] + +m = svm.OneClassSVM(gamma='auto').fit(X) +detected = m.predict(X) +``` + +- `from sklearn import` - import module from [lib:scikit-learn](https://onelinerhub.com/python-scikit-learn/how-to-install-scikit-learn-using-pip) +- `X = ` - example values set to detect outliers for +- `.OneClassSVM(` - creates unsupervised outlier detection model +- `.fit(` - train transformation model +- `.predict(` - predict target variable based on given features dataset +- `detected` - will contain a list of attributed outliers (`-1`) and values that are ok (`1`) + +## Example: +```python +from sklearn import svm + +X = [[0.32], [0.31], [0], [0.32], [0.31], [1], [0.32], [0.31]] + +m = svm.OneClassSVM(gamma='auto').fit(X) +detected = m.predict(X) + +print(detected) +``` +``` +[ 1 1 -1 1 1 -1 1 1] + +``` +