v Random Forest Classifier - Machine Learning

Random Forest Classifier

Preliminaries

# Load libraries
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets

Load Iris Data

# Load data
iris = datasets.load_iris()
X = iris.data
y = iris.target

Create Random Forest Classifier

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

Train Random Forest Classifier

# Train model
model = clf.fit(X, y)

Predict Previously Unseen Observation

# Make new observation
observation = [[ 5,  4,  3,  2]]

# Predict observation's class    
model.predict(observation)
array([1])