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