数据挖掘实践(30):算法基础(七)梯度提升
数据挖掘实践(30):算法基础(七)梯度提升
0 简介
0.1 主题
0.2 目标
1) 能够掌握传统的集成框架的类型
2) 能够掌握GBDT的算法过程
3) 能够掌握GBDT的残差
1 提升的概念
2 基本函数
所有可行的弱函数集合(基函数)
3 目标函数
3.1 目标函数策略
3.2 损失函数
4 最优求解思路
5 最优函数
5.1 过程简介
5.2 GBDT算法核心:残差拟合样本
5.3 小结
6 GDBT算法实验
import gzip import pickle as pkl from sklearn.model_selection import train_test_split def load_data(path): f = gzip.open(path, 'rb') try: #Python3 train_set, valid_set, test_set = pkl.load(f, encoding='latin1') except: #Python2 train_set, valid_set, test_set = pkl.load(f) f.close() return(train_set,valid_set,test_set) path = 'mnist.pkl.gz' train_set,valid_set,test_set = load_data(path) Xtrain,_,ytrain,_ = train_test_split(train_set[0], train_set[1], test_size=0.9) Xtest,_,ytest,_ = train_test_split(test_set[0], test_set[1], test_size=0.9) print(Xtrain.shape, ytrain.shape, Xtest.shape, ytest.shape)
(5000, 784) (5000,) (1000, 784) (1000,)
from sklearn.ensemble import GradientBoostingClassifier import numpy as np import time clf = GradientBoostingClassifier(n_estimators=10, learning_rate=0.1, max_depth=3) start_time = time.time() clf.fit(Xtrain, ytrain) end_time = time.time() print('The training time = {}'.format(end_time - start_time)) #prediction and evaluation pred = clf.predict(Xtest) accuracy = np.sum(pred == ytest) / pred.shape[0] print('Test accuracy = {}'.format(accuracy))
The training time = 22.512996673583984 Test accuracy = 0.807
- 集成算法可以得出特征的重要度,说白了就是看各个树使用的特征情况,使用的多当然就重要了,这是分类器告诉我们的
%matplotlib inline import matplotlib.pyplot as plt plt.hist(clf.feature_importances_) print(max(clf.feature_importances_), min(clf.feature_importances_))
0.042681420232887304 0.0
from collections import OrderedDict d = {} for i in range(len(clf.feature_importances_)): if clf.feature_importances_[i] 0.01: d[i] = clf.feature_importances_[i] sorted_feature_importances = OrderedDict(sorted(d.items(), key=lambda x:x[1], reverse=True)) D = sorted_feature_importances rects = plt.bar(range(len(D)), D.values(), align='center') plt.xticks(range(len(D)), D.keys(),rotation=90) plt.show()
7 Shrinkage(衰减)与Step(步长)
8 参数设置和正则化
9 总结
9.1 GDBT主要由Regression Decision Tree, Gradient Boosting, Shrinkage 三个概念组成
9.2 为什么GBDT的树深较RF通常比较浅(RF是通过减少模型的方差来提高性能,而GBDT是减少模型的偏差来提高性能的原理)
10 笔面试相关
10.1 什么是集成学习集成学习有哪些框架简单介绍各个框架的常用算法
10.2 GBDT相比于决策树有什么优点/
数据挖掘实践(30):算法基础(七)梯度提升 相关文章
- LeetCode:回溯算法
回溯算法 这部分主要是学习了 labuladong 公众号中对于回溯算法的讲解 刷了些 leetcode 题,在此做一些记录,不然没几天就忘光光了 总结 概述 回溯是 DFS 中的一种技巧。回溯法采用 试错 的思想,尝试分步的去解决一个问题,在分步解决问题的过程中,当它通
- 漫谈BBR算法的收敛点和公平性
其实写这篇的初衷起因于我对那些看见4个窗口就想加到8个窗口的人鄙视,并且这些人几乎都是狂暴之人,我发现那些做业务逻辑的只要懂点TCP都不会好好说话,事实上他们大多数人什么都不懂,只有什么都不懂的人才会自以为是,天天鄙视别人。 对于TCP的优化,我听
- 数据挖掘实践(28):算法基础(六)Random Forest(随机森林)算法(集成学习)(二)基于随机森林的医疗费用分析与建模预估
1 基于随机森林的医疗费用分析与建模预估 import warningswarnings.filterwarnings('ignore') # 忽视警告 import pandas as pdfrom matplotlib import pyplot as pltimport seaborn as snsfrom sklearn.ensemble import RandomForestRegressorfrom sklearn.p
- 数据挖掘实践(27):算法基础(五)Random Forest(随机森林)算法(集成学习)(一)
0 简介 0.1 主题 0.2 目标 1. Bootstraping与Bagging策略 1.1Bootstraping/自助算法 1.2 分类 1.3Bagging/套袋法 1.4 集成学习之结合策略 1.5 代码实验 import numpy as npimport os%matplotlib inlineimport matplotlibimport matplotlib.pyplot as pltplt.
- leetcode_回溯算法_python
该类问题的核心:递归过程都在“全局”变量里记录,for(横向的选择)都在递归函数里记录。 主要问题类型:排列问题,组合问题,子集问题,子序列,分割问题,其他NP问题 常用剪枝使得不重复:如果结果不要求次序与原list相对次序相同(组合,子集,排列)问
- 一篇文章图文并茂地带你轻松实践 HTML5 history api
HTML5 history api 前言 由于笔者在网络上没有找到比较好的关于 history api 的实践案例,有的案例过于杂乱,没有重点,有些案例只是告诉读者 api 是什么,却没告诉怎么用,本文章从零开始带读者实践 history api ,建议和笔者一起写一遍。 效果 注意 url 变
- 近期刷题感悟1(搜索)
一个经典算法题 给你两个数n,m,有加一减一乘2三种操作,问从n到m的最小操作次数是多少 首先我们很容易想到这是一个搜索题,dfs和bfs应该是都可以的,但是atcoder 188 F 那道题dfs能过,但是poj 3278 就是不行 我真就觉得没啥区别啊,运行错误是为啥,嘤嘤嘤
- 基于Dijkstra算法的郑州地铁路径规划
需要引入geopy库 pip installgeopy安装即可 import requestsfrom bs4 import BeautifulSoupimport pandas as pdimport jsonimport osfrom tqdm import tqdmfrom collections import defaultdictimport pickleimport itertoolsfrom geopy.distance import geo
- KMP算法
KMP 算法(Knuth-Morris-Pratt 算法)是一个著名的字符串匹配算法。 对于字符串匹配,最简单的做法是暴力法双层循环依次对比。 int search(String pat, String txt) { int M = pat.length; int N = txt.length; for (int i = 0; i = N - M; i++) { int j; fo
- 数据挖掘实践(24):实战-- 建筑能源得分预测报告(二)
3 特征工程 3.1 特征变换 import warningswarnings.filterwarnings("ignore")# 所有的数值数据拿到手numeric_subset = data.select_dtypes('number')# 遍历所有的数值数据for col in numeric_subset.columns: # 如果score就是y值 ,就不做任何变换 if col ==