Python sklearn.covariance.EllipticEnvelope() Examples

The following are 12 code examples of sklearn.covariance.EllipticEnvelope(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.covariance , or try the search function .
Example #1
Source File: test_elliptic_envelope.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_elliptic_envelope():
    rnd = np.random.RandomState(0)
    X = rnd.randn(100, 10)
    clf = EllipticEnvelope(contamination=0.1)
    assert_raises(NotFittedError, clf.predict, X)
    assert_raises(NotFittedError, clf.decision_function, X)
    clf.fit(X)
    y_pred = clf.predict(X)
    scores = clf.score_samples(X)
    decisions = clf.decision_function(X)

    assert_array_almost_equal(
        scores, -clf.mahalanobis(X))
    assert_array_almost_equal(clf.mahalanobis(X), clf.dist_)
    assert_almost_equal(clf.score(X, np.ones(100)),
                        (100 - y_pred[y_pred == -1].size) / 100.)
    assert(sum(y_pred == -1) == sum(decisions < 0)) 
Example #2
Source File: outlier_detection.py    From JusticeAI with MIT License 6 votes vote down vote up
def initialize_fact_model(self):
        # Extract all RASA training sentences for all facts
        rasa_fact_paths = glob.glob(self.RASA_FACT_DIR + '*.json')
        all_sentences = []
        for fact_path in rasa_fact_paths:
            with open(fact_path, 'r') as f:
                file_json = json.loads(f.read().encode('utf-8'))
            for example in file_json['rasa_nlu_data']['common_examples']:
                all_sentences.append(example['text'].lower())

        # TF-IDF model
        tfidf_vectorizer = TfidfVectorizer(ngram_range=self.NGRAM_RANGE, strip_accents='ascii')
        X_tfidf = tfidf_vectorizer.fit_transform(all_sentences)

        # Fit to robust covariance estimation
        outlier_estimator = EllipticEnvelope(contamination=self.CONTAMINATION)
        outlier_estimator.fit(X_tfidf.toarray())

        # Binarize for future use
        with open(self.TFIFD_PICKLE_FILE, 'wb') as f:
            joblib.dump(tfidf_vectorizer, f, compress=True)
        with open(self.OUTLIER_PICKLE_FILE, 'wb') as f:
            joblib.dump(outlier_estimator, f, compress=True) 
Example #3
Source File: outlier_filtering.py    From enhancement_proposals with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def fit_pipe(self, X, y=None):
        self.elliptic_envelope_ = EllipticEnvelope(**self.get_params())
        self.elliptic_envelope_.fit(X)
        return self.transform_pipe(X, y) 
Example #4
Source File: elliptic_envelope.py    From monasca-analytics with Apache License 2.0 5 votes vote down vote up
def __init__(self, _id, _config):
        super(EllipticEnvelope, self).__init__(_id, _config)
        self._nb_samples = int(_config['nb_samples']) 
Example #5
Source File: elliptic_envelope.py    From monasca-analytics with Apache License 2.0 5 votes vote down vote up
def get_default_config():
        return {
            'module': EllipticEnvelope.__name__,
            'nb_samples': N_SAMPLES
        } 
Example #6
Source File: elliptic_envelope.py    From monasca-analytics with Apache License 2.0 5 votes vote down vote up
def _get_best_detector(self, train):
        detector = covariance.EllipticEnvelope()
        detector.fit(train)
        return detector 
Example #7
Source File: test_elliptic_envelope.py    From monasca-analytics with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        super(TestEllipticEnvelope, self).setUp()
        self.ee_sml = elliptic_envelope.EllipticEnvelope(
            "fakeid", {"module": "fake", "nb_samples": 1000}) 
Example #8
Source File: test_elliptic_envelope.py    From monasca-analytics with Apache License 2.0 5 votes vote down vote up
def test_learn_structure(self):
        data = self.get_testing_data()
        clf = self.ee_sml.learn_structure(data)
        self.assertIsInstance(clf, covariance.EllipticEnvelope) 
Example #9
Source File: test_elliptic_envelope.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_score_samples():
    X_train = [[1, 1], [1, 2], [2, 1]]
    clf1 = EllipticEnvelope(contamination=0.2).fit(X_train)
    clf2 = EllipticEnvelope().fit(X_train)
    assert_array_equal(clf1.score_samples([[2., 2.]]),
                       clf1.decision_function([[2., 2.]]) + clf1.offset_)
    assert_array_equal(clf2.score_samples([[2., 2.]]),
                       clf2.decision_function([[2., 2.]]) + clf2.offset_)
    assert_array_equal(clf1.score_samples([[2., 2.]]),
                       clf2.score_samples([[2., 2.]])) 
Example #10
Source File: test_elliptic_envelope.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_raw_values_deprecation():
    X = [[0.0], [1.0]]
    clf = EllipticEnvelope().fit(X)
    assert_warns_message(DeprecationWarning,
                         "raw_values parameter is deprecated in 0.20 and will"
                         " be removed in 0.22.",
                         clf.decision_function, X, raw_values=True) 
Example #11
Source File: test_elliptic_envelope.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_threshold_deprecation():
    X = [[0.0], [1.0]]
    clf = EllipticEnvelope().fit(X)
    assert_warns_message(DeprecationWarning,
                         "threshold_ attribute is deprecated in 0.20 and will"
                         " be removed in 0.22.",
                         getattr, clf, "threshold_") 
Example #12
Source File: test_covariance.py    From pandas-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_objectmapper(self):
        df = pdml.ModelFrame([])
        self.assertIs(df.covariance.EmpiricalCovariance, covariance.EmpiricalCovariance)
        self.assertIs(df.covariance.EllipticEnvelope, covariance.EllipticEnvelope)
        self.assertIs(df.covariance.GraphLasso, covariance.GraphLasso)
        self.assertIs(df.covariance.GraphLassoCV, covariance.GraphLassoCV)
        self.assertIs(df.covariance.LedoitWolf, covariance.LedoitWolf)
        self.assertIs(df.covariance.MinCovDet, covariance.MinCovDet)
        self.assertIs(df.covariance.OAS, covariance.OAS)
        self.assertIs(df.covariance.ShrunkCovariance, covariance.ShrunkCovariance)

        self.assertIs(df.covariance.shrunk_covariance, covariance.shrunk_covariance)
        self.assertIs(df.covariance.graph_lasso, covariance.graph_lasso)