美文网首页machinelearning
实验二:使用随机森林算法对红酒数据集进行分类建模过程

实验二:使用随机森林算法对红酒数据集进行分类建模过程

作者: Conn606 | 来源:发表于2020-10-10 21:41 被阅读0次

实验要求:

1、分类对象:from sklearn.datasets import load_wine

2、实验步骤:参考随机森林章节中的实验5,编写第1-12步骤程序代码,最终给出最优的模型参数。

源码下载

步骤

  • 一、导入各种我们需要的模块或者数据集等(导入就不注释了,不懂的看实验一)
from sklearn.datasets import load_wine
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
import numpy as np
  • 二、将数据实例化,评估

    这是最开始的分数,没经过调参的,random_state可以设为任意数

wine = load_wine()
rfc = RandomForestClassifier(random_state=0) #实例化
score_f=cross_val_score(rfc,wine.data,wine.target,cv=10).mean() #交叉验证10次的平均分
score_f
  • 三、画出n_estimators的学习曲线,找出最优的n_estimaters

    这段代码跑的比较久一点,n_job用来调用电脑的线程,可能会报错warnings.warn("Estimator fit failed. The score on this train-test",报错的可以注释掉

scorel = []    #定义一个列表,用来存放每次循环得到的score
for i in range(0,200,10):
    rfc = RandomForestClassifier(n_estimators=i+1,  #因为n_estimators不能为0,所以i要加1
                             #   n_jobs=-1,
                                 random_state=0)
    score = cross_val_score(rfc,wine.data,wine.target,cv=10).mean()
    scorel.append(score)
print(max(scorel),(scorel.index(max(scorel))*10)+1)  #打印出最好的score和他的索引
plt.figure(figsize=[20,5])
plt.plot(range(1,201,10),scorel)
plt.show()

因为我得出的结果索引是31,为了更精确的确定n_estimators,可以在更小的区间内再画曲线

scorel = []
for i in range(25,35):   #这个是更小的区间
    rfc = RandomForestClassifier(n_estimators=i,  #这里从25开始所以不用加1
                                # n_jobs=-1,
                                 random_state=0)
    score = cross_val_score(rfc,wine.data,wine.target,cv=10).mean()
    scorel.append(score)
print(max(scorel),([*range(25,35)][scorel.index(max(scorel))]))
plt.figure(figsize=[20,5])
plt.plot(range(25,35),scorel)
plt.show()

结果在n_estimators为29的时候分数是最高的


  • 四、用网格搜索找出其他的最优参数

1.首先找max_depth

#调整max_depth
param_grid = {'max_depth':np.arange(1, 14, 1)} #这里范围为1-14,步长为1是因为有13个特征,由wine.data.shape可查看
rfc = RandomForestClassifier(n_estimators=29 #这里记得加上刚刚调整出来的n_estimators
                             ,random_state=0
                            )
GS = GridSearchCV(rfc,param_grid,cv=10)#网格搜索
GS.fit(wine.data,wine.target)
GS.best_params_  #显示调整出来的最佳参数
GS.best_score_    #分数不变,说明max_depth不影响模型

2.调整max_features

#调整max_features
 
param_grid = {'max_features':np.arange(1,10,1)} 
 
rfc = RandomForestClassifier(n_estimators=29
                             ,random_state=0
                             #,max_depth=4  #如果我们加入了map_depth=4分数会更低,可以先加了再注释比较一下分数
                            )
GS = GridSearchCV(rfc,param_grid,cv=10)
GS.fit(wine.data,wine.target)
 
GS.best_params_
GS.best_score_

在我调整完这个参数之后,我发现后面的几个参数对模型的分数没有什么影响了,也可以试着调调看

3.调整min_samples_leaf

改一下参数,其他的不便

param_grid={'min_samples_leaf':np.arange(1, 1+10, 1)}
rfc = RandomForestClassifier(n_estimators=29
                             ,random_state=0
                             ,max_features=1
                            )
GS = GridSearchCV(rfc,param_grid,cv=10)
GS.fit(wine.data,wine.target)
 
GS.best_params_
GS.best_score_

4.调整min_samples_split

param_grid={'min_samples_split':np.arange(2, 2+20, 1)}
rfc = RandomForestClassifier(n_estimators=29
                             ,random_state=0
                             ,max_features=1
                            )
GS = GridSearchCV(rfc,param_grid,cv=10)
GS.fit(wine.data,wine.target)
 
GS.best_params_
GS.best_score_

5.调整min_samples_split

#调整Criterion
 
param_grid = {'criterion':['gini', 'entropy']}
 
rfc = RandomForestClassifier(n_estimators=29
                             ,random_state=0
                            )
GS = GridSearchCV(rfc,param_grid,cv=10)
GS.fit(data.data,data.target)
 
GS.best_params_
GS.best_score_
  • 五、总结
模型的最优参数是当map_depth=13,max_features=1时,但根据泛化误差,map_depth为13应该在曲线的左边,要调最优应该要往右边调,但是max_features=1明显是往曲线的左边调,这好像有一丢丢的矛盾。所以这是因为红酒数据集的原因么? image

相关文章

网友评论

    本文标题:实验二:使用随机森林算法对红酒数据集进行分类建模过程

    本文链接:https://www.haomeiwen.com/subject/qnajpktx.html