使用 sklearn 的预测建模管道
predictive modelling pipeline with sklearn
我一直在从 R
逐渐过渡到 Python
以进行一些预测建模。我想知道通过交叉验证进行超参数优化并将经过训练的模型应用于新实例的最佳管道是什么。
下面您将看到我使用随机森林所做的一个简单示例。我想知道这是否可以,您会添加或删除什么?
#import data sets
train_df = pd.read_csv('../input/train.csv')
test_df = pd.read_csv('../input/test.csv')
#get the predictors only
X_train = train_df.drop(["ID", "target"], axis=1)
y_train = np.log1p(train_df["target"].values)
X_test = test_df.drop(["ID"], axis=1)
#grid to do the random search
from sklearn.model_selection import RandomizedSearchCV
n_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]
max_features = ['auto', 'sqrt']
max_depth = [int(x) for x in np.linspace(10, 110, num = 11)]
max_depth.append(None)
min_samples_split = [2, 5, 10]
min_samples_leaf = [1, 2, 4]
bootstrap = [True, False]
# Create the random grid
random_grid = {'n_estimators': n_estimators,
'max_features': max_features,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'bootstrap': bootstrap}
#Create the model to tune
rf = RandomForestRegressor()
rf_random= RandomizedSearchCV(estimator = rf, param_distributions = random_grid, n_iter = 100, cv = 10, verbose=2, random_state=42, n_jobs =10)
#fit the random search model
rf_random.fit(X_train, y_train)
#get the best estimator
best_random = rf_random.best_estimator_
# train again with the best parameters on the whole training data?
best_random.fit(X_train,y_train)
#apply the best predictor to the test set
pred_test_rf = np.expm1(best_random.predict(X_test))
是否.best_estimator_
使用在网格搜索中找到的最佳参数实例化模型?
如果是这样,我是否需要用整个训练数据再次重新训练(就像我上面所做的那样)还是已经重新训练了?
我想知道这种方法是否可行,或者在 python.
中使用 sklearn 执行此操作的最佳实践是什么
1) 是的,它是一个由 best_params_
从 rf_random
发起的估算器
2) 不,它已经在整个数据上训练好了,不需要做 best_random.fit(X_train,y_train)
RandomizedSearchCV 有一个参数 'refit'
,默认为 True
:
refit : boolean, or string default=True
Refit an estimator using the best found parameters on the whole dataset.
3) 你的方法似乎没问题。这是标准方法。其他事情可能取决于各种因素,例如数据类型、数据大小、使用的算法(估计器)、探索可能性的时间等。但这部分最适合 https://stats.stackexchange.com。
我一直在从 R
逐渐过渡到 Python
以进行一些预测建模。我想知道通过交叉验证进行超参数优化并将经过训练的模型应用于新实例的最佳管道是什么。
下面您将看到我使用随机森林所做的一个简单示例。我想知道这是否可以,您会添加或删除什么?
#import data sets
train_df = pd.read_csv('../input/train.csv')
test_df = pd.read_csv('../input/test.csv')
#get the predictors only
X_train = train_df.drop(["ID", "target"], axis=1)
y_train = np.log1p(train_df["target"].values)
X_test = test_df.drop(["ID"], axis=1)
#grid to do the random search
from sklearn.model_selection import RandomizedSearchCV
n_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]
max_features = ['auto', 'sqrt']
max_depth = [int(x) for x in np.linspace(10, 110, num = 11)]
max_depth.append(None)
min_samples_split = [2, 5, 10]
min_samples_leaf = [1, 2, 4]
bootstrap = [True, False]
# Create the random grid
random_grid = {'n_estimators': n_estimators,
'max_features': max_features,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'bootstrap': bootstrap}
#Create the model to tune
rf = RandomForestRegressor()
rf_random= RandomizedSearchCV(estimator = rf, param_distributions = random_grid, n_iter = 100, cv = 10, verbose=2, random_state=42, n_jobs =10)
#fit the random search model
rf_random.fit(X_train, y_train)
#get the best estimator
best_random = rf_random.best_estimator_
# train again with the best parameters on the whole training data?
best_random.fit(X_train,y_train)
#apply the best predictor to the test set
pred_test_rf = np.expm1(best_random.predict(X_test))
是否
.best_estimator_
使用在网格搜索中找到的最佳参数实例化模型?如果是这样,我是否需要用整个训练数据再次重新训练(就像我上面所做的那样)还是已经重新训练了?
我想知道这种方法是否可行,或者在 python.
中使用 sklearn 执行此操作的最佳实践是什么
1) 是的,它是一个由 best_params_
从 rf_random
2) 不,它已经在整个数据上训练好了,不需要做 best_random.fit(X_train,y_train)
RandomizedSearchCV 有一个参数 'refit'
,默认为 True
:
refit : boolean, or string default=True Refit an estimator using the best found parameters on the whole dataset.
3) 你的方法似乎没问题。这是标准方法。其他事情可能取决于各种因素,例如数据类型、数据大小、使用的算法(估计器)、探索可能性的时间等。但这部分最适合 https://stats.stackexchange.com。