Datawhale-河北高校邀请赛-二手车回归预测-task4模型调参

学习者:天天向上-天天

建模与调参

学习目标

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

内容介绍

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

相关原理介绍与推荐

线性回归模型

线性回归模型是机器学习里面最简单的一个模型。

器最普通的形式是:
f ( x ) = w ‘ x + b f(x) = w^`x+b f(x)=wx+b
其中 x向量代表一条样本 { x 1 , x 2 , x 3 , . . . , x n } {\{x_1,x_2,x_3,...,x_n\}} {x1,x2,x3,...,xn},其中 x 1 , x 2 , x 3 {x_1,x_2,x_3} x1,x2,x3等等代表样本的各个特征,w是一条向量代表的每个特征所占的权重,b偏移量,可以职位模型的basis或者bias。

损失函数

比如回归常用:

l o s s = ( f ( x ) − y ) 2 {loss = (f(x)-y)^2} loss=(f(x)y)2

我们希望能够减小loss,获得一个最佳的权重参数,这里采用最小二乘估计。

优化方法
  • 最小二乘
  • 梯度下降

决策树模型

在这里插入图片描述

GBDT模型,XGBoost模型,LightGBM模型

数学原理劝退

5折交叉检验

数据集常常被分为:训练集(train_set),评估集(valid_set), 测试集(test_set)

在训练集上训练模型,在评估集上验证模型,train_set 和valid_set没有交集,这种思想称为交叉检验(Cross Validation)

from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_absolute_error , make_scorer

def log_transfer(func):
    def wrapper(y, yhat):
        result = func(np.log(y), np.nan_to_num(np.log(yhat)))
        return result
    return wrapper
scores = cross_val_score(model,X= x_train,y=y_train,verbose=1,cv=5,scoring=make_scorer(log_transfer(mean_absolute_error)))

绘制学习率曲线与验证曲线

from sklearn.model_selection import learing_curve, validation_curve

def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,n_jobs=1, train_size=np.linspace(.1, 1.0, 5 )):  
    plt.figure()  
    plt.title(title)  
    if ylim is not None:  
        plt.ylim(*ylim)  
    plt.xlabel('Training example')  
    plt.ylabel('score')  
    train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_size, scoring = make_scorer(mean_absolute_error))  
    train_scores_mean = np.mean(train_scores, axis=1)  
    train_scores_std = np.std(train_scores, axis=1)  
    test_scores_mean = np.mean(test_scores, axis=1)  
    test_scores_std = np.std(test_scores, axis=1)  
    plt.grid()#区域  
    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,  
                     train_scores_mean + train_scores_std, alpha=0.1,  
                     color="r")  
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,  
                     test_scores_mean + test_scores_std, alpha=0.1,  
                     color="g")  
    plt.plot(train_sizes, train_scores_mean, 'o-', color='r',  
             label="Training score")  
    plt.plot(train_sizes, test_scores_mean,'o-',color="g",  
             label="Cross-validation score")  
    plt.legend(loc="best")  
    return plt  


plot_learning_curve(LinearRegression(), 'Liner_model', train_X[:1000], train_y_ln[:1000], ylim=(0.0, 0.5), cv=5, n_jobs=1)  

贪心调参

看了DataWhale的notebook,其贪心调参的思路就是:

每一趟只尝试调整模型的一个参数,

下一趟将上一趟效果最好的参数的值,作为这一趟模型对应参数的值,然后尝试调整另一个参数,找出效果最好的参数值,传到下一次调参过程中。

Grid Search 调参

notebook使用了sklearn封装好的函数

from sklearn.model_selection import GridSearchCV

搜了一下其原理,相当于对参数列表进行穷举,即在参数网格中,一一尝试。

非常暴力,相对而言其耗时较大

贝叶斯调参

贝叶斯真牛啊

这个的原理太复杂,暂时不想挖。

使用起来就是import模块,给出参数选择列表,然后交给模块封装好的函数就行了

安装:

pip install bayesian-optimization

from bayes_opt import BayesianOptimization
def rf_cv(num_leaves, max_depth, subsample, min_child_samples):
    val = cross_val_score(
        LGBMRegressor(objective = 'regression_l1',
            num_leaves=int(num_leaves),
            max_depth=int(max_depth),
            subsample = subsample,
            min_child_samples = int(min_child_samples)
        ),
        X=train_X, y=train_y_ln, verbose=0, cv = 5, scoring=make_scorer(mean_absolute_error)
    ).mean()
    return 1 - val
    
rf_bo = BayesianOptimization(
    rf_cv,
    {
    'num_leaves': (2, 100),
    'max_depth': (2, 100),
    'subsample': (0.1, 1),
    'min_child_samples' : (2, 100)
    }
)


rf_bo.maximize()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值