In [ ]:
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np

In [ ]:
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)

In [ ]:
df['is_train'] = np.random.uniform(0, 1, len(df)) <= .75

In [ ]:
df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)

In [ ]:
df.head()

In [ ]:
train, test = df[df['is_train']==True], df[df['is_train']==False]

In [ ]:
features = df.columns[:4]
clf = RandomForestClassifier(n_jobs=2)
y, _ = pd.factorize(train['species'])
clf.fit(train[features], y)

In [ ]:
preds = iris.target_names[clf.predict(test[features])]
pd.crosstab(test['species'], preds, rownames=['actual'], colnames=['preds'])

In [ ]: