Explain implementation of decision tree regression in Python

707    Asked by SnehaPandey in Data Science , Asked on Nov 9, 2019
Answered by Sneha Pandey

First we import the data

# Importing the libraries

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

# Importing the dataset

dataset = pd.read_csv('Position_Salaries.csv')

X = dataset.iloc[:, 1:2].values

y = dataset.iloc[:, 2].values

Then we split the data

# Splitting the dataset into the Training set and Test set

from sklearn.cross_validation import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

Now we fit and predict the data

#Fitting Decision Tree Regression to the dataset

from sklearn.tree import DecisionTreeRegressor

regressor = DecisionTreeRegressor(random_state = 0)

regressor.fit(X, y)

#Predicting a new result

y_pred = regressor.predict(6.5)



Your Answer

Interviews

Parent Categories