#异常值删除函数 def f(data, col): q1 = data[col].quantile(q=0.25) q3 = data[col].quantile(q=0.75) iqr = q3 - q1 t1 = q1 - 3 * iqr t2 = q3 + 3 * iqr return data[(data[col] > t1) & (data[col] < t2)] # 删除异常值 exception=['消费金额','余额','消费次数'] for i in exception: data2_new.f(data2_new,i) data2_new.describe()输出数据
时间: 2023-06-20 21:09:27 浏览: 33
在你的代码中,你定义了一个名为 `f` 的函数,用于删除给定数据列中的异常值。然后你定义了一个名为 `exception` 的列表,其中包含需要删除异常值的列名。接下来你使用 `for` 循环来逐一处理每个列名,并将处理后的数据存储在一个名为 `data2_new` 的变量中。
但是,在你的代码中有一个问题,你没有正确地使用 `f` 函数来删除异常值。在你的循环中,你调用了 `f` 函数,但是你没有将返回的结果存储在任何变量中。因此,你需要更改你的代码,以便将返回的数据存储在变量中,如下所示:
```
for i in exception:
data2_new = f(data2_new, i)
```
这将确保你的代码正确地删除给定列中的异常值,并将处理后的数据存储在 `data2_new` 变量中。然后你可以使用 `describe()` 函数来输出处理后的数据的统计信息。
相关问题
num_features=data.select_dtypes(exclude=['object','bool']).columns.tolist() for feature in num_features: Q1 = data[feature].quantile(q=0.25) Q3 = data[feature].quantile(q=0.75) IQR = Q3-Q1 top = Q3+1.5*IQR bot = Q1-1.5*IQR values=data[feature].values values[values > top] = top values[values < bot] = bot data[feature] = values.astype(data[feature].dtypes)
这段代码是用来处理数据中的异常值(outliers)。首先,代码会找出数据中所有数值型的特征(exclude=['object','bool']),并将这些特征名存入num_features列表中。接下来,对于num_features中的每一个特征,代码会计算该特征的第一四分位数Q1、第三四分位数Q3和四分位距IQR(IQR=Q3-Q1),然后通过计算上下界来判断该特征中是否存在异常值。如果该特征中存在异常值,就将其替换成上下界的值。最后,将替换后的值存回到原数据中,并将其转换成原来的数据类型。
hbin = 10 for i in tqdm(data['WindNumber'].unique()): col = 'WindSpeed' cond = (data.WindNumber==i) & (data.label == 0) temp_df = data[cond] h_bins = pd.cut(temp_df.Power, np.arange(-1000, 3000, hbin)) temp_df['hbins'] = h_bins groups = [] for index,temp in temp_df.groupby("hbins"): if temp.shape[0]==0: continue iqr = temp[col].quantile(0.75) - temp[col].quantile(0.25) t1 = temp[col].quantile(0.25) - 1.5 * iqr t2 = temp[col].quantile(0.75) + 1.5 * iqr temp = temp[((temp[col]<t1) | (temp[col]>t2))] groups.append(temp) groups = pd.concat(groups).reset_index(drop = True) cond = (data.WindNumber==i) & (data.Time.isin(groups.Time)) data.loc[cond,'label'] = 3 cond = (data.WindNumber==1) vis_result_2D(data[cond],data[cond]['label'])详细解释
这段代码是对数据集中的每个风机进行处理,首先根据风机编号筛选数据,然后根据功率值将数据分为若干个区间,每个区间内的数据按照风速进行离群值检测,如果检测出离群值,则将对应的数据标记为异常(标签为3),最后将标记为异常的数据可视化展示。
具体解释如下:
1. `hbin = 10`:将功率值分为10个区间,每个区间的宽度为1000W。
2. `for i in tqdm(data['WindNumber'].unique()):`:循环遍历数据集中的所有风机。
3. `cond = (data.WindNumber==i) & (data.label == 0)`:根据风机编号和标签筛选数据,其中标签为0表示数据未被标记为异常。
4. `temp_df = data[cond]`:将筛选后的数据赋值给临时变量temp_df。
5. `h_bins = pd.cut(temp_df.Power, np.arange(-1000, 3000, hbin))`:根据功率值将数据分为若干个区间,并将区间标签赋值给 h_bins 变量。
6. `temp_df['hbins'] = h_bins`:将区间标签添加到数据集中。
7. `groups = []`:创建一个空列表,用于存储检测出的离群值数据。
8. `for index,temp in temp_df.groupby("hbins"):`:根据区间标签将数据分组,循环遍历每个区间。
9. `if temp.shape[0]==0:`:如果区间内没有数据,则跳过本次循环。
10. `iqr = temp[col].quantile(0.75) - temp[col].quantile(0.25)`:计算数据的四分位距。
11. `t1 = temp[col].quantile(0.25) - 1.5 * iqr`:计算下界阈值。
12. `t2 = temp[col].quantile(0.75) + 1.5 * iqr`:计算上界阈值。
13. `temp = temp[((temp[col]<t1) | (temp[col]>t2))]`:筛选出离群值数据,即风速小于下界阈值或大于上界阈值的数据。
14. `groups.append(temp)`:将检测出的离群值数据添加到 groups 列表中。
15. `groups = pd.concat(groups).reset_index(drop = True)`:将 groups 列表中的数据合并为一个数据集。
16. `cond = (data.WindNumber==i) & (data.Time.isin(groups.Time))`:根据风机编号和时间标记异常数据。
17. `data.loc[cond,'label'] = 3`:将标记为异常的数据标签设置为3。
18. `cond = (data.WindNumber==1)`:根据风机编号筛选数据。
19. `vis_result_2D(data[cond],data[cond]['label'])`:将标记后的数据可视化展示,其中 vis_result_2D 是一个自定义的函数。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)