离群值清除隔离林

时间:2020-02-09 19:18:02

标签: python pandas csv jupyter-notebook random-forest

我一直在尝试使用隔离林从数据库中删除异常值,但我不知道如何做到。我已经看到了信用卡欺诈和薪水的示例,但是由于数据库由3862900行和19列组成,所以我不知道如何将它们应用于每一列。我已经上传了数据库负责人的图片。我无法弄清楚如何在每列上应用隔离林然后永久删除这些离群值。enter image description here

谢谢。

enter image description here

2 个答案:

答案 0 :(得分:1)

根据docs用于检测未删除异常值的情况

[123,456]

df = pd.DataFrame({'temp': [1,2,3,345,6,7,5345, 8, 9, 10, 11]}) clf = IsolationForest().fit(df['temp'].values.reshape(-1, 1)) clf.predict([[4], [5], [3636]])

从输出array([ 1, 1, -1])4可以看出,离群值不是3636,而是离群值。

如果要从数据框中删除异常值,则应使用IQR

5
quant = df['temp'].quantile([0.25, 0.75])
df['temp'][~df['temp'].clip(*quant).isin(quant)]

您可以看到异常值已被删除

对于整个df

4     6
5     7
7     8
8     9
9    10

注意:隔离林无法从数据集中删除异常值,它用于检测新的异常值

答案 1 :(得分:0)

IsolationForest 可能打算清除异常值中的数据。正如它所说的answer在通常的机器学习设置中,你会运行它来清理你的训练数据集

from sklearn.ensemble import IsolationForest
clf = IsolationForest(max_samples=100, random_state=4, contamination=.1)
#identify outliers:
y_pred_train = clf.fit_predict(X_train)
#Remove outliers where 1 represent inliers and -1 represent outliers:
X_train_cleaned = X_train[np.where(y_pred_train == 1, True, False)]

我们可以使用不同的方法(如 IQR)在无监督设置中对 contamination 进行参数化。