Skip to content

Instantly share code, notes, and snippets.

@formatkaka
Forked from glamp/rf_iris.py
Created August 25, 2016 03:56
Show Gist options
  • Save formatkaka/6d7a84b66a25466ab0a5bd647fb8dedf to your computer and use it in GitHub Desktop.
Save formatkaka/6d7a84b66a25466ab0a5bd647fb8dedf to your computer and use it in GitHub Desktop.
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['is_train'] = np.random.uniform(0, 1, len(df)) <= .75
df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
df.head()
train, test = df[df['is_train']==True], df[df['is_train']==False]
features = df.columns[:4]
clf = RandomForestClassifier(n_jobs=2)
y, _ = pd.factorize(train['species'])
clf.fit(train[features], y)
preds = iris.target_names[clf.predict(test[features])]
pd.crosstab(test['species'], preds, rownames=['actual'], colnames=['preds'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment