Add IsolationForest as a SML
This patch adds a SML which uses IsolationForest algorithm (unsupervised). Change-Id: I77a288e530bd38544d2cce9bf9ed6bdda235b218
This commit is contained in:
parent
f695f22ece
commit
6bf6c14130
|
@ -0,0 +1,87 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
# Copyright (c) 2016 FUJITSU LIMITED
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import logging
|
||||
|
||||
import numpy as np
|
||||
from sklearn import ensemble
|
||||
import voluptuous
|
||||
|
||||
from monasca_analytics.sml.base import BaseSML
|
||||
from monasca_analytics.util.validation_utils import NoSpaceCharacter
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
ANOMALY = -1
|
||||
NON_ANOMALY = 1
|
||||
N_SAMPLES = 1000
|
||||
|
||||
|
||||
class IsolationForest(BaseSML):
|
||||
"""Anomaly detection based on the IsolationForest algorithm"""
|
||||
|
||||
def __init__(self, _id, _config):
|
||||
super(IsolationForest, self).__init__(_id, _config)
|
||||
self._nb_samples = int(_config['nb_samples'])
|
||||
|
||||
@staticmethod
|
||||
def validate_config(_config):
|
||||
isolation_schema = voluptuous.Schema({
|
||||
'module': voluptuous.And(
|
||||
basestring, NoSpaceCharacter()),
|
||||
'nb_samples': voluptuous.Or(float, int)
|
||||
}, required=True)
|
||||
return isolation_schema(_config)
|
||||
|
||||
@staticmethod
|
||||
def get_default_config():
|
||||
return {
|
||||
'module': IsolationForest.__name__,
|
||||
'nb_samples': N_SAMPLES
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def get_params():
|
||||
return [
|
||||
params.ParamDescriptor('nb_samples', type_util.Number(), N_SAMPLES)
|
||||
]
|
||||
|
||||
def number_of_samples_required(self):
|
||||
return self._nb_samples
|
||||
|
||||
def _generate_train_test_sets(self, samples, ratio_train):
|
||||
num_samples_train = int(len(samples) * ratio_train)
|
||||
X_train = np.array(samples[:num_samples_train])
|
||||
X_test = np.array(samples[num_samples_train:])
|
||||
return X_train, X_test
|
||||
|
||||
def _get_best_detector(self, train):
|
||||
detector = ensemble.IsolationForest()
|
||||
detector.fit(train)
|
||||
return detector
|
||||
|
||||
def learn_structure(self, samples):
|
||||
X_train, X_test = self._generate_train_test_sets(samples, 0.75)
|
||||
logger.info('Trainig with ' + str(len(X_train)) +
|
||||
'samples; testing with ' + str(len(X_test)) + ' samples.')
|
||||
|
||||
if_detector = self._get_best_detector(X_train)
|
||||
Y_test = if_detector.predict(X_test)
|
||||
|
||||
num_anomalies = Y_test[Y_test == ANOMALY].size
|
||||
logger.info('Found ' + str(num_anomalies) +
|
||||
' anomalies in testing set')
|
||||
return if_detector
|
|
@ -0,0 +1,54 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
# Copyright (c) 2016 Hewlett Packard Enterprise Development Company, L.P.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import logging
|
||||
|
||||
import numpy as np
|
||||
from sklearn import ensemble
|
||||
|
||||
from monasca_analytics.sml import isolation_forest
|
||||
from test.util_for_testing import MonanasTestCase
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestIsolationForest(MonanasTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestIsolationForest, self).setUp()
|
||||
self.if_sml = isolation_forest.IsolationForest(
|
||||
"fakeid", {"module": "fake", "nb_samples": 1000})
|
||||
|
||||
def tearDown(self):
|
||||
super(TestIsolationForest, self).tearDown()
|
||||
|
||||
def get_testing_data(self):
|
||||
a = np.random.uniform(size=1000)
|
||||
b = np.random.uniform(size=1000)
|
||||
c = np.random.uniform(size=1000)
|
||||
d = np.random.uniform(size=1000)
|
||||
return np.array([a, b, c, d]).T
|
||||
|
||||
def test_generate_train_test_sets(self):
|
||||
data = self.get_testing_data()
|
||||
train, test = self.if_sml._generate_train_test_sets(data, 0.6)
|
||||
self.assertEqual(600, len(train))
|
||||
self.assertEqual(400, len(test))
|
||||
|
||||
def test_learn_structure(self):
|
||||
data = self.get_testing_data()
|
||||
clf = self.if_sml.learn_structure(data)
|
||||
self.assertIsInstance(clf, ensemble.IsolationForest)
|
|
@ -109,7 +109,7 @@ class CommonUtilTest(unittest.TestCase):
|
|||
def test_get_available_sml_class_names(self):
|
||||
names = common_util.get_available_sml_class_names()
|
||||
self.assertItemsEqual(
|
||||
['LiNGAM', "SvmOneClass"],
|
||||
['LiNGAM', "SvmOneClass", "IsolationForest"],
|
||||
names)
|
||||
|
||||
def test_get_voter_class_by_name(self):
|
||||
|
|
Loading…
Reference in New Issue