Datawhale 零基础入门数据挖掘-Task4 建模调参
四、建模与调参
Tip:此部分为零基础入门数据挖掘的 Task4 建模调参 部分,带你来了解各种模型以及模型的评价和调参策略,欢迎大家后续多多交流。
赛题:零基础入门数据挖掘 - 二手车交易价格预测
4.1 学习目标
- 了解常用的机器学习模型,并掌握机器学习模型的建模与调参流程
- 完成相应学习打卡任务
4.2 内容介绍
- 线性回归模型:
- 线性回归对于特征的要求;
- 处理长尾分布;
- 理解线性回归模型;
- 模型性能验证:
- 评价函数与目标函数;
- 交叉验证方法;
- 留一验证方法;
- 针对时间序列问题的验证;
- 绘制学习率曲线;
- 绘制验证曲线;
- 嵌入式特征选择:
- Lasso回归;
- Ridge回归;
- 决策树;
- 模型对比:
- 常用线性模型;
- 常用非线性模型;
- 模型调参:
- 贪心调参方法;
- 网格调参方法;
- 贝叶斯调参方法;
4.3 相关原理介绍与推荐
由于相关算法原理篇幅较长,本文推荐了一些博客与教材供初学者们进行学习。
4.3.1 线性回归模型
https://zhuanlan.zhihu.com/p/49480391
4.3.2 决策树模型
https://zhuanlan.zhihu.com/p/65304798
4.3.3 GBDT模型
https://zhuanlan.zhihu.com/p/45145899
4.3.4 XGBoost模型
https://zhuanlan.zhihu.com/p/86816771
4.3.5 LightGBM模型
https://zhuanlan.zhihu.com/p/89360721
4.3.6 推荐教材:
- 《机器学习》 https://book.douban.com/subject/26708119/
- 《统计学习方法》 https://book.douban.com/subject/10590856/
- 《Python大战机器学习》 https://book.douban.com/subject/26987890/
- 《面向机器学习的特征工程》 https://book.douban.com/subject/26826639/
- 《数据科学家访谈录》 https://book.douban.com/subject/30129410/
4.4 代码示例
4.4.1 读取数据
1
import pandas as pd
2
import numpy as np
3
import warnings
4
warnings.filterwarnings('ignore')
reduce_mem_usage 函数通过调整数据类型,帮助我们减少数据在内存中占用的空间
1
def reduce_mem_usage(df):
2
""" iterate through all the columns of a dataframe and modify the data type
3
to reduce memory usage.
4
"""
5
start_mem = df.memory_usage().sum()
6
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
7
8
for col in df.columns:
9
col_type = df[col].dtype
10
11
if col_type != object:
12
c_min = df[col].min()
13
c_max = df[col].max()
14
if str(col_type)[:3] == 'int':
15
if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
16
df[col] = df[col].astype(np.int8)
17
elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
18
df[col] = df[col].astype(np.int16)
19
elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
20
df[col] = df[col].astype(np.int32)
21
elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
22
df[col] = df[col].astype(np.int64)
23
else:
24
if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
25
df[col] = df[col].astype(np.float16)
26
elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
27
df[col] = df[col].astype(np.float32)
28
else:
29
df[col] = df[col].astype(np.float64)
30
else:
31
df[col] = df[col].astype('category')
32
33
end_mem = df.memory_usage().sum()
34
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
35
print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))
36
return df
1
sample_feature = reduce_mem_usage(pd.read_csv('data_for_tree.csv'))
Memory usage of dataframe is 60507328.00 MB Memory usage after optimization is: 15724107.00 MB Decreased by 74.0%
1
continuous_feature_names = [x for x in sample_feature.columns if x not in ['price','brand','model','brand']]
4.4.2 线性回归 & 五折交叉验证 & 模拟真实业务情况
1
sample_feature = sample_feature.dropna().replace('-', 0).reset_index(drop=True)
2
sample_feature['notRepairedDamage'] = sample_feature['notRepairedDamage'].astype(np.float32)
3
train = sample_feature[continuous_feature_names + ['price']]
4
5
train_X = train[continuous_feature_names]
6
train_y = train['price']
4.4.2 - 1 简单建模
1
from sklearn.linear_model import LinearRegression
1
model = LinearRegression(normalize=True)
1
model = model.fit(train_X, train_y)
查看训练的线性回归模型的截距(int