csv 孤立森林

tsm1rwdh  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(95)

我一直在尝试使用隔离森林从数据库中删除离群值,但我不知道如何做到。我看过信用卡欺诈和工资的例子,但我不知道如何将它们应用于每一列,因为我的数据库由3862900行和19列组成。我上传了一张我数据库负责人的照片。我不知道如何在每个列上应用隔离森林,然后永久删除这些异常值。

谢谢

h43kikqp

h43kikqp1#

根据docs,用于检测离群值,而不是去除离群值

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])
从输出中可以看出,45不是离群值,但3636是。
如果你想从你的网络中删除离群值,你应该使用IQR

quant = df['temp'].quantile([0.25, 0.75])
df['temp'][~df['temp'].clip(*quant).isin(quant)]
4     6
5     7
7     8
8     9
9    10

如你所见,异常值已经被移除
对于整个df

def IQR(df, colname, bounds = [.25, .75]):
    s = df[colname]
    q = s.quantile(bounds)
    return df[~s.clip(*q).isin(q)]

注意:隔离森林不能从数据集中删除离群值,它用于检测新的离群值

zf2sa74q

zf2sa74q2#

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进行参数化。

hpxqektj

hpxqektj3#

我知道我回答这个问题晚了,但正如@凯南所说。
你可以做的一件事是使用O_Sieve,它执行自动离群值删除并提供数据集。

pip install vcosmos
from spatial_domain.anaomaly import O_Sieve
sieve=O_Sieve(your_df target_column, tsf=2,tsf=2)
clean_df=sieve.filtered_data()
print(clean_df)

您可以调整tsf和bsf参数来选择它如何影响离群值的数量。要了解更多信息,请查看vcosmos的文档

相关问题