Jovian
⭐️
Sign In

Classification with Random Forests

In [1]:
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from utils import load_data, draw_tree
import seaborn as sns
sns.set_style("darkgrid", {"figure.facecolor": '#cccccc00'})

We begin by importing the data. For this example, we'll use three columns: "yaw", "pitch" and "roll".

In [2]:
cols = ['yaw', 'pitch', 'roll']
X, y = load_data(cols)
In [3]:
sns.scatterplot(data=X);
Notebook Image

Training a random forest is as simple as instantiating a class and invoking the fit function.

In [4]:
c = RandomForestClassifier()
c.fit(X, y);
c.feature_importances_
Out[4]:
array([0.55562498, 0.2670924 , 0.17728262])

Visual inspection of the tree lets is possible using graphviz.

In [5]:
draw_tree(c.estimators_[0], cols)
Out[5]:
Notebook Image
In [6]:
import jovian
In [ ]:
jovian.commit()
[jovian] Saving notebook..
In [ ]: