无法在sklearn随机林的数据块中再现结果



我正在databricks中运行一些机器学习实验。对于随机森林算法,当我重新启动集群时,每次训练输出都会发生变化,即使设置了随机状态。有人知道这个问题吗?

注意:我在本地机器的anaconda环境中用相同的代码尝试了相同的算法,即使我重新启动机器,结果也没有什么不同。

clf_rf =  RandomForestClassifier(n_estimators=10 , random_state=123)
clf_rf.fit(X_train,y_train)
y_pred = clf_rf.predict(X_test)
tn, fp, fn, tp = confusion_matrix(y_test,y_pred).ravel()
accuracy = metrics.accuracy_score(y_test, y_pred)
precision = metrics.precision_score(y_test, y_pred)
recall =  metrics.recall_score(y_test, y_pred)
f1_score = metrics.f1_score(y_test, y_pred)
print(f"TP:{tp}")
print(f"FP:{fp}")
print(f"TN:{tn}")
print(f"FN:{fn}")
print(f"Accuracy : {accuracy}")
print(f"Precision : {precision}")
print(f"Recall : {recall}")
print(f"F1 Score : {f1_score}")

每次重新启动集群时,此代码的输出都会发生变化。

试试这个:

from numpy.random import seed
seed(1)
clf_rf =  RandomForestClassifier(n_estimators=10 , random_state=123)
clf_rf.fit(X_train,y_train)
y_pred = clf_rf.predict(X_test)
tn, fp, fn, tp = confusion_matrix(y_test,y_pred).ravel()
accuracy = metrics.accuracy_score(y_test, y_pred)
precision = metrics.precision_score(y_test, y_pred)
recall =  metrics.recall_score(y_test, y_pred)
f1_score = metrics.f1_score(y_test, y_pred)
print(f"TP:{tp}")
print(f"FP:{fp}")
print(f"TN:{tn}")
print(f"FN:{fn}")
print(f"Accuracy : {accuracy}")
print(f"Precision : {precision}")
print(f"Recall : {recall}")
print(f"F1 Score : {f1_score}")

在进行列车测试拆分时,随机性可能会进入您的工作流程。如果你在train_testrongplit中设置random_state,我想你会没事的。

示例展示了修复数据集中的随机性可以产生可重复的结果。

from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from sklearn.metrics import confusion_matrix
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=12)
clf_rf =  RandomForestClassifier(n_estimators=10 , random_state=123)
clf_rf.fit(X_train,y_train)
y_pred = clf_rf.predict(X_test)
tn, fp, fn, tp = confusion_matrix(y_test,y_pred).ravel()
accuracy = metrics.accuracy_score(y_test, y_pred)
precision = metrics.precision_score(y_test, y_pred)
recall =  metrics.recall_score(y_test, y_pred)
f1_score = metrics.f1_score(y_test, y_pred)
print(f"TP:{tp}")
print(f"FP:{fp}")
print(f"TN:{tn}")
print(f"FN:{fn}")
print(f"Accuracy : {accuracy}")
print(f"Precision : {precision}")
print(f"Recall : {recall}")
print(f"F1 Score : {f1_score}")

输出:

TP:9
FP:1
TN:12
FN:3
Accuracy : 0.84
Precision : 0.9
Recall : 0.75
F1 Score : 0.8181818181818182

相关内容

  • 没有找到相关文章

最新更新