How to Train a Decision Tree Regressor with Sklearn

02.22.2021

Intro

In a previous article, we learned how to create a Decision Tree for classification. The allow us to predict which cateogory an observation belongs to. In this article, we will learn how to build a tree classifier for regression, using sklearn, which allows us to predict coutinous values (like the cost of a house).

Creating a Tree Classifier

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

# Load libraries
from sklearn.tree import DecisionTreeRegressor
from sklearn import datasets

boston = datasets.load_boston()
features = boston.data
target = boston.target

decisiontree = DecisionTreeRegressor()

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