我是第一次通过一个随机森林模型,并遇到了一个问题,我的精确量化。
目前,我分割数据集(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%
我确实是在学习,但我已经接受了一些正式的训练。对于建模的过程,我真的很兴奋,但是我想弄清楚我在继续学习这个过程时犯了哪些错误。
发布于 2021-04-15 23:31:54
你快到了!score()
方法接受X_test
和y_test
,这是score()
背后的逻辑
# 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的分数。文档你现在知道为什么你得到100%
了吗
如果您想获得其他度量,则需要计算预测,并使用回归度量-> https://scikit-learn.org/stable/modules/classes.html#regression-metrics。
您也可以使用AutoML进行学习(您自己不是一个模型)。您可以运行AutoML来创建基线模型。AutoML将为您计算许多指标。然后,您可以编写自己的脚本并比较结果。
https://stackoverflow.com/questions/67116078
复制