from sklearn.ensemble import RandomForestClassifier # Creating a random forest classifier with 100 trees clf = RandomForestClassifier(n_estimators=100, random_state=42) # Fitting the model to the data clf.fit(X_train, y_train) # Predicting the classes of the test data y_pred = clf.predict(X_test)
# Getting the feature importance scores importance = clf.feature_importances_ # Sorting the features by importance indices = np.argsort(importance)[::-1] # Printing the feature ranking print("Feature ranking:") for f in range(X_train.shape[1]): print("%d. feature %d (%f)" % (f + 1, indices[f], importance[indices[f]]))
from sklearn.model_selection import GridSearchCV # Defining the parameter grid param_grid = { "n_estimators": [100, 200, 300], "max_depth": [None, 5, 10] } # Creating a grid search object grid_search = GridSearchCV(RandomForestClassifier(random_state=42), param_grid, cv=5) # Fitting the grid search object to the data grid_search.fit(X_train, y_train) # Printing the best hyperparameters print("Best hyperparameters: ", grid_search.best_params_)These examples use the scikit-learn package library (sklearn).