数据挖掘-探索性数据分析

Exploratory Data Analysis

0.忽略警告

导入warnings包,利用过滤器来实现忽略警告语句

1
2
import warnings
warnings.filterwarnings('ignore')

1.简单观察数据

1
data.head().append(data.tail())

2.观察数据形状

1
data.shape

3.总览数据概况

观察均值、极值、数量等

1
data.describe()

4.缺失值检测

1
data.isnull().sum()

5.nan可视化

1
2
3
4
missing = data.isnull().sum()
missing = missing[missing > 0]
missing.sort_values(inplace=True)
missing.plot.bar()

6.缺失值填充

这里采用众数填充

1
data = data.fillna(data.mode().iloc[0,:])

7.检查数据类型

1
data.info()

8.异常值处理

以天池二手车价格预测数据集为例,其notRepairedDamage为字符串类型,需要转换成数值类型;异常值’-‘,用众数进行填充

1
2
3
data.notRepairedDamage.replace('-', 0, inplace=True)
data['notRepairedDamage'] = data.notRepairedDamage.astype('float16')
data.notRepairedDamage.value_counts()

9.离群点处理

通过箱线图判断离群点,以’power’, ‘v_11’, ‘v_12’, ‘v_13’, ‘v_14’为例

1
2
3
4
5
6
plt.figure(figsize=(20,10))
BoxPlotList = ['power', 'v_11', 'v_12', 'v_13', 'v_14', ]
for i in range(1,len(BoxPlotList)+1):
plt.subplot(2,3,i)
plt.boxplot(data[BoxPlotList[i-1]])
plt.xlabel(BoxPlotList[i-1])

截断离群值

1
2
3
4
data['power'][data['power']>600] = 600
data['power'][data['power']<1] = 1
data['v_13'][data['v_13']>6] = 6
data['v_14'][data['v_14']>4] = 4

10.压缩数据

判断数据极值,用np.iinfo(np.int8)获取int8的范围,尽量将数据用更少的字节存储

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def MemoryReduce(data):
start_mem = data.memory_usage().sum()
print('Memory usage: {start_mem:.2f} MB')

for col in data.columns:
col_type = data[col].dtype

if col_type != object:
c_min = data[col].min()
c_max = data[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
data[col] = data[col].astype(np.int8)
elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
data[col] = data[col].astype(np.int16)
elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
data[col] = data[col].astype(np.int32)
elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
data[col] = data[col].astype(np.int64)
else:
if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
data[col] = data[col].astype(np.float16)
elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
data[col] = data[col].astype(np.float32)
else:
data[col] = data[col].astype(np.float64)
else:
data[col] = data[col].astype('category')

end_mem = data.memory_usage().sum()
print(f'Memory usage after optimization: {end_mem:.2f} MB')
return data
data = MemoryReduce(data)

11.倾斜值处理

1
2
# 读取每列不同取值的个数
data.nunique()
1
2
3
4
5
6
7
8
>SaleID               200000
>name 128660
>regDate 3898
>model 248
>seller 2
>offerType 1
>creatDate 106
>dtype: int64

如上所示,seller 特征几乎均为 0,offerType 所有特征均为 0,存在严重的数据倾斜,对模型训练毫无帮助,删去

删除特征

1
data.drop(['offerType', 'seller'], axis=1, inplace=True)

12.预测值总体分布曲线

无界约翰逊分布、正态分布、对数正态分布

1
2
3
4
5
6
7
8
import scipy.stats as st
y = data['price']
plt.figure(1); plt.title('Johnson SU')
sns.distplot(y, kde=False, fit=st.johnsonsu)
plt.figure(2); plt.title('Normal')
sns.distplot(y, kde=False, fit=st.norm)
plt.figure(3); plt.title('Log Normal')
sns.distplot(y, kde=False, fit=st.lognorm)

13.生成数据分析报告

1
2
3
4
import pandas_profiling

pfr = pandas_profiling.ProfileReport(data)
pfr.to_file("./example.html")
不要打赏,只求关注呀QAQ