Roo*_*123 5 python machine-learning random-forest scikit-learn grid-search
我试图在随机森林回归器的帮助下解决波士顿数据集上的回归问题。我正在使用GridSearchCV来选择最佳超参数。
问题一
我应该适合GridSearchCV
一些X_train, y_train
,然后获得最佳参数。
或者
我应该适合它X, y
以获得最佳参数。(X,y =整个数据集)
问题二
说如果我适合它X, y
并获得最佳参数,然后在这些最佳参数上建立一个新模型。现在我应该如何训练这个新模型?
我应该在X_train, y_train
或上训练新模型X, y.
问题三
如果我训练新模型,X,y
那么我将如何验证结果?
到目前为止我的代码
#Dataframes
feature_cols = ['CRIM','ZN','INDUS','NOX','RM','AGE','DIS','TAX','PTRATIO','B','LSTAT']
X = boston_data[feature_cols]
y = boston_data['PRICE']
Run Code Online (Sandbox Code Playgroud)
训练测试数据拆分
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 1)
Run Code Online (Sandbox Code Playgroud)
网格搜索以获得最佳超参数
from sklearn.grid_search import GridSearchCV
param_grid = {
'n_estimators': [100, 500, 1000, 1500],
'max_depth' : [4,5,6,7,8,9,10]
}
CV_rfc = GridSearchCV(estimator=RFReg, param_grid=param_grid, cv= 10)
CV_rfc.fit(X_train, y_train)
CV_rfc.best_params_
#{'max_depth': 10, 'n_estimators': 100}
Run Code Online (Sandbox Code Playgroud)
在 max_depth 上训练模型:10,n_estimators:100
RFReg = RandomForestRegressor(max_depth = 10, n_estimators = 100, random_state = 1)
RFReg.fit(X_train, y_train)
y_pred = RFReg.predict(X_test)
y_pred_train = RFReg.predict(X_train)
Run Code Online (Sandbox Code Playgroud)
均方根误差: 2.8139766730629394
我只是想要一些关于正确步骤的指导
一般来说,要调整超参数,您应该始终在 上训练模型X_train
,并使用X_test
来检查结果。您必须根据获得的结果调整参数X_test
。
您永远不应该在整个数据集上调整超参数,因为这会破坏测试/训练分割的目的(正如您在问题3中正确提出的那样)。
归档时间: |
|
查看次数: |
3001 次 |
最近记录: |