Title: Decision Tree Classifier
Slug: decision_tree_classifier
Summary: Training a decision tree classifier in scikit-learn.
Date: 2017-09-19 12:00
Category: Machine Learning
Tags: Trees And Forests
Authors: Chris Albon
In [1]:
# Load libraries
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
In [2]:
# Load data
iris = datasets.load_iris()
X = iris.data
y = iris.target
In [3]:
# Create decision tree classifer object using gini
clf = DecisionTreeClassifier(criterion='gini', random_state=0)
In [4]:
# Train model
model = clf.fit(X, y)
In [5]:
# Make new observation
observation = [[ 5, 4, 3, 2]]
In [6]:
# Predict observation's class
model.predict(observation)
Out[6]:
In [7]:
# View predicted class probabilities for the three classes
model.predict_proba(observation)
Out[7]: