集成学习_随机森林分类

集成学习_随机森林分类_第1张图片

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn import neighbors
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import RandomForestClassifier



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()

def set_knn(x_data,y_data):#knn 模型
    knn = neighbors.KNeighborsClassifier()
    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 bigg_model(model,x_data,y_data,n):#Bagging 模型
    #建立若干个model模型,之后组合(按照概率)起来的模型
    Bagging=BaggingClassifier(model,n_estimators = n)
    Bagging.fit(x_data,y_data)
    return Bagging

def set_random_forest(x_data,y_data,n):#随机森林 模型
    forest = RandomForestClassifier(n_estimators = n)
    forest.fit(x_data,y_data)
    return forest

def main():
    print ("----------ing-------------")
    data = np.genfromtxt("testSet2.txt",delimiter=",")
    x_data= data[:,:-1]
    y_data= data[:,-1]
    x_train,x_test,y_train,y_test=train_test_split(x_data,y_data,test_size=0.3)

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

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

    bigg_knn = bigg_model(knn_model,x_train,y_train,10)
    score=bigg_knn.score(x_test,y_test)
    print("score=",score)
    draw(x_data,y_data,bigg_knn)

    bigg_tree = bigg_model(tree_model,x_train,y_train,10)
    score=bigg_tree.score(x_test,y_test)
    print("score=",score)
    draw(x_data,y_data,bigg_tree)

    forest_model = set_random_forest(x_train,y_train,50)
    score=forest_model.score(x_test,y_test)
    print("score=",score)
    draw(x_data,y_data,forest_model)
    
    print ("----------end-------------")

main()

集成学习_随机森林分类_第2张图片

集成学习_随机森林分类_第3张图片

你可能感兴趣的:(机器学习,AI,python)