8000 Fix #7141 · scikit-learn/scikit-learn@1a657e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a657e6

Browse files
committed
Fix #7141
Allow small discripance for decision function as suggested in #16721.
1 parent b4757f7 commit 1a657e6

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

sklearn/ensemble/_iforest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def predict(self, X):
331331
check_is_fitted(self)
332332
X = check_array(X, accept_sparse='csr')
333333
is_inlier = np.ones(X.shape[0], dtype=int)
334-
is_inlier[self.decision_function(X) < 0] = -1
334+
is_inlier[self.decision_function(X) < -1e-15] = -1
335335
return is_inlier
336336

337337
def decision_function(self, X):

sklearn/ensemble/tests/test_iforest.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -327,32 +327,33 @@ def test_iforest_deprecation():
327327
def test_iforest_with_uniform_data():
328328
"""Test whether iforest predicts inliers when using uniform data"""
329329

330-
# 2-d array of all 1s
331-
X = np.ones((100, 10))
332-
iforest = IsolationForest()
333-
iforest.fit(X)
334-
335-
rng = np.random.RandomState(0)
336-
337-
assert all(iforest.predict(X) == 1)
338-
assert all(iforest.predict(rng.randn(100, 10)) == 1)
339-
assert all(iforest.predict(X + 1) == 1)
340-
assert all(iforest.predict(X - 1) == 1)
341-
342-
# 2-d array where columns contain the same value across rows
343-
X = np.repeat(rng.randn(1, 10), 100, 0)
344-
iforest = IsolationForest()
345-
iforest.fit(X)
346-
347-
assert all(iforest.predict(X) == 1)
348-
assert all(iforest.predict(rng.randn(100, 10)) == 1)
349-
assert all(iforest.predict(np.ones((100, 10))) == 1)
350-
351-
# Single row
352-
X = rng.randn(1, 10)
353-
iforest = IsolationForest()
354-
iforest.fit(X)
355-
356-
assert all(iforest.predict(X) == 1)
357-
assert all(iforest.predict(rng.randn(100, 10)) == 1)
358-
assert all(iforest.predict(np.ones((100, 10))) == 1)
330+
for n_samples, n_features in np.random.randint(10, 200, size=(10, 2)):
331+
# 2-d array of all 1s
332+
X = np.ones((n_samples, n_features))
333+
iforest = IsolationForest()
334+
iforest.fit(X)
335+
336+
rng = np.random.RandomState(0)
337+
338+
assert all(iforest.predict(X) == 1)
339+
assert all(iforest.predict(rng.randn(n_samples, n_features)) == 1)
340+
assert all(iforest.predict(X + 1) == 1)
341+
assert all(iforest.predict(X - 1) == 1)
342+
343+
# 2-d array where columns contain the same value across rows
344+
X = np.repeat(rng.randn(1, n_features), n_samples, 0)
345+
iforest = IsolationForest()
346+
iforest.fit(X)
347+
348+
assert all(iforest.predict(X) == 1)
349+
assert all(iforest.predict(rng.randn(n_samples, n_features)) == 1)
350+
assert all(iforest.predict(np.ones((n_samples, n_features))) == 1)
351+
352+
# Single row
353+
X = rng.randn(1, n_features)
354+
iforest = IsolationForest()
355+
iforest.fit(X)
356+
357+
assert all(iforest.predict(X) == 1)
358+
assert all(iforest.predict(rng.randn(n_samples, n_features)) == 1)
359+
assert all(iforest.predict(np.ones((n_samples, n_features))) == 1)

0 commit comments

Comments
 (0)
0