【问题标题】:How to fix X does not have valid feature names, but IsolationForest was fitted with feature names warnings.warn(如何修复 X ​​没有有效的功能名称,但 IsolationForest 配备了功能名称 warnings.warn(
【发布时间】:2022-01-19 07:40:58
【问题描述】:

这是我的代码:

import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.ensemble import IsolationForest

data = pd.read_csv('marks1.csv', encoding='latin-1',
                   on_bad_lines='skip', index_col=0, header=0
                   )

random_state = np.random.RandomState(42)

model = IsolationForest(n_estimators=100, max_samples='auto', contamination=float(0.2)
                        , random_state=random_state)

model.fit(data[['Mark']])

random_state = np.random.RandomState(42)

data['scores'] = model.decision_function(data[['Mark']])

data['anomaly_score'] = model.predict(data[['Mark']])

data[data['anomaly_score'] == -1].head()

错误:

C:\Program Files\Python39\lib\site-packages\sklearn\base.py:450: UserWarning: X 没有有效的功能名称,但 IsolationForest 配备了功能名称 警告.warn(

【问题讨论】:

    标签: python pandas scikit-learn


    【解决方案1】:

    这取决于您使用的 sklearn 版本。在 1.0 之后的版本中,模型在使用集成了列名称的数据框进行训练时具有 feature_names 属性。此版本中有一个错误,在使用数据帧进行训练时会引发错误。 https://github.com/scikit-learn/scikit-learn/issues/21577

    我还没有了解最新的最佳实践,所以我不能明确地说应该如何设置。但我现在只是在我的代码中解决了这个问题。 为了解决这个问题,我在训练之前将我的数据帧转换为一个 numpy 数组

    df.to_numpy()
    

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 2022-10-07
      • 2022-11-27
      • 1970-01-01
      • 2012-09-28
      • 2023-04-03
      • 2018-01-07
      • 2022-01-07
      相关资源
      最近更新 更多