这个专栏停了也有一段时间了,自从上次对之前的内容进行了一次梳理之后,似乎是给自己一个“借口”休息了一阵子,现在感觉还是得重新拿出来继续更新了。
# 导入数据集
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
train = pd.read_csv('./data/Group-Image-of-Consumers/train_dataset.csv')
test = pd.read_csv('./data/Group-Image-of-Consumers/test_dataset.csv')
data = pd.concat([train, test], ignore_index=True)
data.head()
我们简单从里面挑选几个数值型变量来看看分布情况吧,画图的技巧这里就不说了,可以参考之前画箱体图的那篇镜囊文章。
# 挑选其中几个变量
feature_list=['当月网购类应用使用次数','当月金融理财类应用使用总次数','当月视频播放类应用使用次数']
# 绘制箱体图
sns.set_style("white")
f, ax = plt.subplots(figsize=(8, 7))
ax.set_xscale("log")
ax = sns.boxplot(data=data[feature_list] , orient="h", palette="Set1")
ax.xaxis.grid(False)
ax.set(ylabel="Feature names")
ax.set(xlabel="Numeric values")
ax.set(title="Numeric Distribution of Features")
sns.despine(trim=True, left=True)
可以看到红色框框圈起来的就是我们的离群点,那么我们可以怎么处理一下呢?这里给大家介绍一个方法,代码如下:
def process(all_data,feature_list):
#处理离群点
for col in feature_list:
ulimit=np.percentile(all_data[col].values, 99.9) #计算一个多维数组的任意百分比分位数
llimit=np.percentile(all_data[col].values, 0.1)
all_data.loc[all_data[col]>ulimit,col]=ulimit # 大于99.9%的直接赋值
all_data.loc[all_data[col]<llimit,col]=llimit
return all_data
使用刚刚我们编写的那个方法:
# 使用函数进行极端值的转换
data_new = process(data,feature_list)
# 绘制箱体图
sns.set_style("white")
f, ax = plt.subplots(figsize=(8, 7))
ax.set_xscale("log")
ax = sns.boxplot(data=data_new[feature_list] , orient="h", palette="Set1")
ax.xaxis.grid(False)
ax.set(ylabel="Feature names")
ax.set(xlabel="Numeric values")
ax.set(title="Numeric Distribution of Features")
sns.despine(trim=True, left=True)
see!我们的异常值就会被直接“安排”了,是不是很简单呢?其实异常值的处理还是有很大方法的,今天就抛砖引玉一下,更多的方法等待大家去挖掘哦!
往 期 锦 囊
? GitHub传送门
https://github.com/Pysamlam/Tips-of-Feature-engineering
原创不易,如果觉得这种学习方式有用,希望可以帮忙随手转发or点下“在看”,这是对我的极大鼓励!阿里嘎多!?