更新CART回归树的Sklearn 和 利用AdaBoost元算法提高分类的md

This commit is contained in:
jiangzhonglian
2017-03-14 17:48:43 +08:00
parent d8f25c2b87
commit 31f59dbec4
2 changed files with 102 additions and 21 deletions

View File

@@ -0,0 +1,26 @@
# 7) 利用AdaBoost元算法提高分类
* 元算法(meta-algorithm) 或 集成方法(ensemble method)
* 概念:是对其他算法进行组合的一种形式。
* 通俗来说: 当做重要决定时,大家可能都会考虑吸取多个专家而不只是一个人的意见。
机器学习处理问题时又何尝不是如此? 这就是元算法(meta-algorithm)背后的思想。
* AdaBoost(adaptive boosting: 自适应boosting)
* 能否使用弱分类器和多个实例来构建一个强分类器? 这是一个非常有趣的理论问题。
* 优点:泛化错误率低,易编码,可以应用在大部分分类器上,无参数调节。
* 缺点:对离群点敏感。
* 适用数据类型:数值型和标称型数据。
* bagging:基于数据随机重抽样的分类起构造方法
* 自举汇聚法(bootstrap aggregating),也称为bagging方法,是在从原始数据集选择S次后得到S个新数据集的一种技术。
* 1. 新数据集和原数据集的大小相等。
* 2. 每个数据集都是通过在原始数据集中随机选择一个样本来进行替换(替换:意味着可以多次选择同一个样本,也就有重复值)而得到的。
* 3. 该算法作用的数据集就会得到S个分类器,与此同时,选择分类器投票结果中最多的类别作为最后的分类结果。
* 4. 例如:随即森林(random forest)
* boosting
* boosting是一种与bagging很类似的技术。不论是boosting还是bagging当中,所使用的多个分类器的类型都是一致的。
* 区别是什么?
* 1. bagging:不同的分类器是通过串形训练而获得的,每个新分类器斗根据已训练出的分类器的性能来进行训练。
* 2. boosting:是通过集中关注被已有分类器错分的那些数据来获得新的分类器。
* 3. 由于boosting分类的结果是基于所有分类器的加权求和结果的,因此boosting与bagging不太一样。
* 4. bagging中的分类器权重是相等的,而boosting中的分类器权重并不相等,每个权重代表的是其对应分类器在上一轮迭代中的成功度。
* 目前boosting方法最流行的版本是: AdaBoost。

View File

@@ -1,50 +1,105 @@
#!/usr/bin/python
# coding:utf8
# '''
# Created on 2017-03-10
# Update on 2017-03-10
# author: jiangzhonglian
# content: 回归树
# '''
# print(__doc__)
# # Import the necessary modules and libraries
# import numpy as np
# from sklearn.tree import DecisionTreeRegressor
# import matplotlib.pyplot as plt
# # Create a random dataset
# rng = np.random.RandomState(1)
# X = np.sort(5 * rng.rand(80, 1), axis=0)
# y = np.sin(X).ravel()
# print X, '\n\n\n-----------\n\n\n', y
# y[::5] += 3 * (0.5 - rng.rand(16))
# # Fit regression model
# regr_1 = DecisionTreeRegressor(max_depth=2, min_samples_leaf=5)
# regr_2 = DecisionTreeRegressor(max_depth=5, min_samples_leaf=5)
# regr_1.fit(X, y)
# regr_2.fit(X, y)
# # Predict
# X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
# y_1 = regr_1.predict(X_test)
# y_2 = regr_2.predict(X_test)
# # Plot the results
# plt.figure()
# plt.scatter(X, y, c="darkorange", label="data")
# plt.plot(X_test, y_1, color="cornflowerblue", label="max_depth=2", linewidth=2)
# plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
# plt.xlabel("data")
# plt.ylabel("target")
# plt.title("Decision Tree Regression")
# plt.legend()
# plt.show()
'''
Created on 2017-03-10
Update on 2017-03-10
author: jiangzhonglian
content: 回归树
content: 模型树
'''
print(__doc__)
# Author: Noel Dawe <noel.dawe@gmail.com>
#
# License: BSD 3 clause
# Import the necessary modules and libraries
# importing necessary libraries
import numpy as np
from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import AdaBoostRegressor
# Create a random dataset
# Create the dataset
rng = np.random.RandomState(1)
X = np.sort(5 * rng.rand(80, 1), axis=0)
y = np.sin(X).ravel()
print X, '\n\n\n-----------\n\n\n', y
y[::5] += 3 * (0.5 - rng.rand(16))
X = np.linspace(0, 6, 100)[:, np.newaxis]
y = np.sin(X).ravel() + np.sin(6 * X).ravel() + rng.normal(0, 0.1, X.shape[0])
# Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2, min_samples_leaf=5)
regr_2 = DecisionTreeRegressor(max_depth=5, min_samples_leaf=5)
regr_1 = DecisionTreeRegressor(max_depth=4)
regr_2 = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4),
n_estimators=300, random_state=rng)
regr_1.fit(X, y)
regr_2.fit(X, y)
# Predict
X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)
y_1 = regr_1.predict(X)
y_2 = regr_2.predict(X)
# Plot the results
plt.figure()
plt.scatter(X, y, c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue", label="max_depth=2", linewidth=2)
plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
plt.scatter(X, y, c="k", label="training samples")
plt.plot(X, y_1, c="g", label="n_estimators=1", linewidth=2)
plt.plot(X, y_2, c="r", label="n_estimators=300", linewidth=2)
plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree Regression")
plt.title("Boosted Decision Tree Regression")
plt.legend()
plt.show()