Python: 3.6
窗户: 10
关于兰登森林和眼前的问题,我几乎没有什么问题:
我使用Gridsearch运行回归问题使用随机森林。我想绘制对应于网格搜索发现的最佳匹配参数的树。这是密码。
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=55)
# Use the random grid to search for best hyperparameters
# First create the base model to tune
rf = RandomForestRegressor()
# Random search of parameters, using 3 fold cross validation,
# search across 100 different combinations, and use all available cores
rf_random = RandomizedSearchCV(estimator = rf, param_distributions = random_grid, n_iter = 50, cv = 5, verbose=2, random_state=56, n_jobs = -1)
# Fit the random search model
rf_random.fit(X_train, y_train)
rf_random.best_params_最好的参数是:
{'n_estimators': 1000,
'min_samples_split': 5,
'min_samples_leaf': 1,
'max_features': 'auto',
'max_depth': 5,
'bootstrap': True}y位于范围0,1中,所有预测变量都是二进制变量或范畴变量。在输入输出特征空间中,哪种算法一般都能很好地工作。我试过兰登森林。(没有给出好的结果)。注y变量是一种比值,因此它介于0和1. Example: Expense on food/Total Expense之间。y变量在60%的数据中有value=1,在其余的数据中有0到1之间。比如0.66, 0.87等等。{0,1}和分类变量{A,B,C}。是否需要将其转换为one-hot encoding变量以使用随机林?发布于 2020-06-03 17:02:58
关于情节(恐怕你的其他问题太宽泛了,一般的想法是避免同时问多个问题):
适合您的RandomizedSearchCV导致了一个rf_random.best_estimator_,它本身就是一个随机森林,其参数显示在您的问题中(包括'n_estimators': 1000)。
根据文档,拟合的RandomForestRegressor包含一个属性:
estimators_ : list of DecisionTreeRegressor 拟合次估计器的集合。
因此,要绘制任意一棵随机森林的树,您应该使用
from sklearn import tree
tree.plot_tree(rf_random.best_estimator_.estimators_[k])或
from sklearn import tree
tree.export_graphviz(rf_random.best_estimator_.estimators_[k])对于所需的k (在您的情况下为[0, 999] )(一般情况下为[0, n_estimators-1])。
发布于 2020-05-31 08:39:57
请允许我在回答你的问题之前退后一步。
理想情况下,人们应该进一步深入研究best_params_ of RandomizedSearchCV输出,通过GridSearchCV。RandomizedSearchCV将检查您的参数,而不尝试所有可能的选项。然后,一旦您有了best_params_ of RandomizedSearchCV,我们就可以在更窄的范围内研究所有可能的选项。
您没有在代码输入中包括random_grid参数,但我希望您执行这样的GridSearchCV:
# Create the parameter grid based on the results of RandomizedSearchCV
param_grid = {
'max_depth': [4, 5, 6],
'min_samples_leaf': [1, 2],
'min_samples_split': [4, 5, 6],
'n_estimators': [990, 1000, 1010]
}
# Fit the grid search model
grid_search = GridSearchCV(estimator = rf, param_grid = param_grid,
cv = 5, n_jobs = -1, verbose = 2, random_state=56)上面所做的就是遍历param_grid中所有可能的参数组合,并给出最佳的参数。
现在来问你们的问题:
https://stackoverflow.com/questions/62111883
复制相似问题