本文整理汇总了Python中sklearn.ensemble.IsolationForest方法的典型用法代码示例。如果您正苦于以下问题:Python ensemble.IsolationForest方法的具体用法?Python ensemble.IsolationForest怎么用?Python ensemble.IsolationForest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble
的用法示例。
在下文中一共展示了ensemble.IsolationForest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def __init__(self, hybrid=False, n_estimators=100, max_samples='auto', contamination=0.1, n_jobs=-1, seed=None,
**kwargs):
"""Init Isolation Forest instance."""
self.n_estimators = n_estimators
self.max_samples = max_samples
self.contamination = contamination
self.n_jobs = n_jobs
self.seed = seed
self.model = IsolationForest(n_estimators=n_estimators, max_samples=max_samples, contamination=contamination,
n_jobs=n_jobs, random_state=seed, **kwargs)
self.hybrid = hybrid
self.ae_net = None # autoencoder network for the case of a hybrid model
self.results = {
'train_time': None,
'test_time': None,
'test_auc': None,
'test_scores': None
}
示例2: test_iforest_sparse
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def test_iforest_sparse():
"""Check IForest for various parameter settings on sparse input."""
rng = check_random_state(0)
X_train, X_test, y_train, y_test = train_test_split(boston.data[:50],
boston.target[:50],
random_state=rng)
grid = ParameterGrid({"max_samples": [0.5, 1.0],
"bootstrap": [True, False]})
for sparse_format in [csc_matrix, csr_matrix]:
X_train_sparse = sparse_format(X_train)
X_test_sparse = sparse_format(X_test)
for params in grid:
# Trained on sparse format
sparse_classifier = IsolationForest(
n_estimators=10, random_state=1, **params).fit(X_train_sparse)
sparse_results = sparse_classifier.predict(X_test_sparse)
# Trained on dense format
dense_classifier = IsolationForest(
n_estimators=10, random_state=1, **params).fit(X_train)
dense_results = dense_classifier.predict(X_test)
assert_array_equal(sparse_results, dense_results)
示例3: test_iforest_performance
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def test_iforest_performance():
"""Test Isolation Forest performs well"""
# Generate train/test data
rng = check_random_state(2)
X = 0.3 * rng.randn(120, 2)
X_train = np.r_[X + 2, X - 2]
X_train = X[:100]
# Generate some abnormal novel observations
X_outliers = rng.uniform(low=-4, high=4, size=(20, 2))
X_test = np.r_[X[100:], X_outliers]
y_test = np.array([0] * 20 + [1] * 20)
# fit the model
clf = IsolationForest(max_samples=100, random_state=rng).fit(X_train)
# predict scores (the lower, the more normal)
y_pred = - clf.decision_function(X_test)
# check that there is at most 6 errors (false positive or false negative)
assert_greater(roc_auc_score(y_test, y_pred), 0.98)
示例4: test_deprecation
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def test_deprecation():
X = [[0.0], [1.0]]
clf = IsolationForest()
assert_warns_message(FutureWarning,
'default contamination parameter 0.1 will change '
'in version 0.22 to "auto"',
clf.fit, X)
assert_warns_message(FutureWarning,
'behaviour="old" is deprecated and will be removed '
'in version 0.22',
clf.fit, X)
clf = IsolationForest().fit(X)
assert_warns_message(DeprecationWarning,
"threshold_ attribute is deprecated in 0.20 and will"
" be removed in 0.22.",
getattr, clf, "threshold_")
示例5: sample_hyps_iso_forest
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def sample_hyps_iso_forest(nest, contam, boot):
"""
:param nest:
:param contam:
:param boot:
:return: An IsolationForest object with specified hyperparameters, used to detect anomaly.
"""
n_estimators = nest # random.choice(range(20, 300)) # default is 100
max_samples = 'auto'
contamination = contam #randrange_float(0.0, 0.5, 0.05)
max_features = 1.0 # default is 1.0 (use all features)
bootstrap = boot # random.choice(['True', 'False'])
n_jobs = -1 # Uses all cores
verbose = 0
model = IsolationForest(n_estimators=n_estimators, max_samples=max_samples,
contamination=contamination, max_features=max_features,
bootstrap=bootstrap, n_jobs=n_jobs, verbose=verbose)
return model
示例6: run_isolation_forest
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def run_isolation_forest(features, id_list, fraction_of_outliers=.3):
"""Performs anomaly detection based on Isolation Forest."""
rng = np.random.RandomState(1984)
num_samples = features.shape[0]
iso_f = IsolationForest(max_samples=num_samples,
contamination=fraction_of_outliers,
random_state=rng)
iso_f.fit(features)
pred_scores = iso_f.decision_function(features)
threshold = stats.scoreatpercentile(pred_scores, 100 * fraction_of_outliers)
outlying_ids = id_list[pred_scores < threshold]
return outlying_ids
示例7: test_iforest_error
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def test_iforest_error():
"""Test that it gives proper exception on deficient input."""
X = iris.data
# Test max_samples
assert_raises(ValueError,
IsolationForest(max_samples=-1).fit, X)
assert_raises(ValueError,
IsolationForest(max_samples=0.0).fit, X)
assert_raises(ValueError,
IsolationForest(max_samples=2.0).fit, X)
# The dataset has less than 256 samples, explicitly setting
# max_samples > n_samples should result in a warning. If not set
# explicitly there should be no warning
assert_warns_message(UserWarning,
"max_samples will be set to n_samples for estimation",
IsolationForest(max_samples=1000).fit, X)
assert_no_warnings(IsolationForest(max_samples='auto').fit, X)
assert_no_warnings(IsolationForest(max_samples=np.int64(2)).fit, X)
assert_raises(ValueError, IsolationForest(max_samples='foobar').fit, X)
assert_raises(ValueError, IsolationForest(max_samples=1.5).fit, X)
示例8: __init__
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def __init__(self, _id, _config):
super(IsolationForest, self).__init__(_id, _config)
self._nb_samples = int(_config['nb_samples'])
示例9: get_default_config
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def get_default_config():
return {
'module': IsolationForest.__name__,
'nb_samples': N_SAMPLES
}
示例10: _get_best_detector
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def _get_best_detector(self, train):
detector = ensemble.IsolationForest()
detector.fit(train)
return detector
示例11: setUp
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def setUp(self):
super(TestIsolationForest, self).setUp()
self.if_sml = isolation_forest.IsolationForest(
"fakeid", {"module": "fake", "nb_samples": 1000})
示例12: test_learn_structure
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def test_learn_structure(self):
data = self.get_testing_data()
clf = self.if_sml.learn_structure(data)
self.assertIsInstance(clf, ensemble.IsolationForest)
示例13: test_iforest
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def test_iforest():
"""Check Isolation Forest for various parameter settings."""
X_train = np.array([[0, 1], [1, 2]])
X_test = np.array([[2, 1], [1, 1]])
grid = ParameterGrid({"n_estimators": [3],
"max_samples": [0.5, 1.0, 3],
"bootstrap": [True, False]})
with ignore_warnings():
for params in grid:
IsolationForest(random_state=rng,
**params).fit(X_train).predict(X_test)
示例14: test_iforest_error
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def test_iforest_error():
"""Test that it gives proper exception on deficient input."""
X = iris.data
# Test max_samples
assert_raises(ValueError,
IsolationForest(max_samples=-1).fit, X)
assert_raises(ValueError,
IsolationForest(max_samples=0.0).fit, X)
assert_raises(ValueError,
IsolationForest(max_samples=2.0).fit, X)
# The dataset has less than 256 samples, explicitly setting
# max_samples > n_samples should result in a warning. If not set
# explicitly there should be no warning
assert_warns_message(UserWarning,
"max_samples will be set to n_samples for estimation",
IsolationForest(max_samples=1000).fit, X)
# note that assert_no_warnings does not apply since it enables a
# PendingDeprecationWarning triggered by scipy.sparse's use of
# np.matrix. See issue #11251.
with pytest.warns(None) as record:
IsolationForest(max_samples='auto').fit(X)
user_warnings = [each for each in record
if issubclass(each.category, UserWarning)]
assert len(user_warnings) == 0
with pytest.warns(None) as record:
IsolationForest(max_samples=np.int64(2)).fit(X)
user_warnings = [each for each in record
if issubclass(each.category, UserWarning)]
assert len(user_warnings) == 0
assert_raises(ValueError, IsolationForest(max_samples='foobar').fit, X)
assert_raises(ValueError, IsolationForest(max_samples=1.5).fit, X)
# test X_test n_features match X_train one:
assert_raises(ValueError, IsolationForest().fit(X).predict, X[:, 1:])
# test threshold_ attribute error when behaviour is not old:
msg = "threshold_ attribute does not exist when behaviour != 'old'"
assert_raises_regex(AttributeError, msg, getattr,
IsolationForest(behaviour='new'), 'threshold_')
示例15: test_recalculate_max_depth
# 需要导入模块: from sklearn import ensemble [as 别名]
# 或者: from sklearn.ensemble import IsolationForest [as 别名]
def test_recalculate_max_depth():
"""Check max_depth recalculation when max_samples is reset to n_samples"""
X = iris.data
clf = IsolationForest().fit(X)
for est in clf.estimators_:
assert_equal(est.max_depth, int(np.ceil(np.log2(X.shape[0]))))