零基础入门数据挖掘 task4

Datawhale 零基础入门数据挖掘-Task4 建模调参

四、建模与调参

Tip:此部分为零基础入门数据挖掘的 Task4 建模调参 部分,带你来了解各种模型以及模型的评价和调参策略,欢迎大家后续多多交流。

赛题:零基础入门数据挖掘 - 二手车交易价格预测

地址:https://tianchi.aliyun.com/competition/entrance/231784/introduction?spm=5176.12281957.1004.1.38b02448ausjSX

4.1 学习目标

  • 了解常用的机器学习模型,并掌握机器学习模型的建模与调参流程
  • 完成相应学习打卡任务

4.2 内容介绍

  1. 线性回归模型:
    • 线性回归对于特征的要求;
    • 处理长尾分布;
    • 理解线性回归模型;
  2. 模型性能验证:
    • 评价函数与目标函数;
    • 交叉验证方法;
    • 留一验证方法;
    • 针对时间序列问题的验证;
    • 绘制学习率曲线;
    • 绘制验证曲线;
  3. 嵌入式特征选择:
    • Lasso回归;
    • Ridge回归;
    • 决策树;
  4. 模型对比:
    • 常用线性模型;
    • 常用非线性模型;
  5. 模型调参:
    • 贪心调参方法;
    • 网格调参方法;
    • 贝叶斯调参方法;

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 推荐教材:

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值