原因:為什么需要用到這樣一個參數(shù)random_state(隨機狀態(tài))?
在此先簡單羅列三種情況:
1、在構(gòu)建模型時:
forest = RandomForestClassifier(n_estimators=100, random_state=0)
forest.fit(X_train, y_train)
2、在生成數(shù)據(jù)集時:
X, y = make_moons(n_samples=100, noise=0.25, random_state=3)
3、在拆分數(shù)據(jù)集為訓練集、測試集時:
X_train, X_test, y_train, y_test = train_test_split(
cancer.data, cancer.target, stratify=cancer.target, random_state=42)
如果不設(shè)置random_state的話會怎樣?
例如1中,每次構(gòu)建的模型是不同的。
例如2中,每次生成的數(shù)據(jù)集是不同的。
例如3中,每次拆分出的訓練集、測試集是不同的。
之所以會這樣,是因為模型的構(gòu)建、數(shù)據(jù)集的生成、數(shù)據(jù)集的拆分都是一個隨機的過程。
作用:控制隨機狀態(tài)。
random_state 它按一定的規(guī)則去取出我們的數(shù)據(jù)
原文鏈接:https://blog.csdn.net/ytomc/article/details/113437926