machinelearning

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

2020-10-10  本文已影响0人  Conn606

实验要求:

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
wine = load_wine()
rfc = RandomForestClassifier(random_state=0) #实例化
score_f=cross_val_score(rfc,wine.data,wine.target,cv=10).mean() #交叉验证10次的平均分
score_f
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
上一篇 下一篇

猜你喜欢

热点阅读