Python的Blending算法怎么用

发布时间:2022-01-28 13:42:14 作者:iii
来源:亿速云 阅读:89

这篇文章主要介绍了Python的Blending算法怎么用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Python的Blending算法怎么用文章都会有所收获,下面我们一起来看看吧。

一、前言

普通机器学习:从训练数据中学习一个假设。

集成方法:试图构建一组假设并将它们组合起来,集成学习是一种机器学习范式,多个学习器被训练来解决同一个问题。

集成方法分类为:

Bagging(并行训练):随机森林

Boosting(串行训练):Adaboost; GBDT; XgBoost

Stacking:

Blending:

或者分类为串行集成方法和并行集成方法

1.串行模型:通过基础模型之间的依赖,给错误分类样本一个较大的权重来提升模型的性能。

2.并行模型的原理:利用基础模型的独立性,然后通过平均能够较大地降低误差

二、Blending介绍

训练数据划分为训练和验证集+新的训练数据集和新的测试集

将训练数据进行划分,划分之后的训练数据一部分训练基模型,一部分经模型预测后作为新的特征训练元模型。
测试数据同样经过基模型预测,形成新的测试数据。最后,元模型对新的测试数据进行预测。Blending框架图如下所示:
注意:其是在stacking的基础上加了划分数据

三、Blending流程

四、案例

相关工具包加载

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)

输出预测的结果

lr.fit(val_features,y_val)
from sklearn.model_selection import cross_val_score
cross_val_score(lr,test_features,y_test,cv=5)

关于“Python的Blending算法怎么用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Python的Blending算法怎么用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 怎么用Python写算法题
  2. python快排算法怎么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python blending

上一篇:python常用魔法方法是什么

下一篇:jstat命令怎么使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》