通过调用sklearn库快速实现随机森林、决策树、极端随机树等分类算法
(方便大家学习以及自己以后使用)
代码
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 21 20:08:53 2020
@author: dell
"""
#import math
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
import numpy as np
n_features=2
x,y=make_blobs(n_samples=5000,n_features=n_features,centers=6) #生成数据及标签
x_train, x_test, y_train, y_test = train_test_split(x,y,random_state=1,train_size=0.7) #划分训练集、测试集
clf1=RandomForestClassifier(n_estimators=10,max_features=math.sqrt(n_features), max_depth=None,min_samples_split=2, bootstrap=True) #随机森林分类器
clf2=DecisionTreeClassifier(max_depth=None,min_samples_split=2,random_state=0) #决策树分类器
clf3=ExtraTreesClassifier(n_estimators=10,max_features=math.sqrt(n_features), max_depth=None,min_samples_split=2, bootstrap=False) #极端随机树分类器
scores1=cross_val_score(clf1,x_train,y_train) #交叉验证
scores2=cross_val_score(clf2,x_train,y_train)
scores3=cross_val_score(clf3,x_train,y_train)
print('RandomForestClassifier交叉验证概率为:' + str(scores1.mean()))
print('DecisionTreeClassifier交叉验证概率为:' + str(scores2.mean()))
print('ExtraTreesClassifier交叉验证概率为:' + str(scores3.mean()))
运行结果
RandomForestClassifier交叉验证概率为:0.9874285714285713
DecisionTreeClassifier交叉验证概率为:0.9865714285714287
ExtraTreesClassifier交叉验证概率为:0.9888571428571428