This tutorial demonstrates the implementation of Random ForestTM Classifier from Scikit Learn library.

###Importing Libraries
import pandas as pd
from sklearn.model_selection import train_test_split as tts
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn import datasets
from sklearn import metrics

###Importing Dataset
iris = datasets.load_iris()
# print(iris.data[:,3])

###Constructing Data Frame
data = pd.DataFrame({"sl":iris.data[:,0], "sw":iris.data[:,1], "pl":iris.data[:,2], "pw":iris.data[:,3], 'species': iris.target})
# print(data["species"])

###Splitting train/test data
x=data[['sl','sw','pl','pw']]
y=data["species"]
X_tr, X_ts, y_tr, y_ts = tts(X,y, test_size=30/100)

###Creating Random Forest Classifier Model
GC = RFC(n_estimators=100)

###Training the Model
GC.fit(X_tr,y_tr)

###Making Predictions
y_pr=GC.predict(X_ts)
print(y_pr)

###Evaluating Prediction Accuracy
print("Acc %:",metrics.accuracy_score(y_ts, y_pr)*100)

###Making Prediction with Foreign Data
print(GC.predict([[4,5,6,6]]))

This tutorial aims to provide a simple, clear and reusable Random ForestTM implementation example, so that, seasoned visitors can just take a look at it, comprehend what’s going on and copy/reproduce it for their own use cases without losing too much time.

If you’d like to see a step by step explanation of random forest algorithm you can check out this tutorial.

You can also see its history, read about random forest optimization parameters and find more examples in the main Random ForestTM page here.