How to Train a Decision Tree Classifier with Sklearn

02.19.2021

Intro

Trees are one of the most powerful machine learning models you can use. They break down functions into break points and decision trees that can be interpreted much easier than deep learning models. They also have great performance. In this article, we will learn how to build a Tree Classifier in Sklearn.

Creating a Tree Classifier

To create a tree model, we use the DecisionTreeClassifier class. We use this similar to any other model; we create an instance, then pass our x and y data to the fit method.

from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets

iris = datasets.load_iris()
features = iris.data
target = iris.target

decisiontree = DecisionTreeClassifier()

model = decisiontree.fit(features, target)
model.fit()