博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
集成学习之blending
阅读量:3948 次
发布时间:2019-05-24

本文共 2142 字,大约阅读时间需要 7 分钟。

集成学习之blending

方法实现步骤介绍

1、将数据集划分为训练集和测试集,假设有10000个样本,训练集占(80%,8000),测试集占(20%,2000)。然后再将训练集划分为训练集和验证集,其中训练集占70%,验证集占(30%,2400)。

2、通过K个同质或不同质的基训练器,以训练集数据进行拟合,将拟合得到的模型对验证集和测试集数据进行预测,将拟合结果作为新的变量标签加入各样本。
3、这时,每个样本都有K个基础模型预测结果的变量,将这K个变量作为自变量,利用验证集数据去拟合预测目标变量,得到第二层模型
4、使用第二层模型对验证集数据进行预测,根据结果报出准确率等一系列评价指标。

方法优劣

优势:方法简单,易于理解

缺点:第二层模型的拟合只用了全部样本的24%,没有充分的利用样本信息。

实现

# 加载相关工具包 import numpy as np  import pandas as pd  import matplotlib.pyplot as plt  plt.style.use("ggplot")  %matplotlib inline  import seaborn as sns
# 创建数据 from sklearn import datasets from sklearn.datasets import make_blobs from sklearn.model_selection import train_test_split data, target = make_blobs(n_samples=10000, centers=2, random_state=1, cluster_std=1.0 ) ## 创建训练集和测试集 X_train1,X_test,y_train1,y_test = train_test_split(data, target, test_size=0.2, random_state=1) ## 创建训练集和验证集 X_train,X_val,y_train,y_val = train_test_split(X_train1, y_train1, test_size=0.3, random_state=1) print("The shape of training X:",X_train.shape) print("The shape of training y:",y_train.shape) print("The shape of test X:",X_test.shape) print("The shape of test y:",y_test.shape) print("The shape of validation X:",X_val.shape) print("The shape of validation y:",y_val.shape)

在这里插入图片描述

# 设置第一层分类器 from sklearn.svm import SVC from sklearn.ensemble import RandomForestClassifier from sklearn.neighbors import KNeighborsClassifier clfs = [SVC(probability = True),RandomForestClassifier(n_estimators=5, n_jobs=-1, criterion='gini'),KNeighborsClassifier()] # 设置第二层分类器 from sklearn.linear_model import LinearRegression lr = LinearRegression()
# 输出第一层的验证集结果与测试集结果 val_features = np.zeros((X_val.shape[0],len(clfs))) # 初始化验证集结果 test_features = np.zeros((X_test.shape[0],len(clfs))) # 初始化测试集结果 for i,clf in enumerate(clfs): 	clf.fit(X_train,y_train) 	val_feature = clf.predict_proba(X_val)[:, 1] 	test_feature = clf.predict_proba(X_test)[:,1] 	val_features[:,i] = val_feature 	test_features[:,i] = test_feature
# 将第一层的验证集的结果输入第二层训练第二层分类器 lr.fit(val_features,y_val) # 输出预测的结果 from sklearn.model_selection import cross_val_score cross_val_score(lr,test_features,y_test,cv=5)

在这里插入图片描述

联系(补做)

使用Blending方式对iris数据集进行预测,并用决策边界画出来。

转载地址:http://wogwi.baihongyu.com/

你可能感兴趣的文章
有关数据挖掘的10个常见问题
查看>>
电信数据挖掘之流失管理
查看>>
web DB优化思路
查看>>
敏捷笔记
查看>>
SOA业务理解与应用
查看>>
Google File System(中文翻译)
查看>>
Google's BigTable 原理 (翻译)
查看>>
MapReduce:超大机群上的简单数据处理
查看>>
设计模式笔记(转载)
查看>>
加站点加入IE的可信站点做法
查看>>
软件研发中的《破窗理论》
查看>>
敏捷的三种误区和五种改进
查看>>
vs2010一些设置
查看>>
Python 3 之多线程研究
查看>>
简单明了《a标签的href》跳转页面情况,看完秒懂!!!
查看>>
Android系统目录结构
查看>>
Activity的生命周期及启动模式整理
查看>>
android的IPC机制思维导图
查看>>
Fragment中mAdded和mDetached标志位
查看>>
Android的View事件机制思维导图
查看>>