如何知道Python中已有的随机森林回归模型的决策树的最大深度,min_samples_leaf?

问题描述 投票:0回答:1

我正在使用随机森林回归模型进行预测,并且正在解决与准确性和 MAPE 相关的问题,它们给了我奇怪的值,但是 MSE 和 RMSE 仍然给了我明显不错的结果。我尝试过以下代码:

rf_datas_temp = rf_datas.copy()

rf_datas_temp1 = rf_datas_temp.sample(frac=1, replace=False).reset_index(drop=True)
x = rf_datas_temp1[RFR_Col_2 ]   
y = rf_datas_temp1['ΔR'].values  # Target

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)

# # train RF

x_train = x_train.fillna(0).reset_index(drop=True)
x_test = x_test.fillna(0).reset_index(drop=True)

y_train[np.isnan(y_train)] = 0
y_test[np.isnan(y_test)] = 0 

rf = RandomForestRegressor(n_estimators=200, random_state=42)
rf.fit(x_train, y_train)

# # Predict with RF and evaluate

prediction = rf.predict(x_test)
mse = mean_squared_error(y_test, prediction)
rmse = mse**.5
abs_diff = np.array(np.abs((y_test - prediction)/y_test))
abs_diff = abs_diff[~np.isinf(abs_diff)]

mape = np.nanmean(abs_diff)*100
accuracy = 100 - mape

print(mse,rmse,mape, accuracy)
print("Training Accuracy = ", rf.score(x_train, y_train))
print("Test Accuracy = ", rf.score(x_test, y_test))

我得到了以下结果

MSE = 0.016, RMSE = 0.1280 , MAPE = 183 accuracy = -83.26

Training Accuracy = 0.90, Test Accuracy = 0.195

所以我想知道是否有一种方法可以知道“最大树深度”、“min_samples_leaf”等参数的值,以便调整它们以获得更好的结果。

另外,根据之前的结果,我想知道我的模型是否过度拟合。

python machine-learning regression random-forest prediction
1个回答
0
投票
  1. 要检索 scikit-learn 中已训练的
    RandomForestRegressor
    的所有超参数,您可以使用
    get_params
    方法。这将为您提供一个包含所有超参数及其各自值的字典。
from sklearn.ensemble import RandomForestRegressor

rf = RandomForestRegressor()  # For demonstration purposes. Substitute with your trained model.
rf.fit(X_train, y_train)      # For demonstration purposes.

# Fetch all parameters
params = rf.get_params()

# Output each parameter and its corresponding value
for key, value in params.items():
    print(f"{key}: {value}")
  1. 没有办法知道参数的值。您根据您的经验给出数字。有几种方法可以找到最佳参数。其中一些是 GridSearchCV 和 RandomizedSearchCV。但是在这些技术中,将调用的参数是根据秘密经验塑造的

  2. 我猜,你处理了回归问题,那么准确度是多少?但训练准确率(0.90)远高于测试准确率(0.195),这表明潜在的过拟合。

我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.