Python进行Bagging和Adaboost

一、Bagging

第一步,导入数据和库;
# 导入库
from sklearn import datasets
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 使文字可以展示
plt.rcParams['font.sans-serif'] = ['SimHei']
# 使负号可以展示
plt.rcParams['axes.unicode_minus'] = False

# 读取数据
data = pd.read_excel('F:\\Desktop\\建模数据.xlsx')
data[:5]
第二步,数据处理;
# 设置 X 和 y
X = data.iloc[:, 1:]
y = data.iloc[:, 0]

from sklearn.cross_validation import train_test_split
# 设置训练数据集和测试数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)

# 数据标准化
from sklearn.preprocessing import StandardScaler
stdsc = StandardScaler()
# 将训练数据标准化
X_train_std = stdsc.fit_transform(X_train)
# 将测试数据标准化
X_test_std = stdsc.transform(X_test)
第三步,Bagging;
# 拟合500棵决策树
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier

tree = DecisionTreeClassifier(criterion='entropy', 
                              max_depth=None,
                              random_state=1)

bag = BaggingClassifier(base_estimator=tree,
                        n_estimators=500, 
                        max_samples=1.0, 
                        max_features=1.0, 
                        bootstrap=True, 
                        bootstrap_features=False, 
                        n_jobs=1, 
                        random_state=1)
# 比较 单个决策树 和 bagging 的区别
from sklearn.metrics import accuracy_score

tree = tree.fit(X_train, y_train)
y_train_pred = tree.predict(X_train)
y_test_pred = tree.predict(X_test)

tree_train = accuracy_score(y_train, y_train_pred)
tree_test = accuracy_score(y_test, y_test_pred)
# 查看训练集和测试集的准确率
print('Decision tree train/test accuracies %.3f/%.3f'
      % (tree_train, tree_test))

bag = bag.fit(X_train, y_train)
y_train_pred = bag.predict(X_train)
y_test_pred = bag.predict(X_test)

bag_train = accuracy_score(y_train, y_train_pred) 
bag_test = accuracy_score(y_test, y_test_pred) 
# 查看训练集和测试集的准确率
print('Bagging train/test accuracies %.3f/%.3f'
      % (bag_train, bag_test))
第四步,模型评价;
(1)混淆矩阵
# 绘制Bagging混淆矩阵
from sklearn.metrics import confusion_matrix
y_pred1 = bag.predict(X_test)
confmat1 = confusion_matrix(y_true=y_test, y_pred=y_pred1)
print(confmat1)
# 将混淆矩阵可视化
# 将混淆矩阵可视化
fig, ax = plt.subplots(figsize=(2.5, 2.5))
ax.matshow(confmat1, cmap=plt.cm.Blues, alpha=0.3)
for i in range(confmat1.shape[0]):
    for j in range(confmat1.shape[1]):
        ax.text(x=j, y=i, s=confmat1[i, j], va='center', ha='center')

plt.xlabel('预测类标')
plt.ylabel('真实类标')
plt.show()

在这里插入图片描述

(2)ROC曲线
from sklearn.metrics import roc_curve, auc
from scipy import interp


# 设置图形大小
fig = plt.figure(figsize=(7, 5))

# 计算 预测率---使用测试数据集
probas = tree.fit(X_train, y_train).predict_proba(X_test)
probas1 = bag.fit(X_train, y_train).predict_proba(X_test)

# 计算 fpr,tpr    
fpr, tpr, thresholds = roc_curve(y_test, probas[:, 1], pos_label=1)
fpr1, tpr1, thresholds1 = roc_curve(y_test, probas1[:, 1], pos_label=1)

# 计算 AUC 值
roc_auc = auc(fpr, tpr)
roc_auc1 = auc(fpr1, tpr1)

# 画 ROC 曲线
plt.plot(fpr, tpr, lw=1, color = 'b', label='决策树ROC (area = %0.2f)' 
                    % ( roc_auc))

plt.plot(fpr1, tpr1, lw=1, color = 'y', label='BaggingROC (area = %0.2f)' 
                    % ( roc_auc1))


# 画斜线
plt.plot([0, 1], [0, 1], linestyle='--', color=(0.6, 0.6, 0.6), label='random guessing')
# 画完美表现 线
plt.plot([0, 0, 1], 
         [0, 1, 1], 
         lw=2, 
         linestyle=':', 
         color='black', 
         label='perfect performance')
# 设置坐标轴范围
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
# 设置坐标轴标题
plt.xlabel('假正率')
plt.ylabel('真正率')
# 设置标题
plt.title('')
# 设置图例位置
plt.legend(loc="lower right")
plt.show()

在这里插入图片描述

二、Adaboost

第一步,Adaboost;
from sklearn.ensemble import AdaBoostClassifier

tree = DecisionTreeClassifier(criterion='entropy', 
                              max_depth=1,
                              random_state=0)

ada = AdaBoostClassifier(base_estimator=tree,
                         n_estimators=500, 
                         learning_rate=0.1,
                         random_state=0)



# # 比较 决策树 和 Adaboost 的区别
tree = tree.fit(X_train, y_train)
y_train_pred = tree.predict(X_train)
y_test_pred = tree.predict(X_test)

tree_train = accuracy_score(y_train, y_train_pred)
tree_test = accuracy_score(y_test, y_test_pred)
print('Decision tree train/test accuracies %.3f/%.3f'
      % (tree_train, tree_test))

ada = ada.fit(X_train, y_train)
y_train_pred = ada.predict(X_train)
y_test_pred = ada.predict(X_test)

ada_train = accuracy_score(y_train, y_train_pred) 
ada_test = accuracy_score(y_test, y_test_pred) 
print('AdaBoost train/test accuracies %.3f/%.3f'
      % (ada_train, ada_test))
第二步,模型评价
(1)混淆矩阵
# 绘制Adaboost混淆矩阵
from sklearn.metrics import confusion_matrix
y_pred1 = ada.predict(X_test)
confmat1 = confusion_matrix(y_true=y_test, y_pred=y_pred1)
print(confmat1)
# 将混淆矩阵可视化
fig, ax = plt.subplots(figsize=(2.5, 2.5))
ax.matshow(confmat1, cmap=plt.cm.Blues, alpha=0.3)
for i in range(confmat1.shape[0]):
    for j in range(confmat1.shape[1]):
        ax.text(x=j, y=i, s=confmat1[i, j], va='center', ha='center')

plt.xlabel('预测类标')
plt.ylabel('真实类标')
plt.show()

在这里插入图片描述

(2)ROC曲线
from sklearn.metrics import roc_curve, auc
from scipy import interp


# 设置图形大小
fig = plt.figure(figsize=(7, 5))

# 计算 预测率---使用测试数据集
probas = tree.fit(X_train, y_train).predict_proba(X_test)
probas1 = ada.fit(X_train, y_train).predict_proba(X_test)

# 计算 fpr,tpr    
fpr, tpr, thresholds = roc_curve(y_test, probas[:, 1], pos_label=1)
fpr1, tpr1, thresholds1 = roc_curve(y_test, probas1[:, 1], pos_label=1)

# 计算 AUC 值
roc_auc = auc(fpr, tpr)
roc_auc1 = auc(fpr1, tpr1)

# 画 ROC 曲线
plt.plot(fpr, tpr, lw=1, color = 'b', label='决策树ROC (area = %0.2f)' 
                    % ( roc_auc))

plt.plot(fpr1, tpr1, lw=1, color = 'y', label='AdaROC (area = %0.2f)' 
                    % ( roc_auc1))


# 画斜线
plt.plot([0, 1], [0, 1], linestyle='--', color=(0.6, 0.6, 0.6), label='random guessing')
# 画完美表现 线
plt.plot([0, 0, 1], [0, 1, 1], lw=2, linestyle=':',  color='black',label='perfect performance')
# 设置坐标轴范围
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
# 设置坐标轴标题
plt.xlabel('假正率')
plt.ylabel('真正率')
# 设置标题
plt.title('')
# 设置图例位置
plt.legend(loc="lower right")
plt.show()

在这里插入图片描述

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值