scikit-learn v0.20 +中的IsolationForest.threshold_属性是否可以替代?

时间:2019-06-06 19:21:45

标签: python-3.x scikit-learn

我正在更新一些代码以与最新版本的scikit-learn(0.21.2)一起使用。在该代码中,我使用IsolationForest并在拟合后访问.threshold_属性。但是,此属性已被弃用。人们有没有其他选择?

这是"old"的行为。我可以访问.threshold_,但会获得DeprecationWarnings。

from sklearn.ensemble import IsolationForest
from sklearn import datasets
import numpy as np

dataset = datasets.load_iris()
np.random.seed(10)

X_train = dataset.data[:,:2] 
X_test  = np.random.random((10,2))*np.array([6,4])+np.array([3,1]) # fake observations

clf = IsolationForest(max_samples=100, contamination=0.1, random_state=1001, behaviour="old")
clf.fit(X_train)
#> /Users/ncg/anaconda3/envs/sklearntest/lib/python3.6/site-packages/sklearn/ensemble/iforest.py:247: FutureWarning: behaviour="old" is deprecated and will be removed in version 0.22. Please use behaviour="new", which makes the decision_function change to match other anomaly detection algorithm API.
#>   FutureWarning)
#> IsolationForest(behaviour='old', bootstrap=False, contamination=0.1,
#>            max_features=1.0, max_samples=100, n_estimators=100,
#>            n_jobs=None, random_state=1001, verbose=0, warm_start=False)

y_pred_test = clf.predict(X_test)
#> /Users/ncg/anaconda3/envs/sklearntest/lib/python3.6/site-packages/sklearn/ensemble/iforest.py:415: DeprecationWarning: threshold_ attribute is deprecated in 0.20 and will be removed in 0.22.
#>   " be removed in 0.22.", DeprecationWarning)


print(clf.threshold_)
#> -0.06984862813837053
#> /Users/ncg/anaconda3/envs/sklearntest/lib/python3.6/site-packages/sklearn/ensemble/iforest.py:415: DeprecationWarning: threshold_ attribute is deprecated in 0.20 and will be removed in 0.22.
#>   " be removed in 0.22.", DeprecationWarning)

reprexpy package

创建于2019-06-06

这是"new"的行为。没有DeprecationWarning,但没有.threshold_

from sklearn.ensemble import IsolationForest
from sklearn import datasets
import numpy as np

dataset = datasets.load_iris()
np.random.seed(10)

X_train = dataset.data[:,:2] 
X_test  = np.random.random((10,2))*np.array([6,4])+np.array([3,1]) # fake observations

clf = IsolationForest(max_samples=100, contamination=0.1, random_state=1001, behaviour="new")
clf.fit(X_train)
#> IsolationForest(behaviour='new', bootstrap=False, contamination=0.1,
#>            max_features=1.0, max_samples=100, n_estimators=100,
#>            n_jobs=None, random_state=1001, verbose=0, warm_start=False)

y_pred_test = clf.predict(X_test)
print(clf.threshold_)
#> Traceback (most recent call last):
#> <ipython-input-13-27950b57c945> in <module>()
#> ----> 1 print(clf.threshold_)
#> ~/anaconda3/envs/sklearntest/lib/python3.6/site-packages/sklearn/ensemble/iforest.py in threshold_(self)
#>     410     def threshold_(self):
#>     411         if self.behaviour != 'old':
#> --> 412             raise AttributeError("threshold_ attribute does not exist when "
#>     413                                  "behaviour != 'old'")
#>     414         warn("threshold_ attribute is deprecated in 0.20 and will"
#> AttributeError: threshold_ attribute does not exist when behaviour != 'old'

reprexpy package

创建于2019-06-06

即使使用新行为,是否仍可以访问.threshold_

0 个答案:

没有答案