原因:为什么需要用到这样一个参数random_state(随机状态)?
在此先简单罗列三种情况:
1、在构建模型时:
forest = RandomForestClassifier(n_estimators=100, random_state=0)
forest.fit(X_train, y_train)
2、在生成数据集时:
X, y = make_moons(n_samples=100, noise=0.25, random_state=3)
3、在拆分数据集为训练集、测试集时:
X_train, X_test, y_train, y_test = train_test_split(
cancer.data, cancer.target, stratify=cancer.target, random_state=42)
如果不设置random_state的话会怎样?
例如1中,每次构建的模型是不同的。
例如2中,每次生成的数据集是不同的。
例如3中,每次拆分出的训练集、测试集是不同的。
之所以会这样,是因为模型的构建、数据集的生成、数据集的拆分都是一个随机的过程。
作用:控制随机状态。
random_state 它按一定的规则去取出我们的数据
原文链接:https://blog.csdn.net/ytomc/article/details/113437926
网友评论