【问题标题】:Random Forest Train Test Split Accuracy随机森林训练测试分割精度
【发布时间】:2021-07-10 22:33:26
【问题描述】:

我是第一次使用随机森林模型,但我的准确度量化遇到了问题。

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

分割训练集

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

创建并拟合模型

# 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%

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

【问题讨论】:

标签: python model random-forest decision-tree train-test-split


【解决方案1】:

你快到了! 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%

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

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

【讨论】:

    猜你喜欢
    • 2019-08-12
    • 2021-01-13
    • 2020-10-31
    • 1970-01-01
    • 2017-01-22
    • 2017-10-13
    • 2018-06-13
    • 2012-10-25
    • 1970-01-01
    相关资源
    最近更新 更多