typestar

Random forests in Python

An ensemble of trees, and the feature importances that come with it.

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=0)

forest = RandomForestClassifier(
    n_estimators=200, max_depth=4, random_state=0)
forest.fit(X_train, y_train)

print(f"{forest.score(X_test, y_test):.3f}")
for name, weight in zip(load_iris().feature_names,
                        forest.feature_importances_):
    print(f"{name:22} {weight:.3f}")

How it works

  1. n_estimators is how many trees vote.
  2. feature_importances_ ranks the columns the forest relied on.
  3. Trees need no scaling, which makes them a good first model.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
538
Tokens
126
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 69 seconds.

Type this snippet

Step 1 of 4 in Models, step 12 of 25 in Machine learning with scikit-learn.

← Previous Next →