美文网首页
机器学习 之 集成学习和随机森林

机器学习 之 集成学习和随机森林

作者: 音符纸飞机 | 来源:发表于2018-12-12 23:04 被阅读38次

    如果你合并了一组分类器的预测(像分类或者回归),会得到一个比单一分类器更好的预测结果。这一组分类器就叫做集成。因此,这个技术就叫做集成学习,一个集成学习算法就叫做集成方法。
    以训练一组决策树分类器,每一个都在一个随机的训练集上。为了去做预测,必须得到所有单一树的预测值,然后通过投票来预测类别。例如一种决策树的集成就叫做随机森林

    1.投票分类

    投票分类
    from sklearn.model_selection import train_test_split
    from sklearn.datasets import make_moons
    
    X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.ensemble import VotingClassifier
    from sklearn.linear_model import LogisticRegression
    from sklearn.svm import SVC
    
    log_clf = LogisticRegression(random_state=42)
    rnd_clf = RandomForestClassifier(random_state=42)
    svm_clf = SVC(random_state=42)
    
    voting_clf = VotingClassifier(
        estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
        voting='hard')
    
    from sklearn.metrics import accuracy_score
    
    for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
        clf.fit(X_train, y_train)
        y_pred = clf.predict(X_test)
        print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
    '''
    LogisticRegression 0.864
    RandomForestClassifier 0.872
    SVC 0.888
    VotingClassifier 0.896
    '''
    

    如果所有的分类器都能够预测类别的概率(例如他们有一个 predict_proba() 方法),那么你就可以让 sklearn 以最高的类概率来预测这个类,平均在所有的分类器上。这种方式叫做软投票。他经常比硬投票表现的更好,因为它给予高自信的投票更大的权重。你可以通过把 voting="hard" 设置为 voting="soft" 来保证分类器可以预测类别概率。然而这不是 SVC类的分类器默认的选项,所以你需要把它的 probability hyperparameter 设置为 True (这会使 SVC 使用交叉验证去预测类别概率,其降低了训练速度,但会添加 predict_proba() 方法)。如果修改了之前的代码去使用软投票,会发现投票分类器正确率高达 91%

    2. Bagging, Pasting

    对每一个分类器都使用相同的训练算法,但是在不同的训练集上去训练它们。有放回采样被称为装袋(Bagging,是 bootstrap aggregating 的缩写)。无放回采样称为粘贴(pasting)。


    Pasting/bagging
    from sklearn.ensemble import BaggingClassifier
    from sklearn.tree import DecisionTreeClassifier
    
    # 如果想尝试Pasting,就设置 bootstrap=False
    bag_clf = BaggingClassifier(
        DecisionTreeClassifier(random_state=42), n_estimators=500,
        max_samples=100, bootstrap=True, n_jobs=-1, random_state=42)
    bag_clf.fit(X_train, y_train)
    y_pred = bag_clf.predict(X_test)
    
    from sklearn.metrics import accuracy_score
    print(accuracy_score(y_test, y_pred))
    # 0.904
    
    
    tree_clf = DecisionTreeClassifier(random_state=42)
    tree_clf.fit(X_train, y_train)
    y_pred_tree = tree_clf.predict(X_test)
    print(accuracy_score(y_test, y_pred_tree))
    # 0.856
    
    from matplotlib.colors import ListedColormap
    
    def plot_decision_boundary(clf, X, y, axes=[-1.5, 2.5, -1, 1.5], alpha=0.5, contour=True):
        x1s = np.linspace(axes[0], axes[1], 100)
        x2s = np.linspace(axes[2], axes[3], 100)
        x1, x2 = np.meshgrid(x1s, x2s)
        X_new = np.c_[x1.ravel(), x2.ravel()]
        y_pred = clf.predict(X_new).reshape(x1.shape)
        custom_cmap = ListedColormap(['#fafab0','#9898ff','#a0faa0'])
        plt.contourf(x1, x2, y_pred, alpha=0.3, cmap=custom_cmap)
        if contour:
            custom_cmap2 = ListedColormap(['#7d7d58','#4c4c7f','#507d50'])
            plt.contour(x1, x2, y_pred, cmap=custom_cmap2, alpha=0.8)
        plt.plot(X[:, 0][y==0], X[:, 1][y==0], "yo", alpha=alpha)
        plt.plot(X[:, 0][y==1], X[:, 1][y==1], "bs", alpha=alpha)
        plt.axis(axes)
        plt.xlabel(r"$x_1$", fontsize=18)
        plt.ylabel(r"$x_2$", fontsize=18, rotation=0)
    
    plt.figure(figsize=(11,4))
    plt.subplot(121)
    plot_decision_boundary(tree_clf, X, y)
    plt.title("Decision Tree", fontsize=14)
    plt.subplot(122)
    plot_decision_boundary(bag_clf, X, y)
    plt.title("Decision Trees with Bagging", fontsize=14)
    plt.show()
    
    Bagging与决策树对比
    out of bag

    对于 Bagging 来说,一些实例可能被一些分类器重复采样,但其他的有可能不会被采样。 BaggingClassifier 默认采样。 BaggingClassifier 默认是有放回的采样 m 个实例( bootstrap=True ),其中 m 是训练集的大小,这意味着平均下来只有63%的训练实例被每个分类器采样,剩下的37%个没有被采样的训练实例就叫做 Out-of-Bag 实例。

    bag_clf = BaggingClassifier(
        DecisionTreeClassifier(random_state=42), n_estimators=500,
        max_samples=100, bootstrap=True, n_jobs=-1, random_state=42, oob_score=True)
    bag_clf.fit(X_train, y_train)
    
    print(bag_clf.oob_score_)
    # 0.925333
    

    3. 随机森林

    随机森林是决策树的一种集成。

    bagging方式
    bag_clf = BaggingClassifier(
        DecisionTreeClassifier(splitter="random", max_leaf_nodes=16, random_state=42),
        n_estimators=500, max_samples=1.0, bootstrap=True, n_jobs=-1, random_state=42)
    
    bag_clf.fit(X_train, y_train)
    y_pred = bag_clf.predict(X_test)
    '''
    [0 0 0 1 1 1 0 0 0 0 1 0 1 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 1 0 1 1 0 0 1 0 0
     1 1 1 1 1 0 0 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 1 0 1 1 0 1 0 0 0 0 1 0 0 1 1
     0 0 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 0 0 1 0 1 0 1 0 1 1 0 0 0 0 0 1 1 1
     0 0 1 0 0 0 0 0 1 1 1 0 0 0]
    '''
    
    RandomForestClassifier
    from sklearn.ensemble import RandomForestClassifier
    # 训练了带有 500 个树(每个被限制为 16 叶子结点)的决策森林
    rnd_clf = RandomForestClassifier(n_estimators=500, max_leaf_nodes=16, n_jobs=-1, random_state=42)
    rnd_clf.fit(X_train, y_train)
    
    y_pred_rf = rnd_clf.predict(X_test)
    
    '''
    [0 0 0 1 1 1 0 0 0 0 1 0 1 1 1 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 1 1 0 0 1 0 0
     1 1 1 1 1 0 0 0 0 1 0 1 1 1 1 0 0 1 0 1 1 0 1 0 1 1 0 1 0 0 0 0 1 0 0 1 1
     0 0 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 0 0 1 0 1 0 1 0 1 1 0 0 0 0 0 1 1 1
     0 0 1 1 0 0 0 0 1 1 1 0 0 0]
    '''
    np.sum(y_pred == y_pred_rf) / len(y_pred)
    # 0.976
    

    特征重要度

    观察一个单一决策树,重要的特征会出现在更靠近根部的位置,而不重要的特征会经常出现在靠近叶子的位置。因此我们可以通过计算一个特征在森林的全部树中出现的平均深度来预测特征的重要性。
    sklearn 在训练后会自动计算每个特征的重要度。可以通过 feature_importances_ 变量来查看结果。

    from sklearn.datasets import load_iris
    iris = load_iris()
    rnd_clf = RandomForestClassifier(n_estimators=500, n_jobs=-1, random_state=42)
    rnd_clf.fit(iris["data"], iris["target"])
    for name, score in zip(iris["feature_names"], rnd_clf.feature_importances_):
        print(name, score)
    '''
    sepal length (cm) 0.11249225099876374
    sepal width (cm) 0.023119288282510326
    petal length (cm) 0.44103046436395765
    petal width (cm) 0.4233579963547681
    '''
    rnd_clf.feature_importances_
    # array([0.11249225, 0.02311929, 0.44103046, 0.423358  ])
    

    4. Boosting(提升)

    指的是可以将几个弱学习者组合成强学习者的集成方法。对于大多数的提升方法的思想就是按顺序去训练分类器,每一个都要尝试修正前面的分类。

    Adaboost

    适应性提升,Adaptive Boosting。使一个新的分类器去修正之前分类结果的方法就是对之前分类结果不对的训练实例多加关注。这导致新的预测因子越来越多地聚焦于这些之前分类错误的实例。(增加权重)

    Adaboost训练过程中实例权重的变化
    from sklearn.ensemble import AdaBoostClassifier
    
    ada_clf = AdaBoostClassifier(
    # 200次迭代
        DecisionTreeClassifier(max_depth=1), n_estimators=200,
        algorithm="SAMME.R", learning_rate=0.5, random_state=42)
    ada_clf.fit(X_train, y_train)
    
    plot_decision_boundary(ada_clf, X, y)
    
    sklearn的Ababoost训练结果
    五次SVM
    m = len(X_train)
    
    plt.figure(figsize=(11, 4))
    for subplot, learning_rate in ((121, 1), (122, 0.5)):
        sample_weights = np.ones(m)
        plt.subplot(subplot)
        for i in range(5):
            svm_clf = SVC(kernel="rbf", C=0.05, random_state=42)
            svm_clf.fit(X_train, y_train, sample_weight=sample_weights)
            y_pred = svm_clf.predict(X_train)
            sample_weights[y_pred != y_train] *= (1 + learning_rate)
            plot_decision_boundary(svm_clf, X, y, alpha=0.2)
            plt.title("learning_rate = {}".format(learning_rate), fontsize=16)
        if subplot == 121:
            plt.text(-0.7, -0.65, "1", fontsize=14)
            plt.text(-0.6, -0.10, "2", fontsize=14)
            plt.text(-0.5,  0.10, "3", fontsize=14)
            plt.text(-0.4,  0.55, "4", fontsize=14)
            plt.text(-0.3,  0.90, "5", fontsize=14)
    
    plt.show()
    
    五次SVM的Adaboost

    梯度提升 Gradient Boosting

    使用新的分类器去拟合前面分类器预测的残差 。

    算法过程实践
    np.random.seed(42)
    X = np.random.rand(100, 1) - 0.5
    y = 3*X[:, 0]**2 + 0.05 * np.random.randn(100)
    
    from sklearn.tree import DecisionTreeRegressor
    
    tree_reg1 = DecisionTreeRegressor(max_depth=2, random_state=42)
    tree_reg1.fit(X, y)
    
    # 第一个分类器的残差
    y2 = y - tree_reg1.predict(X)
    tree_reg2 = DecisionTreeRegressor(max_depth=2, random_state=42)
    tree_reg2.fit(X, y2)
    
    y3 = y2 - tree_reg2.predict(X)
    tree_reg3 = DecisionTreeRegressor(max_depth=2, random_state=42)
    tree_reg3.fit(X, y3)
    
    X_new = np.array([[0.8]])
    y_pred = sum(tree.predict(X_new) for tree in (tree_reg1, tree_reg2, tree_reg3))
    # 0.75
    
    
    def plot_predictions(regressors, X, y, axes, label=None, style="r-", data_style="b.", data_label=None):
        x1 = np.linspace(axes[0], axes[1], 500)
        y_pred = sum(regressor.predict(x1.reshape(-1, 1)) for regressor in regressors)
        plt.plot(X[:, 0], y, data_style, label=data_label)
        plt.plot(x1, y_pred, style, linewidth=2, label=label)
        if label or data_label:
            plt.legend(loc="upper center", fontsize=16)
        plt.axis(axes)
    
    plt.figure(figsize=(11,11))
    
    plt.subplot(321)
    plot_predictions([tree_reg1], X, y, axes=[-0.5, 0.5, -0.1, 0.8], label="$h_1(x_1)$", style="g-", data_label="Training set")
    plt.ylabel("$y$", fontsize=16, rotation=0)
    plt.title("Residuals and tree predictions", fontsize=16)
    
    plt.subplot(322)
    plot_predictions([tree_reg1], X, y, axes=[-0.5, 0.5, -0.1, 0.8], label="$h(x_1) = h_1(x_1)$", data_label="Training set")
    plt.ylabel("$y$", fontsize=16, rotation=0)
    plt.title("Ensemble predictions", fontsize=16)
    
    plt.subplot(323)
    plot_predictions([tree_reg2], X, y2, axes=[-0.5, 0.5, -0.5, 0.5], label="$h_2(x_1)$", style="g-", data_style="k+", data_label="Residuals")
    plt.ylabel("$y - h_1(x_1)$", fontsize=16)
    
    plt.subplot(324)
    plot_predictions([tree_reg1, tree_reg2], X, y, axes=[-0.5, 0.5, -0.1, 0.8], label="$h(x_1) = h_1(x_1) + h_2(x_1)$")
    plt.ylabel("$y$", fontsize=16, rotation=0)
    
    plt.subplot(325)
    plot_predictions([tree_reg3], X, y3, axes=[-0.5, 0.5, -0.5, 0.5], label="$h_3(x_1)$", style="g-", data_style="k+")
    plt.ylabel("$y - h_1(x_1) - h_2(x_1)$", fontsize=16)
    plt.xlabel("$x_1$", fontsize=16)
    
    plt.subplot(326)
    plot_predictions([tree_reg1, tree_reg2, tree_reg3], X, y, axes=[-0.5, 0.5, -0.1, 0.8], label="$h(x_1) = h_1(x_1) + h_2(x_1) + h_3(x_1)$")
    plt.xlabel("$x_1$", fontsize=16)
    plt.ylabel("$y$", fontsize=16, rotation=0)
    
    plt.show()
    
    梯度提升过程解析
    sklearn中的梯度提升
    from sklearn.ensemble import GradientBoostingRegressor
    
    gbrt = GradientBoostingRegressor(max_depth=2, n_estimators=3, learning_rate=1.0, random_state=42)
    gbrt.fit(X, y)
    
    gbrt_slow = GradientBoostingRegressor(max_depth=2, n_estimators=200, learning_rate=0.1, random_state=42)
    gbrt_slow.fit(X, y)
    
    plt.figure(figsize=(11,4))
    
    plt.subplot(121)
    plot_predictions([gbrt], X, y, axes=[-0.5, 0.5, -0.1, 0.8], label="Ensemble predictions")
    plt.title("learning_rate={}, n_estimators={}".format(gbrt.learning_rate, gbrt.n_estimators), fontsize=14)
    
    plt.subplot(122)
    plot_predictions([gbrt_slow], X, y, axes=[-0.5, 0.5, -0.1, 0.8])
    plt.title("learning_rate={}, n_estimators={}".format(gbrt_slow.learning_rate, gbrt_slow.n_estimators), fontsize=14)
    
    plt.show()
    
    左面是一个没有足够树去拟合训练集的树,右面是有过多的树过拟合训练集的树。

    超参数 learning_rate 确立了每个树的贡献。如果你把它设置为一个很小的树,例如 0.1,在集成中就需要更多的树去拟合训练集,但预测通常会更好。

    运用早停技术
    import numpy as np
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import mean_squared_error
    
    X_train, X_val, y_train, y_val = train_test_split(X, y, random_state=49)
    
    gbrt = GradientBoostingRegressor(max_depth=2, n_estimators=120, random_state=42)
    gbrt.fit(X_train, y_train)
    
    errors = [mean_squared_error(y_val, y_pred)
              for y_pred in gbrt.staged_predict(X_val)]
    bst_n_estimators = np.argmin(errors)
    
    gbrt_best = GradientBoostingRegressor(max_depth=2,n_estimators=bst_n_estimators, random_state=42)
    gbrt_best.fit(X_train, y_train)
    
    min_error = np.min(errors)
    
    plt.figure(figsize=(11, 4))
    
    plt.subplot(121)
    plt.plot(errors, "b.-")
    plt.plot([bst_n_estimators, bst_n_estimators], [0, min_error], "k--")
    plt.plot([0, 120], [min_error, min_error], "k--")
    plt.plot(bst_n_estimators, min_error, "ko")
    plt.text(bst_n_estimators, min_error*1.2, "Minimum", ha="center", fontsize=14)
    plt.axis([0, 120, 0, 0.01])
    plt.xlabel("Number of trees")
    plt.title("Validation error", fontsize=14)
    
    plt.subplot(122)
    plot_predictions([gbrt_best], X, y, axes=[-0.5, 0.5, -0.1, 0.8])
    plt.title("Best model (%d trees)" % bst_n_estimators, fontsize=14)
    
    plt.show()
    
    加上早停技术的梯度提升回归

    5. Stacking

    训练一个模型来执行所有分类器输出的聚合,这个模型称为blender或者meta learner

    stacking原理
    如何训练这个blender?

    首先,训练集被分为两个子集,第一个子集被用作训练第一层。



    接下来,第一层的分类器被用来预测第二个子集(被称为保持集hold-on set)。现在对在保持集中的每一个实例都有三个预测值。
    现在使用这些预测结果作为输入特征来创建一个新的训练集(这使得这个训练集是三维的)。随后 blender 在这个新的训练集上训练,因此,它学会了预测第一层预测的目标值。



    显然可以用这种方法训练不同的 blender (例如一个线性回归,另一个是随机森林等等):我们得到了一层 blender 。诀窍是将训练集分成三个子集:第一个子集用来训练第一层,第二个子集用来创建训练第二层的训练集(使用第一层分类器的预测值),第三个子集被用来创建训练第三层的训练集(使用第二层分类器的预测值)。以上步骤做完了,我们可以通过逐个遍历每个层来预测一个新的实例。

    练习

    针对MNIST数据,把它切分进一个训练集,一个验证集,和一个测试集(例如 40000 个实例进行训练,10000 个进行验证,10000 个进行测试)。然后训练多个分类器,例如一个随机森林分类器,一个 Extra-Tree 分类器和一个 SVM。接下来,尝试将它们组合成集成,使用软或硬投票分类器来胜过验证集上的所有集合。

    MNIST数据加载参考这篇简书

    X_train_val, X_test, y_train_val, y_test = train_test_split(
        X_train, y_train, test_size=10000, random_state=42)
    X_train, X_val, y_train, y_val = train_test_split(
        X_train_val, y_train_val, test_size=10000, random_state=42)
    
    from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
    from sklearn.svm import LinearSVC
    from sklearn.neural_network import MLPClassifier
    from sklearn.ensemble import VotingClassifier
    
    random_forest_clf = RandomForestClassifier(random_state=42)
    extra_trees_clf = ExtraTreesClassifier(random_state=42)
    svm_clf = LinearSVC(random_state=42)
    mlp_clf = MLPClassifier(random_state=42)
    
    voting_clf = VotingClassifier(named_estimators)
    voting_clf.fit(X_train, y_train)
    voting_clf.score(X_val, y_val)
    # 0.9576
    
    [estimator.score(X_val, y_val) for estimator in voting_clf.estimators_]
    # [0.9393, 0.9463, 0.864, 0.9604]
    
    # remove svm classifier
    del voting_clf.estimators_[2]
    voting_clf.score(X_val, y_val)
    # 0.9614
    voting_clf.voting = "soft"
    voting_clf.score(X_val, y_val)
    # 0.9688
    voting_clf.score(X_test, y_test)
    # 0.967
    
    上面每一个单独的分类器来对验证集进行预测,并合成创建一个新的训练集。用一个新的分类器对其进行训练(训练了一个 blender) 用这个分类器来评估测试集。
    X_val_predictions = np.empty((len(X_val), len(estimators)), dtype=np.float32)
    
    for index, estimator in enumerate(estimators):
        X_val_predictions[:, index] = estimator.predict(X_val)
    
    '''
    array([[2., 2., 2., 2.],
           [8., 3., 3., 3.],
           [8., 8., 8., 8.],
           ...,
           [2., 8., 2., 2.],
           [3., 8., 8., 8.],
           [7., 7., 7., 7.]], dtype=float32)
    '''
    rnd_forest_blender = RandomForestClassifier(n_estimators=200, oob_score=True, random_state=42)
    rnd_forest_blender.fit(X_val_predictions, y_val)
    
    rnd_forest_blender.oob_score_
    # 0.9621
    
    X_test_predictions = np.empty((len(X_test), len(estimators)), dtype=np.float32)
    
    for index, estimator in enumerate(estimators):
        X_test_predictions[:, index] = estimator.predict(X_test)
    y_pred = rnd_forest_blender.predict(X_test_predictions)
    
    from sklearn.metrics import accuracy_score
    accuracy_score(y_test, y_pred)
    # 0.9586
    

    相关文章

      网友评论

          本文标题:机器学习 之 集成学习和随机森林

          本文链接:https://www.haomeiwen.com/subject/xcxlhqtx.html