【问题标题】:Isolation Forest Parameter tuning with gridSearchCV使用 gridSearchCV 调整隔离林参数
【发布时间】:2023-03-05 18:17:02
【问题描述】:

我有多变量时间序列数据,想用隔离森林算法检测异常。 想从gridSearchCV中得到最好的参数,这里是gridSearch CV的sn-p代码。

使用以下 sn-p 加载的输入数据集。

df = pd.read_csv("train.csv")
df.drop(['dataTimestamp','Anomaly'], inplace=True, axis=1)
X_train = df
y_train = df1[['Anomaly']] ( Anomaly column is labelled data).

定义隔离森林的参数。

clf = IsolationForest(random_state=47, behaviour='new', score="accuracy")
param_grid = {'n_estimators': list(range(100, 800, 5)), 'max_samples': list(range(100, 500, 5)), 'contamination': [0.1, 0.2, 0.3, 0.4, 0.5], 'max_features': [5,10,15], 'bootstrap': [True, False], 'n_jobs': [5, 10, 20, 30]}

f1sc = make_scorer(f1_score)
grid_dt_estimator = model_selection.GridSearchCV(clf, param_grid,scoring=f1sc, refit=True,cv=10, return_train_score=True)
grid_dt_estimator.fit(X_train, y_train)

执行 fit 后,出现以下错误。

ValueError:目标是多类但平均值='二进制'。请选择其他平均设置。

有人可以指导我这是怎么回事,尝试了平均='体重',但仍然没有运气,这里有什么问题。 请告诉我如何获得 F 分数。

【问题讨论】:

    标签: python-3.x scikit-learn data-science


    【解决方案1】:

    您会出现此错误,因为您在将 f1_score 转换为记分器时没有设置参数 average。其实详情见documentation

    average : string, [None, ‘binary’ (默认), ‘micro’, ‘macro’, ‘samples’, ‘weighted’] 这个参数是必需的 多类/多标签目标。如果没有,每个班级的分数是 返回。

    结果是记分器为分类问题中的每个类别返回多个分数,而不是单个度量。解决方案是根据您的需要为f1_score 声明average 参数的可能值之一。因此,我重构了您作为示例提供的代码,以便为您的问题提供可能的解决方案:

    from sklearn.ensemble import IsolationForest
    from sklearn.metrics import make_scorer, f1_score
    from sklearn import model_selection
    from sklearn.datasets import make_classification
    
    X_train, y_train = make_classification(n_samples=500, 
                                           n_classes=2)
    
    clf = IsolationForest(random_state=47, behaviour='new')
    
    param_grid = {'n_estimators': list(range(100, 800, 5)), 
                  'max_samples': list(range(100, 500, 5)), 
                  'contamination': [0.1, 0.2, 0.3, 0.4, 0.5], 
                  'max_features': [5,10,15], 
                  'bootstrap': [True, False], 
                  'n_jobs': [5, 10, 20, 30]}
    
    f1sc = make_scorer(f1_score(average='micro'))
    
    grid_dt_estimator = model_selection.GridSearchCV(clf, 
                                                     param_grid,
                                                     scoring=f1sc, 
                                                     refit=True,
                                                     cv=10, 
                                                     return_train_score=True)
    grid_dt_estimator.fit(X_train, y_train)
    

    【讨论】:

    • 嗨 Luca,非常感谢您的回复。修改代码 f1sc = make_scorer(f1_score(average='micro')) 后出现以下错误,错误消息如下(TypeError:f1_score()缺少2个必需的位置参数:'y_true'和'y_pred')。
    • 用 1 和 -1 而不是 0 和 1 标记数据后问题已得到解决。
    • 即使将其更改为 -1 和 1 Counter({-1: 250, 1: 250}) 我也会收到相同的错误 --------------- -------------------------------------------------- ---------- TypeError: f1_score() 缺少 2 个必需的位置参数:'y_true' 和 'y_pred'
    • 对于IsolationForest 的无监督情况,scoring 参数是什么?
    【解决方案2】:

    用这个更新 make_scorer 让它工作。

    make_scorer(f1_score, average='micro')
    

    【讨论】:

    【解决方案3】:

    您调整的参数并非全部必需
    例如:
    contamination是异常率,您可以通过调整model.score_samples上的阈值来确定拟合模型后的最佳值

    n_jobs 是您使用的 CPU 内核。

    【讨论】:

      猜你喜欢
      • 2016-05-11
      • 1970-01-01
      • 2017-09-21
      • 2021-10-12
      • 2021-01-10
      • 2021-08-11
      • 2018-02-02
      • 1970-01-01
      • 2017-11-28
      相关资源
      最近更新 更多