繁体   English   中英

随机森林训练测试分割精度

[英]Random Forest Train Test Split Accuracy

我第一次通过随机森林 model 工作,并且遇到了我的准确性量化问题。

目前,我拆分数据集(30% 作为测试大小),拟合 model,然后根据我的 model 预测 y 值,并根据测试预测值对 model 进行评分。 但是我目前遇到了 100% 准确率的问题,我想知道这是因为我的 model 设置的参数,还是因为我在此过程中犯了语法错误。

拆分训练集

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30, random_state=1)

创建并安装 model

# Import the model we are using
from sklearn.ensemble import RandomForestRegressor

# Instantiate model with 1000 decision trees
rf = RandomForestRegressor(n_estimators = 1000,
                           random_state = 42,
                           min_samples_split = 10,
                           max_features = "sqrt",
                           bootstrap = True)

# Train the model on training data
rf.fit(X_train, y_train)

预测测试集并计算准确性

y_pred = rf.predict(X_test)

print("Accuracy:", round((rf.score(X_test, y_pred)*100),2), "%")

>> 100.0%

我肯定在学习 go,但接受过一些正式培训。 真的只是对建模方面感到兴奋,但想弄清楚我在继续学习这个过程时犯了什么错误。

你快到了! score()方法接受X_testy_testscore()背后的逻辑:

# simplified logic behind score()

def score(X, y):
  y_predicted = model.predict(X)
  value = compute_metric(y, y_predicted)
  return value

上面的逻辑只是为了展示分数是如何工作的。

要在您的代码中获得分数:

rf.score(X_test, y_test)

您将获得 R^2 分数。 docs你现在知道了,为什么你会得到100%吗?

如果您想获得其他指标,那么您需要计算预测并使用回归指标 -> https://scikit-learn.org/stable/modules/classes.html#regression-metrics

您还可以使用 AutoML 进行学习(您自己不是模型)。 您可以运行 AutoML 来创建基线模型。 AutoML 将为您计算许多指标。 然后您可以编写自己的脚本并比较结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM