Decision tree algorithm creates a tree like conditional control statements to create its model hence it is named as decision tree.

Decision tree machine learning algorithm can be used to solve both regression and classification problem.

In this post we will be implementing a simple decision tree regression model using python and sklearn.

First thing first , let us import the required libraries.

import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import r2_score,mean_squared_error

After that we need to load data in jupyter notebook. You can find the data here.

Note that the above data has a feature called x and a label called y. We have to use values of x to predict y. The next step would be to split data into train and test as below.

x = df.x.values.reshape(-1, 1)
y = df.y.values.reshape(-1, 1)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.30, random_state=42)