<p>您需要创建自己的评分函数,因为<code>IsolationForest</code>没有内置<code>score</code>方法。相反,您可以使用<code>score_samples</code>中提供的<code>score_samples</code>函数(可以看作是<code>score</code>的代理)并创建自己的记分器,如<a href="https://scikit-learn.org/stable/modules/model_evaluation.html#implementing-your-own-scoring-object" rel="noreferrer">here</a>所述,并将其传递给<code>GridSearchCV</code>。我修改了您的代码以执行以下操作:</p>
<pre><code>import pandas as pd
import numpy as np
from sklearn.ensemble import IsolationForest
from sklearn.model_selection import GridSearchCV
df = pd.DataFrame({'first': [-112,0,1,28,5,6,3,5,4,2,7,5,1,3,2,2,5,2,42,84,13,43,13],
'second': [42,1,2,85,2,4,6,8,3,5,7,3,64,1,4,1,2,4,13,1,0,40,9],
'third': [3,4,7,74,3,8,2,4,7,1,53,6,5,5,59,0,5,12,65,4,3,4,11],
'result': [5,2,3,0.04,3,4,3,125,6,6,0.8,9,1,4,59,12,1,4,0,8,5,4,1]})
x = df.iloc[:,:-1]
tuned = {'n_estimators':[70,80], 'max_samples':['auto'],
'contamination':['legacy'], 'max_features':[1],
'bootstrap':[True], 'n_jobs':[None,1,2], 'behaviour':['old'],
'random_state':[None,1,], 'verbose':[0,1,2], 'warm_start':[True]}
def scorer_f(estimator, X): #your own scorer
return np.mean(estimator.score_samples(X))
#or you could use a lambda aexpression as shown below
#scorer = lambda est, data: np.mean(est.score_samples(data))
isolation_forest = GridSearchCV(IsolationForest(), tuned, scoring=scorer_f)
model = isolation_forest.fit(x)
</code></pre>
<blockquote>
<p>SAMPLE OUTPUT</p>
</blockquote>
^{pr2}$
<p>希望这有帮助!在</p>