您需要将数据拆分为测试和训练 (20:80)(例如 sklearn 中的 test_train_split),然后使用训练数据运行模型并检查准确性。如果它不是您所期望的,那么您可以尝试应用超参数调整。
您可以通过 GridSearchCV 执行此操作,您需要在其中拟合所需的估计器(取决于问题的类型)和参数值。
附上示例代码:
from sklearn.model_selection import GridSearchCV
# Create the parameter grid based on the results of random search
param_grid = {
'bootstrap': [True],
'max_depth': [50, 55, 60, 65],
'max_features': ["auto","sqrt", 2, 3],
'min_samples_leaf': [1, 2, 3],
'min_samples_split': [2, 3, 4],
'n_estimators': [60, 65, 70, 75]
}
grid_search = GridSearchCV(estimator = rfcv, param_grid = param_grid, cv = 3, n_jobs = -1, verbose = 2)
grid_search.fit(X_train, Y_train)
grid_search.best_params_
基于最佳参数结果,您可以微调网格搜索。
例如,如果 n_estimators 的最佳参数值接近 60,那么您需要将值更改为 60,如 [50,55,60,60]。找出确切的值。
然后根据最佳参数值构建机器学习模型。评估训练数据的准确性,然后使用测试数据值预测结果。
rf = rgf(n_estimators = 70, random_state=0, min_samples_split = 2, min_samples_leaf=1, max_features = 'sqrt',bootstrap='True', max_depth=65)
regressor = rf.fit(X_train,Y_train)
pred_tuned = regressor.predict(X_test)
你会发现你的准确性有所提高!!