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_
-
-