如何从我的数据中删除异常值?我应该使用 RobustScaler 吗?我知道我可以使用 DecisionTree 但我想使用 XGBoost ......
请你帮帮我,这有点紧急,我不知道该怎么做,我已经研究并看到了以前的问题,但它不能很好地工作并且没有帮助。
谢谢
干杯
如何从我的数据中删除异常值?我应该使用 RobustScaler 吗?我知道我可以使用 DecisionTree 但我想使用 XGBoost ......
请你帮帮我,这有点紧急,我不知道该怎么做,我已经研究并看到了以前的问题,但它不能很好地工作并且没有帮助。
谢谢
干杯
首先,您不需要删除异常值,因为像 XGBoost 这样的“决策族算法”可以处理它。
其次,您可以使用 Tukey 方法 (Tukey JW., 1977):
def detect_outliers(df,n,features):
outlier_indices = []
# iterate over features(columns)
for col in features:
# 1st quartile (25%)
Q1 = np.percentile(df[col], 25)
# 3rd quartile (75%)
Q3 = np.percentile(df[col],75)
# Interquartile range (IQR)
IQR = Q3 - Q1
# outlier step
outlier_step = 1.5 * IQR
# Determine a list of indices of outliers for feature col
outlier_list_col = df[(df[col] < Q1 - outlier_step) | (df[col] > Q3 + outlier_step )].index
# append the found outlier indices for col to the list of outlier indices
outlier_indices.extend(outlier_list_col)
# select observations containing more than 2 outliers
outlier_indices = Counter(outlier_indices)
multiple_outliers = list( k for k, v in outlier_indices.items() if v > n )
return multiple_outliers
Outliers_to_drop = detect_outliers(data,2,["col1","col2"])
data.loc[Outliers_to_drop] # Show the outliers rows
# Drop outliers
data= data.drop(Outliers_to_drop, axis = 0).reset_index(drop=True)
https://www.kaggle.com/yassineghouzam/titanic-top-4-with-ensemble-modeling