集成学习下 01 blending集成学习算法

2023-10-07 22:39
文章标签 算法 学习 01 集成 blending

本文主要是介绍集成学习下 01 blending集成学习算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

集成学习下 01 blending集成学习算法

导读:开源学习地址datawhale

1.何为blending?

blending类似于对多个模型的效果进行融合,具体如下:
1)将数据划分为训练集和测试集,训练集再划分为训练集train_set和验证集val_set
2) 建立第一层的多个模型,通过train_set训练第一层的模型,预测val_set和测试集,得到val_predict和test_predict1
3)创建第二层的模型,根据val_predict训练第二层的模型
4)根据训练好的第二层模型,预测test_predict1。预测结果为整个测试集的结果
在这里插入图片描述

2.示例

#方法二
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#获取鸢尾花数据
#仅仅考虑0,1类鸢尾花
iris = datasets.load_iris()
X = iris.data
y = iris.target
features = iris.feature_names
iris_data = pd.DataFrame(X, columns=features)
iris_data['target'] = y
# 数据预处理
# 仅仅考虑0,1类鸢尾花
iris_data = iris_data.loc[iris_data.target.isin([0, 1])]
y = iris_data['target'].values
X = iris_data[['sepal length (cm)', 'sepal width (cm)']].values## 创建训练集和测试集
X_train1, X_test, y_train1, y_test = train_test_split(X,y, 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 KNeighborsClassifierclfs = [SVC(probability = True),RandomForestClassifier(),KNeighborsClassifier()]# 设置第二层分类器
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()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(X_val)test_feature = clf.predict(X_test)val_features[:,i] = val_featuretest_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)#默认计算的是模型精度

The shape of training X: (56, 2)
The shape of training y: (56,)
The shape of test X: (20, 2)
The shape of test y: (20,)
The shape of validation X: (24, 2)
The shape of validation y: (24,)
Out[200]:
array([1., 1., 1., 1., 1.])

#决策边界
x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1
y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1),np.arange(y_min, y_max, 0.1))
# Obtain labels for each point in mesh using the model.
X_TEST=np.c_[xx.ravel(), yy.ravel()]
meta_X=np.zeros((len(X_TEST),len(clfs)))
for i,clf in enumerate(clfs):yhat= clf.predict(X_TEST)meta_X[:,i]=yhat
# 第二层模型预测
Z = lr.predict(meta_X).reshape(-1, 1).reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.3)
plt.scatter(X_test[y_test == 0, 0], X_test[y_test == 0, 1], c='blue', marker='^')
plt.scatter(X_test[y_test == 1, 0], X_test[y_test == 1, 1], c='red', marker='o')

在这里插入图片描述

这篇关于集成学习下 01 blending集成学习算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/160895

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot集成XXL-JOB实现任务管理全流程

《SpringBoot集成XXL-JOB实现任务管理全流程》XXL-JOB是一款轻量级分布式任务调度平台,功能丰富、界面简洁、易于扩展,本文介绍如何通过SpringBoot项目,使用RestTempl... 目录一、前言二、项目结构简述三、Maven 依赖四、Controller 代码详解五、Service

springboot2.1.3 hystrix集成及hystrix-dashboard监控详解

《springboot2.1.3hystrix集成及hystrix-dashboard监控详解》Hystrix是Netflix开源的微服务容错工具,通过线程池隔离和熔断机制防止服务崩溃,支持降级、监... 目录Hystrix是Netflix开源技术www.chinasem.cn栈中的又一员猛将Hystrix熔

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

SpringBoot集成P6Spy的实现示例

《SpringBoot集成P6Spy的实现示例》本文主要介绍了SpringBoot集成P6Spy的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录本节目标P6Spy简介抛出问题集成P6Spy1. SpringBoot三板斧之加入依赖2. 修改

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal

SpringBoot集成Shiro+JWT(Hutool)完整代码示例

《SpringBoot集成Shiro+JWT(Hutool)完整代码示例》ApacheShiro是一个强大且易用的Java安全框架,提供了认证、授权、加密和会话管理功能,在现代应用开发中,Shiro因... 目录一、背景介绍1.1 为什么使用Shiro?1.2 为什么需要双Token?二、技术栈组成三、环境