Title: Random Forest Classifier
Slug: random_forest_classifier
Summary: Training a random forest classifier in scikit-learn.
Date: 2017-09-21 12:00
Category: Machine Learning
Tags: Trees And Forests
Authors: Chris Albon

Preliminaries


In [1]:
# Load libraries
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets

Load Iris Data


In [2]:
# Load data
iris = datasets.load_iris()
X = iris.data
y = iris.target

Create Random Forest Classifier


In [3]:
# Create random forest classifer object that uses entropy
clf = RandomForestClassifier(criterion='entropy', random_state=0, n_jobs=-1)

Train Random Forest Classifier


In [4]:
# Train model
model = clf.fit(X, y)

Predict Previously Unseen Observation


In [5]:
# Make new observation
observation = [[ 5,  4,  3,  2]]
              
# Predict observation's class    
model.predict(observation)


Out[5]:
array([1])