Random forest

Create random forest from scratch in Python
ai
Published

September 20, 2021

from sklearn.datasets import load_breast_cancer
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import f1_score
import random
raw = load_breast_cancer(return_X_y=True)

X = pd.DataFrame(raw[0])
y = pd.DataFrame(raw[1])
features = X.columns
n_features = len(features)
n_features_to_consider = int(round(np.sqrt(n_features)))
features, n_features_to_consider
(RangeIndex(start=0, stop=30, step=1), 5)
trees = []

for i in range(10): 
    feature_subset = random.sample(features.values.tolist(), k=n_features_to_consider)
    tree = DecisionTreeClassifier(max_depth=2)
    sampling_index = X.sample(frac=1).index # RANDOMly select data to train on 
    tree.fit(X.loc[sampling_index, feature_subset], y.loc[sampling_index]) # RANDOMly select features to train on
    trees.append((tree, feature_subset))
rf_predictions = np.mean([tree.predict(X.loc[:, features]) for tree, features in trees], axis=0)
# The precision of a single tree
for i in range(5):
    one_tree_predictions = trees[i][0].predict(X.loc[:, trees[i][1]])
    print(f1_score(y, one_tree_predictions) )
0.9195088676671215
0.9346879535558781
0.9439124487004104
0.9482517482517482
0.9410187667560322
# The precision of the forest
f1_score(y, (rf_predictions > 0.5) * 1)
0.9665738161559889