首页 > 解决方案 > IsolationForest KeyError:“[Index([''], dtype='object')] 均不在 [columns] 中”

问题描述

我正在尝试使用 IsolationForest 实现我的第一个异常检测,但不幸的是它没有成功。

我有一个.csv具有不同网络参数的文件,例如 ip.ttl、frame.len 等。

#Einlesen
quelle = pd.read_csv('./x.csv')
pdf=quelle.to_numpy()
print(quelle.columns)

索引([';ip.proto;ttl;frame.len;ip.src;ip.dst;ip.len;ip.flags;eth.src;eth.dst;eth.type;vlan.id;udp.port '], dtype='对象')

print(quelle.shape)

(1658, 1)

但是,当我尝试使用 ip.ttl 或 frame.len(其中一列)之类的列创建 IsolationForest 模型时,出现错误

model=IsolationForest(n_estimators=50, max_samples='auto',contamination=float(0.1),max_features=1.0)
model.fit(quelle[['frame.len']])

KeyError:“[Index(['frame.len'], dtype='object')] 均不在 [columns] 中”

我的错误在哪里?

提前致谢

标签: python

解决方案


数据框有许多数据点,但只有一列。

print(quelle.shape)
(1658, 1)

当您将文件加载到数据框中时,它无法自动检测文件的正确分隔符是什么,而不是读取每一列,而是将所有列打包到一个列中。

要解决此问题,您应该在读取文件时指定分隔符。

pd.read_csv('./x.csv', sep=';')

推荐阅读