Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sample randomforest hyperparameter tuning

# Define the dictionary 'params_rf'
params_rf = {
             'n_estimators': [100, 350, 500],
             'max_features': ['log2', 'auto', 'sqrt'],
             'min_samples_leaf': [2, 10, 30], 
             }
# Import GridSearchCV
from sklearn.model_selection import GridSearchCV

# Instantiate grid_rf
grid_rf = GridSearchCV(estimator=...base_randomforest_model...,
                       param_grid=params_rf,
                       scoring='neg_mean_squared_error',
                       cv=3,
                       verbose=1,
                       n_jobs=-1)
                       
#Train the model
grid_rf.fit(X_train, y_train)

# Import mean_squared_error from sklearn.metrics as MSE 
from sklearn.metrics import mean_squared_error as MSE

# Extract the best estimator
best_model = grid_rf.best_estimator_

# Predict test set labels
y_pred = best_model.predict(X_test)

# Compute rmse_test
rmse_test = MSE(y_test, y_pred)**(1/2)

# Print rmse_test
print('Test RMSE of best model: {:.3f}'.format(rmse_test)) 
Comment

PREVIOUS NEXT
Code Example
Python :: autopy in python install 
Python :: how to stop python prompt 
Python :: python write txt utf8 
Python :: check python version conda env 
Python :: django RetrieveUpdateDestroyAPIView 
Python :: plot distribution seaborn 
Python :: pandas get date from datetime 
Python :: compute mad python 
Python :: remove empty rows csv python 
Python :: pandas transform date format? 
Python :: python way to unindent blocks of code 
Python :: Local to ISO 8601 with TimeZone information (Python 3): 
Python :: show battery of my laptop python 
Python :: python datetime add one week 
Python :: python difference between consecutive element in list 
Python :: urlsplit python 
Python :: pandas datetime.time 
Python :: install python 3.9 centos8 
Python :: python filter list of dictionaries by value 
Python :: default ordering django 
Python :: python requests with login 
Python :: python lexicographical comparison 
Python :: python print user input 
Python :: smtp email template 
Python :: convert a tuple into string python 
Python :: python font family list 
Python :: screen size python 
Python :: clear cookies selenium python 
Python :: register temporary table pyspark 
Python :: convert pandas column type 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =