集成学习_Stacking

33 篇文章 0 订阅
24 篇文章 0 订阅

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn import model_selection
from sklearn.model_selection import train_test_split
from sklearn import neighbors
from sklearn.ensemble import BaggingClassifier
from sklearn.linear_model import LogisticRegression
from mlxtend.classifier import StackingClassifier



def draw(x_data,y_data,model):
        x_min,x_max=x_data[:,0].min()-1,x_data[:,0].max()+1
        y_min,y_max=x_data[:,1].min()-1,x_data[:,1].max()+1
        xx,yy = np.meshgrid(np.arange(x_min,x_max,0.02),np.arange(y_min,y_max,0.02))

        z = model.predict(np.c_[xx.ravel(),yy.ravel()])
        z = z.reshape(xx.shape)

        cs = plt.contourf(xx,yy,z)
        plt.scatter(x_data[:,0],x_data[:,1],c=y_data)
        plt.show()
        #predictions = model.predict(x_data)
        #return (classification_report(predictions,y_data))
def set_knn(x_data,y_data):
    knn = neighbors.KNeighborsClassifier(n_neighbors=1)
    knn.fit(x_data,y_data)
    return knn

def set_tree(x_data,y_data):
    Tree = tree.DecisionTreeClassifier()
    Tree.fit(x_data,y_data)
    return Tree

def set_logistic(x_data,y_data):
    logistic = LogisticRegression()
    logistic.fit(x_data,y_data)
    return logistic

def set_stack(x_data,y_data):
    knn = neighbors.KNeighborsClassifier(n_neighbors=1)
    Tree = tree.DecisionTreeClassifier()
    logistic = LogisticRegression()

    lr = LogisticRegression()
    stack = StackingClassifier(classifiers=[knn,Tree,logistic],meta_classifier=lr)
    stack.fit(x_data,y_data)
    return stack
    
def main():
    print ("----------ing-------------")
    iris_data = load_iris()
    x_data= iris_data.data[:,1:3]
    y_data= iris_data.target
    
    knn = neighbors.KNeighborsClassifier(n_neighbors=1)
    Tree = tree.DecisionTreeClassifier()
    logistic = LogisticRegression()

    lr = LogisticRegression()
    stack = StackingClassifier(classifiers=[knn,Tree,logistic],meta_classifier=lr)
    for model,label in zip([knn,Tree,logistic,stack],["knn","decision tree","logisticregression","stackingclassifier"])    :
        scores = model_selection.cross_val_score(model,x_data,y_data,cv=3,scoring='accuracy')
        print("Accuracy:%0.3f,%s"%(scores.mean(),label))
    print ("----------end-------------")

main()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn import model_selection
from sklearn.model_selection import train_test_split
from sklearn import neighbors
from sklearn.ensemble import BaggingClassifier
from sklearn.linear_model import LogisticRegression
from mlxtend.classifier import StackingClassifier



def draw(x_data,y_data,model):
        x_min,x_max=x_data[:,0].min()-1,x_data[:,0].max()+1
        y_min,y_max=x_data[:,1].min()-1,x_data[:,1].max()+1
        xx,yy = np.meshgrid(np.arange(x_min,x_max,0.02),np.arange(y_min,y_max,0.02))

        z = model.predict(np.c_[xx.ravel(),yy.ravel()])
        z = z.reshape(xx.shape)

        cs = plt.contourf(xx,yy,z)
        plt.scatter(x_data[:,0],x_data[:,1],c=y_data)
        plt.show()
        #predictions = model.predict(x_data)
        #return (classification_report(predictions,y_data))
def set_knn(x_data,y_data):
    knn = neighbors.KNeighborsClassifier(n_neighbors=1)
    knn.fit(x_data,y_data)
    return knn

def set_tree(x_data,y_data):
    Tree = tree.DecisionTreeClassifier()
    Tree.fit(x_data,y_data)
    return Tree

def set_logistic(x_data,y_data):
    logistic = LogisticRegression()
    logistic.fit(x_data,y_data)
    return logistic

def set_stack(x_data,y_data):
    knn = neighbors.KNeighborsClassifier(n_neighbors=1)
    Tree = tree.DecisionTreeClassifier()
    logistic = LogisticRegression()

    lr = LogisticRegression()
    stack = StackingClassifier(classifiers=[knn,Tree,logistic],meta_classifier=lr)
    stack.fit(x_data,y_data)
    return stack
    
def main():
    print ("----------ing-------------")
    iris_data = load_iris()
    x_data= iris_data.data[:,1:3]
    y_data= iris_data.target

    x_train,x_test,y_train,y_test=train_test_split(x_data,y_data)

    knn_model=set_knn(x_train,y_train)
    knn_score=knn_model.score(x_test,y_test)
    print("knn_model score=",knn_score)
    draw(x_data,y_data,knn_model)


    tree_model=set_tree(x_train,y_train)
    tree_score=tree_model.score(x_test,y_test)
    print("tree_model score=",tree_score)
    draw(x_data,y_data,tree_model)

    logistic_model=set_logistic(x_train,y_train)
    logistic_score=logistic_model.score(x_test,y_test)
    print("logistic_model score=",logistic_score)
    draw(x_data,y_data,logistic_model)

    stack_model=set_stack(x_train,y_train)
    stack_score=stack_model.score(x_test,y_test)
    print("stack_model score=",stack_score)
    draw(x_data,y_data,stack_model)
    

    print ("----------end-------------")

main()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值