<jupyter_start><jupyter_text>Decision Tree RegressionDecision tree regression observes features of an object and trains a model in the structure of a tree to predict data in the future to produce meaningful continuous output. Continuous output means that the output/result is not discrete, i.e., it is not represented just by a discrete, known set of numbers or values.1.     Discrete output example: A weather prediction model that predicts whether or not there’ll be rain on a particular day. 2.     Continuous output example: A profit prediction model that states the probable profit that can be generated from the sale of a product.Here, continuous values are predicted with the help of a decision tree regression model. Step 1: Import the required libraries.<jupyter_code># import numpy package for arrays and stuff
import numpy as np
# import matplotlib.pyplot for plotting our result
import matplotlib.pyplot as plt
# import pandas for importing csv files
import pandas as pd<jupyter_output><empty_output><jupyter_text>Step 2: Initialize and print the Dataset.<jupyter_code># import dataset
# dataset = pd.read_csv('Data.csv')
# alternatively open up .csv file to read data

dataset = np.array(
[['Asset Flip', 100, 1000],
['Text Based', 500, 3000],
['Visual Novel', 1500, 5000],
['2D Pixel Art', 3500, 8000],
['2D Vector Art', 5000, 6500],
['Strategy', 6000, 7000],
['First Person Shooter', 8000, 15000],
['Simulator', 9500, 20000],
['Racing', 12000, 21000],
['RPG', 14000, 25000],
['Sandbox', 15500, 27000],
['Open-World', 16500, 30000],
['MMOFPS', 25000, 52000],
['MMORPG', 30000, 80000]
])

# print the dataset
print(dataset)<jupyter_output>[['Asset Flip' '100' '1000']
 ['Text Based' '500' '3000']
 ['Visual Novel' '1500' '5000']
 ['2D Pixel Art' '3500' '8000']
 ['2D Vector Art' '5000' '6500']
 ['Strategy' '6000' '7000']
 ['First Person Shooter' '8000' '15000']
 ['Simulator' '9500' '20000']
 ['Racing' '12000' '21000']
 ['RPG' '14000' '25000']
 ['Sandbox' '15500' '27000']
 ['Open-World' '16500' '30000']
 ['MMOFPS' '25000' '52000']
 ['MMORPG' '30000' '80000']]<jupyter_text>Step 3: Select all the rows and column 1 from the dataset to “X”.<jupyter_code># select all rows by : and column 1
# by 1:2 representing features
X = dataset[:, 1:2].astype(int)

# print X
print(X)<jupyter_output>[[  100]
 [  500]
 [ 1500]
 [ 3500]
 [ 5000]
 [ 6000]
 [ 8000]
 [ 9500]
 [12000]
 [14000]
 [15500]
 [16500]
 [25000]
 [30000]]<jupyter_text>Step 4: Select all of the rows and column 2 from the dataset to “y”.<jupyter_code># select all rows by : and column 2
# by 2 to Y representing labels
y = dataset[:, 2].astype(int)

# print y
print(y)<jupyter_output>[ 1000  3000  5000  8000  6500  7000 15000 20000 21000 25000 27000 30000
 52000 80000]<jupyter_text>Step 5: Fit decision tree regressor to the dataset<jupyter_code># import the regressor
from sklearn.tree import DecisionTreeRegressor

# create a regressor object
regressor = DecisionTreeRegressor(random_state = 0)

# fit the regressor with X and Y data
regressor.fit(X, y)<jupyter_output><empty_output><jupyter_text>Step 6: Predicting a new value<jupyter_code># predicting a new value

# test the output by changing values, like 3750
y_pred = regressor.predict([[3750]])

# print the predicted price
print("Predicted price: % d\n"% y_pred)<jupyter_output>Predicted price:  8000<jupyter_text>Step 7: Visualising the result<jupyter_code># arange for creating a range of values
# from min value of X to max value of X
# with a difference of 0.01 between two
# consecutive values
X_grid = np.arange(min(X), max(X), 0.01)

# reshape for reshaping the data into
# a len(X_grid)*1 array, i.e. to make
# a column out of the X_grid values
X_grid = X_grid.reshape((len(X_grid), 1))

# scatter plot for original data
plt.scatter(X, y, color = 'red')

# plot predicted data
plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')

# specify title
plt.title('Profit to Production Cost (Decision Tree Regression)')

# specify X axis label
plt.xlabel('Production Cost')

# specify Y axis label
plt.ylabel('Profit')

# show the plot
plt.show()<jupyter_output><empty_output><jupyter_text>Step 8: The tree is finally exported and shown in the TREE STRUCTURE below, visualized using http://www.webgraphviz.com/ by copying the data from the ‘tree.dot’ file.<jupyter_code># import export_graphviz
from sklearn.tree import export_graphviz
from graphviz import Digraph

# export the decision tree to a tree.dot file
# for visualizing the plot easily anywhere
export_graphviz(regressor, out_file ='tree.dot',
			feature_names =['Production Cost'])<jupyter_output><empty_output><jupyter_text>Step 9 : Converting the tree.dot to tree.png<jupyter_code>! dot -Tpng tree.dot -o tree.png<jupyter_output><empty_output><jupyter_text>Step 10 : Viewing the tree.png file to Visualize the constructed tree<jupyter_code>import matplotlib.pyplot as plt
import cv2
%matplotlib inline
img = cv2.imread('tree.png')
plt.figure(figsize = (20, 20))
plt.imshow(img)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11507778220981252, 'fraction_numerical': 0.08496210610291184, 'mean_word_length': 5.380407124681934, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '550000', 'license': 'MIT', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1669, 'n_words': 608, 'path': 'Decision_Tree_Regression.ipynb', 'repo_name': 'Benjamindavid03/MachineLearningLab', 'size': '5014'}