采用随机森林分类模型(RandomForestClassifier)再次训练上述鸢尾花数据集,具体要求如下:
1、使用pandas库再次读取数据集,得到相应矩阵,并进项相应的数据预处理:包括数据标准化与鸢尾花类别编码等。
2、采用决策树模型训练鸢尾花数据集,测试集取30%,训练集取70%。
3、特征选择标准criterion请选择 “entropy”,随机森林的子树个数“n_estimators”取值为10,在控制台打印出其测试集正确率。请分析该正确率是否比决策树分类模型正确率更高。
4、为了提升模型的泛化能力,请分别使用十折交叉验证,确定随机森林分类模型的参数max_depth(子树的最大深度)与n_estimators(子树个数)的最优取值。max_depth取值范围为1-5,n_estimators的取值范围为1-20。请在控制台输出这两个参数的最优取值。
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import pandas as pd
from sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import cross_val_s