Machine learning outlier processing logic summary 1

1. Clear the constant values ​​in the data

If a certain data does not change for a long time, the default is abnormal and this part of the data is cleared:

# 使用 `shift` 和 `cumsum` 来创建一个分组键,每次值改变都会增加组号
g = (df['沉淀池3号进水流量'] != df['沉淀池3号进水流量'].shift()).cumsum()

# 使用 `transform` 来计算每个组的大小
counts = df.groupby(g)['沉淀池3号进水流量'].transform('count')
print('counts:', counts)
# 应用一个布尔掩码,将连续出现至少5次的值替换为 NaN
df.loc[counts >= 5, '沉淀池3号进水流量'] = np.nan

# 现在df中的'column_name'列已经将所有连续5个相同的值替换为了 NaN
df.info()

2. Clear out-of-range values

Clean values ​​outside the specified data range:

df['原水浊度'] = df['原水浊度'].apply(lambda x: x if 0.01 <= x <= 3 else None)

3. Use one column of data to replace another column of data

all_data.loc[all_data['温度'].isnull(), '温度'] = all_data.loc[all_data['温度'].isnull(), '温度2']

Four specified conditions to replace a certain part of the data np.where

df['沉后水浊度3'] = np.where(df.index > mid_time, 0.1, df['沉后水浊度3'])

Guess you like

Origin blog.csdn.net/March_A/article/details/135364516