Created
February 27, 2018 15:00
-
-
Save twolodzko/af2daac3e37f02be4d9d07390d9e5e87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.base import BaseEstimator, ClassifierMixin | |
from sklearn.ensemble import IsolationForest | |
from sklearn.covariance import EllipticEnvelope | |
from sklearn.svm import OneClassSVM | |
def mode(x): | |
(values, counts) = np.unique(x, return_counts = True) | |
idx = np.argmax(counts) | |
return values[idx] | |
class AnomalyClassifier(BaseEstimator, ClassifierMixin): | |
''' | |
Use estimator to classify the majority class in y as inliers | |
and the minority class as outliers. If target variable | |
y is not used, use X dataset to fit the anomaly detection | |
estimator. | |
''' | |
def __init__(self, estimator): | |
self.estimator = estimator | |
def fit(self, X, y = None): | |
if y is not None: | |
idx = y == mode(y) | |
X = X[idx, :].copy() | |
y = y[idx].copy() | |
return self.estimator.fit(X, y) | |
def predict(self, X): | |
return (self.estimator.predict(X) == -1).astype(int) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment