机器学习---运用sklearn库实现随机森林分类

通过调用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
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值