数据挖掘实战--二手车交易价格预测(二)数据探索性分析(EDA)

包的安装:
采用Anaconda 3进行代码的编译,Anaconda 3里基础的数据分析包都已经准备好,我们需要安装的就是sklearn,lightgbm和xgboost包。

Anaconda可以支持我们采取多种方式安装所需要的包。可以采用pip,conda和从PYPI下载相关包等方式。这里采用的是pip方式。

pip install scikit-learn
pip install lightgbm
pip install xgboost

因为之前一直在进行Arcpy的开发工作,因此我电脑里装配的是Anaconda 2 32位,这在安装lightgbm和xgboost的过程中遇到了错误。因此又安装了Anaconda 3 64位版本。同队的韩哥也遇到了报错的问题,似乎是因为pip的版本不够新,需要升级后再安装。

数据加载与查看

首先我们需要将已有的数据读进内存里,

import pandas as pd
import numpy as np
import warnings
#为了防止没有维护的包弹警告,可以在这里过滤掉警告
warnings.filterwarnings('ignore')
#在Jupyter里,可能会对过多的列进行隐藏, 如果想要查看全部的列,可以设置max_columns
pd.set_option('display.max_columns', None)

train_df = pd.read_csv('D:/DataMining/Train Data/used_car_train_20200313.csv', sep=' ')
print(train_df.shape)
train_df.describe()

train_df.head()

下一步来求一下空值数量,看看各个变量空值缺失的状况,如果缺失多,可以考虑在构建特证的时候剔除。

train_df.isnull().sum().sort_values(ascending=False).head()

接下来看一下价格的分布

import matplotlib.pyplot as plt
import seaborn as sns


plt.figure()
sns.distplot(train_df['price'])
plt.figure()
train_df['price'].plot.box()
plt.show()

可以看到明显的正偏态
再将测试集读进来,看一下测试集的状态。

import gc


test_df = pd.read_csv('datalab/231784/used_car_testA_20200313.csv', sep=' ')
print(test_df.shape)
df = pd.concat([train_df, test_df], axis=0, ignore_index=True)
del train_df, test_df
gc.collect()
df.head()

在这里插入图片描述

我们接下来可以看一下非匿名的几个可能会比较相关的数据的分布。

plt.figure()
plt.figure(figsize=(16, 6))
i = 1
for f in date_cols:
    for col in ['year', 'month', 'day', 'dayofweek']:
        plt.subplot(2, 4, i)
        i += 1
        v = df[f + '_' + col].value_counts()
        fig = sns.barplot(x=v.index, y=v.values)
        for item in fig.get_xticklabels():
            item.set_rotation(90)
        plt.title(f + '_' + col)
plt.tight_layout()
plt.show()

特征向量

对数据进行初步的处理和加工

移除只有单个因素的特征

cate_cols.remove('seller')
cate_cols.remove('offerType')
date_cols = ['regDate_year', 'regDate_month', 'regDate_day', 'regDate_dayofweek', 'creatDate_month', 'creatDate_day', 'creatDate_dayofweek']

下一步想要查看一下各个特征和price的相关性,以及特征和特征之间的关系。这个图表可以帮助我们理解哪些特征可以被选入特征向量的的构建,还有降维时要去除哪些特征。

corr1 = abs(df[~df['price'].isnull()][['price'] + date_cols + num_cols].corr())
plt.figure(figsize=(10, 10))
sns.heatmap(corr1, linewidths=0.1, cmap=sns.cm.rocket_r)

在这里插入图片描述
跟price相关性高的是regDate_year、kilometer;匿名特征中的v_0、v_3、v_8、v_12与price相关性很高。有些特征跟特征之间的相关性也很高,比如v_1跟v_6、v_2跟v_7、v_3跟v_8、v_4跟v_9等,这些特征之间可能存在冗余现象,训练的时候可以依据效果尝试去掉一部分。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章