Datawhale 二手车交易价格预测Task1&2赛题理解

1赛题概况

比赛要求参赛选手根据给定的数据集,建立模型,二手汽车的交易价格。
来自 Ebay Kleinanzeigen 报废的二手车,数量超过 370,000,包含 20 列变量信息,为了保证 比赛的公平性,将会从中抽取 10 万条作为训练集,5 万条作为测试集 A,5 万条作为测试集 B。同时会对名称、车辆类型、变速
箱、model、燃油类型、品牌、公里数、价格等信息进行 脱敏。

数据概况

train.csv
name - 汽车编码
regDate - 汽车注册时间
model - 车型编码
brand - 品牌
bodyType - 车身类型
fuelType - 燃油类型
gearbox - 变速箱
power - 汽车功率
kilometer - 汽车行驶公里
notRepairedDamage - 汽车有尚未修复的损坏
regionCode - 看车地区编码
seller - 销售方
offerType - 报价类型
creatDate - 广告发布时间
price - 汽车价格
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’(根据汽车的评
论、标签等大量信息得到的embedding向量)【人工构造 匿名特征】
数字全都脱敏处理,都为label encoding形式,即数字形式

预测指标

本赛题的评价标准为MAE(Mean Absolute Error):
M A E = ∑ i = 1 n ∣ y i − y ^ i ∣ n M A E=\frac{\sum_{i=1}^{n}\left|y_{i}-\hat{y}_{i}\right|}{n} MAE=ni=1nyiy^i
其中y_{i}代表第 个样本的真实值,其中\hat{y}代表第 个样本的预测值。

分析赛题

  1. 此题为传统的数据挖掘问题,通过数据科学以及机器学习深度学习的办法来进行建模得到结果。
  2. 此题是一个典型的回归问题。
  3. 主要应用xgb、lgb、catboost,以及pandas、numpy、matplotlib、seabon、sklearn、keras等等数据挖掘常
    用库或者框架来进行数据挖掘任务。
  4. 通过EDA来挖掘数据的联系和自我熟悉数据。

代码示例

本部分为对于数据读取和指标评价的示例。

数据读取pandas

import pandas as pd
import numpy as np
path = './data/'
## 1) 载入训练集和测试集;
Train_data = pd.read_csv(path+'train.csv', sep=' ')
Test_data = pd.read_csv(path+'testA.csv', sep=' ')
print('Train data shape:',Train_data.shape)
print('TestA data shape:',Test_data.shape)

分类指标评价计算示例

import numpy as np
from sklearn.metrics import accuracy_score
y_pred = [0, 1, 0, 1]
y_true = [0, 1, 1, 1]
print('ACC:',accuracy_score(y_true, y_pred))
## Precision,Recall,F1-score
from sklearn import metrics
y_pred = [0, 1, 0, 0]
y_true = [0, 1, 0, 1]
print('Precision',metrics.precision_score(y_true, y_pred))
print('Recall',metrics.recall_score(y_true, y_pred))
print('F1-score:',metrics.f1_score(y_true, y_pred))
## AUC
import numpy as np
from sklearn.metrics import roc_auc_score
y_true = np.array([0, 0, 1, 1])
y_scores = np.array([0.1, 0.4, 0.35, 0.8])
print('AUC socre:',roc_auc_score(y_true, y_scores))

回归指标评价计算示例

# coding=utf-8
import numpy as np
from sklearn import metrics
# MAPE需要自己实现 def mape(y_true, y_pred):
 return np.mean(np.abs((y_pred - y_true) / y_true))
y_true = np.array([1.0, 5.0, 4.0, 3.0, 2.0, 5.0, -3.0])
y_pred = np.array([1.0, 4.5, 3.8, 3.2, 3.0, 4.8, -2.2])
# MSE
print('MSE:',metrics.mean_squared_error(y_true, y_pred))
# RMSE
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_true, y_pred)))
# MAE
print('MAE:',metrics.mean_absolute_error(y_true, y_pred))
# MAPE
print('MAPE:',mape(y_true, y_pred))

EDA-数据探索性分析

EDA目标

EDA的价值主要在于熟悉数据集,了解数据集,对数据集进行验证来确定所获得数据集可以用于接下来的机
器学习或者深度学习使用。
当了解了数据集之后我们下一步就是要去了解变量间的相互关系以及变量与预测值之间的存在关系。
引导数据科学从业者进行数据处理以及特征工程的步骤,使数据集的结构和特征集让接下来的预测问题更加可
靠。

内容介绍

  1. 载入各种数据科学以及可视化库:
    数据科学库 pandas、numpy、scipy;
    可视化库 matplotlib、seabon;
    其他;
  2. 载入数据:
    载入训练集和测试集;
    简略观察数据(head()+shape); 3. 数据总览:
    通过describe()来熟悉数据的相关统计量
    通过info()来熟悉数据类型
  3. 判断数据缺失和异常
    查看每列的存在nan情况
    异常值检测
  4. 了解预测值的分布
    总体分布概况(无界约翰逊分布等)
    查看skewness and kurtosis
    查看预测值的具体频数
  5. 特征分为类别特征和数字特征,并对类别特征查看unique分布
  6. 数字特征分析
    相关性分析
    查看几个特征得 偏度和峰值
    每个数字特征得分布可视化
    数字特征相互之间的关系可视化
    多变量互相回归关系可视化
  7. 类型特征分析
    unique分布
    类别特征箱形图可视化
    类别特征的小提琴图可视化
    类别特征的柱形图可视化类别
    特征的每个类别频数可视化(count_plot)
  8. 用pandas_profiling生成数据报告

代码示例

载入各种数据科学以及可视化库

#coding:utf-8
#导入warnings包,利用过滤器来实现忽略警告语句。 import warnings
warnings.filterwarnings('ignore') import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import missingno as msno

载入数据

## 1) 载入训练集和测试集;
Train_data = pd.read_csv('train.csv', sep=' ')
Test_data = pd.read_csv('testA.csv', sep=' ')
Train_data.head().append(Train_data.tail())

在这里插入图片描述

Train_data.shape

(150000, 31)

Test_data.head().append(Test_data.tail())

在这里插入图片描述

Test_data.shape

(50000, 30)

总览数据概况

  1. describe种有每列的统计量,个数count、平均值mean、方差std、最小值min、中位数25% 50% 75% 、以
    及最大值 看这个信息主要是瞬间掌握数据的大概的范围以及每个值的异常值的判断,比如有的时候会发现
    999 9999 -1 等值这些其实都是nan的另外一种表达方式,有的时候需要注意下
  2. info 通过info来了解数据每列的type,有助于了解是否存在除了nan以外的特殊符号异常
Train_data.describe()

在这里插入图片描述

Test_data.describe()

在这里插入图片描述

## 2) 通过info()来熟悉数据类型
Train_data.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150000 entries, 0 to 149999
Data columns (total 31 columns):
SaleID 150000 non-null int64
name 150000 non-null int64
regDate 150000 non-null int64
model 149999 non-null float64
brand 150000 non-null int64
bodyType 145494 non-null float64
fuelType 141320 non-null float64
gearbox 144019 non-null float64
power 150000 non-null int64
kilometer 150000 non-null float64
notRepairedDamage 150000 non-null object
regionCode 150000 non-null int64
seller 150000 non-null int64
offerType 150000 non-null int64
creatDate 150000 non-null int64
price 150000 non-null int64
v_0 150000 non-null float64
v_1 150000 non-null float64
v_2 150000 non-null float64
v_3 150000 non-null float64
v_4 150000 non-null float64
v_5 150000 non-null float64
v_6 150000 non-null float64
v_7 150000 non-null float64
v_8 150000 non-null float64
v_9 150000 non-null float64
v_10 150000 non-null float64
v_11 150000 non-null float64
v_12 150000 non-null float64
v_13 150000 non-null float64
v_14 150000 non-null float64
dtypes: float64(20), int64(10), object(1)
memory usage: 35.5+ MB

判断数据缺失和异常

## 1) 查看每列的存在nan情况
Train_data.isnull().sum()


Out[17]:
SaleID 0
name 0
regDate 0
model 1
brand 0
bodyType 4506
fuelType 8680
gearbox 5981
power 0
kilometer 0
notRepairedDamage 0
regionCode 0
seller 0
offerType 0
creatDate 0
price 0
v_0 0
v_1 0
v_2 0
v_3 0
v_4 0
v_5 0
v_6 0
v_7 0
v_8 0
v_9 0
v_10 0
v_11 0
v_12 0
v_13 0
v_14 0
dtype: int64
# nan可视化
missing = Train_data.isnull().sum()
missing = missing[missing > 0]
missing.sort_values(inplace=True)
missing.plot.bar()

Out[19]:
<matplotlib.axes._subplots.AxesSubplot at 0x111b156dba8>

在这里插入图片描述
通过以上两句可以很直观的了解哪些列存在 “nan”, 并可以把nan的个数打印,主要的目的在于 nan存在的个数是
否真的很大,如果很小一般选择填充,如果使用lgb等树模型可以直接空缺,让树自己去优化,但如果nan存在的
过多、可以考虑删掉

# 可视化看下缺省值
msno.matrix(Train_data.sample(250))

在这里插入图片描述

了解预测值的分布

Train_data['price']

Out[35]:
0 1850
1 3600
2 6222
3 2400
4 5200
 ... 
149995 5900
149996 9500
149997 7500
149998 4999
149999 4700
Name: price, Length: 150000, dtype: int64

Train_data['price'].value_counts()

Out[36]:
500 2337
1500 2158
1200 1922
1000 1850
2500 1821
 ... 
25321 1
8886 1
8801 1
37920 1
8188 1
Name: price, Length: 3763, dtype: int64

特征分为类别特征和数字特征,并对类别特征查看unique分布

# 特征nunique分布 for cat_fea in categorical_features:
 print(cat_fea + "的特征分布如下:")
 print("{}特征有个{}不同的值".format(cat_fea, Train_data[cat_fea].nunique()))
 print(Train_data[cat_fea].value_counts())

2.3.7 数字特征分析

numeric_features.append('price')
numeric_features

Out[55]:
['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',
 'price']
Out[56]:
SaleID name regDate model brand bodyType fuelType gearbox power kilometer ...
0 0 736 20040402 30.0 6 1.0 0.0 0.0 60 12.5 ...
1 1 2262 20030301 40.0 1 2.0 0.0 0.0 0 15.0 ...
2 2 14874 20040403 115.0 15 1.0 0.0 0.0 163 12.5 ...
3 3 71865 19960908 109.0 10 0.0 0.0 1.0 193 15.0 ...
4 4 111080 20120103 110.0 5 1.0 0.0 0.0 68 5.0 ...
5 rows × 29 columns
numeric_features.append('price')
numeric_features
Train_data.head()
In [57]:
## 1) 相关性分析
price_numeric = Train_data[numeric_features]
correlation = price_numeric.corr()
print(correlation['price'].sort_values(ascending = False),'\n')
price 1.000000
v_12 0.692823
v_8 0.685798
v_0 0.628397
power 0.219834
v_5 0.164317
v_2 0.085322
v_6 0.068970
v_1 0.060914
v_14 0.035911
v_13 -0.013993
v_7 -0.053024
v_4 -0.147085
v_9 -0.206205
v_10 -0.246175
v_11 -0.275320
kilometer -0.440519
v_3 -0.730946
Name: price, dtype: float64
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)

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值