二手车交易价格预测-EDA数据探索性分析
赛题:零基础入门数据挖掘 - 二手车交易价格预测
地址:https://tianchi.aliyun.com/competition/entrance/231784/introduction?spm=5176.12281957.1004.1.38b02448ausjSX
#一、导入训练数据集和测试数据集
import pandas as pd
train = pd.read_csv('D:/天池-二手车价格预测/train.csv',sep = ' ')
test = pd.read_csv('D:/天池-二手车价格预测/testA.csv',sep = ' ')
#二、查看数据集,以训练集为例``
1.数据集维度,m为行数,n为列数
m,n = train.shape
2.查看前10行和后10行
train.head(100).append(train.tail(100))
3.通过describe()来熟悉数据的相关统计量
train.describe()
4.info()查看数据类型及是否有缺失值
train.info()
可以看出,除了notRepairedDamage 为object类型其他都为数字 ,且model 、bodyType、fuelType和gearbox这四个字段有缺失值。
5.检查异常字段
train['notRepairedDamage'].value_counts()
可见引起该字段为object类型的原因是缺失值用“-”代替。
#三、特征分析
将特征分为数字特征和类型特征
numeric_features = ['power','kilometer','v_0','v_1', 'v_2', 'v_3', 'v_4', 'v_5', 'v_6', 'v_7','v_8','v_9', 'v_10', 'v_11', 'v_12', 'v_13', 'v_14']
categorical_features = ['name','model','brand','bodyType','fuelType','gearbox','notRepairedDamage','regionCode']
1.数字特征相关性分析
计算皮尔逊系数
numeric_features.append('price')
price_numeric = train[numeric_features]
correlation = price_numeric.corr('spearman')
print(correlation['price'].sort_values(ascending = False),"\n")
v_0,v_12,v_8,这三个字段与price的皮尔逊相关系数大于0.8,呈强相关
画相关系数热力图
#导入相关包
import matplotlib.pyplot as plt
import seaborn as sns
from pylab import *
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
#作图
f,ax = plt.subplots(figsize = (7,7))
plt.title('Correlation of Numeric Features with Price',y=1,size = 16)
sns.heatmap(correlation,square = True, vmax=0.8)
数字特征之间的关系可视化
#melt()函数将列名categorical_features转换为列数据
f = pd.melt(train,id_vars = ['price'],value_vars = categorical_features)
#FacetGrid数据框初始化对象以及将形成网格的行,列或色调维度的变量名称来使用该类,5*2
g = sns.FacetGrid(f,col = 'variable',col_wrap=2,sharex=False, sharey=False,height=5)
#绘图
g = g.map(bar_plot,'value','price')
2.类别特征分析柱形图可视化
柱形图可视化
def bar_plot(x, y, **kwargs):
sns.barplot(x=x, y=y)
x=plt.xticks(rotation=90)
#melt()函数将列名categorical_features转换为列数据
f = pd.melt(train,id_vars = ['price'],value_vars = categorical_features)
#FacetGrid数据框初始化对象以及将形成网格的行,列或色调维度的变量名称来使用该类,5*2
g = sns.FacetGrid(f,col = 'variable',col_wrap=2,sharex=False, sharey=False,height=5)
#绘图
g = g.map(bar_plot,'value','price')
#四、了解预测值price
price_count = train['price'].value_counts().to_frame()
price_count['prices'] = price_count.index
#dataframe列命名
price_count.columns = ['counts','price']
#按照price升序排列
price_count = price_count.sort_values('price')
#计算price对应比例
p = (1*price_count['counts'].cumsum()/price_count['counts'].sum()).to_frame()
#作帕累托图
plt.plot(p.index,p['counts'])
plt.title('二手车价格帕累托图')
plt.xlabel('price')
plt.show()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9NqMt2Bi-1585055809270)(https://i.loli.net/2020/03/24/9ls7oWSpnbVhTiF.png)]
plt.title('二手车价格帕累托图')
plt.xlabel('price')
plt.show()
可以看出大概有80%的二手车辆的价格在9000元以下。