【发布时间】: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%
我肯定会边走边学,但接受过一些正式培训。真的只是对建模方面感到兴奋,但想弄清楚我在继续学习这个过程时犯了什么错误。
【问题讨论】:
-
如果只求准确度,可以直接用scikit的
accuracy_score()函数得到:scikit-learn.org/stable/modules/generated/…
标签: python model random-forest decision-tree train-test-split