学习者:天天向上-天天
建模与调参
学习目标
- 了解常用的机器学习模型,并掌握机器学习模型的建模与调参流程
- 完成相应学习打卡任务
内容介绍
- 线性回归模型
- 线性回归对于特征的要求
- 处理长尾分布
- 理解线性回归模型
- 模型性能验证
- 评价函数与目标函数
- 交叉检验方法
- 留一验证方法
- 针对时间序列问题的验证
- 绘制学习率曲线
- 绘制验证曲线
- 嵌入式特征选择
- Lasso回归
- Ridge回归
- 决策树
- 模型对比
- 常用线性模型
- 常用非线性模型
- 模型调参
- 贪心调参方法
- 网格调参方法
- 贝叶斯调调参方法
相关原理介绍与推荐
线性回归模型
线性回归模型是机器学习里面最简单的一个模型。
器最普通的形式是:
f
(
x
)
=
w
‘
x
+
b
f(x) = w^`x+b
f(x)=w‘x+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()