# Decision Tree Regression

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv(r'C:\Users\hp\Desktop\MLDL\Machine Learning A-Z (Model Selection)\Regression\Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

#Spliting The dataset
from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest = train_test_split(X,y,test_size = 0.2,random_state = 0)


# Training the Decision Tree Regression model on the whole dataset
from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor(random_state = 0)
regressor.fit(xtrain, ytrain)


# Predicting a new result
ypred = regressor.predict(xtest)

#Metrics
from sklearn.metrics import r2_score
score = r2_score(ytest,ypred)
print(score)


# Visualising the Decision Tree Regression results (higher resolution)
# X_grid = np.arange(min(X), max(X), 0.01)
# X_grid = X_grid.reshape((len(X_grid), 1))
# plt.scatter(X, y, color = 'red')
# plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
# plt.title('Truth or Bluff (Decision Tree Regression)')
# plt.xlabel('Position level')
# plt.ylabel('Salary')
# plt.show()