-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecisiontree.py
More file actions
52 lines (37 loc) · 1.21 KB
/
decisiontree.py
File metadata and controls
52 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""DecisionTree.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1kyQF8I-NpsW7lTBD9dfhxHZmeYEPTjJX
## Importing Libraries
"""
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:-1].values
y = dataset.iloc[:, -1].values
print(X)
print(y)
"""## Training Decision Tree Regression model on the whole dataset"""
from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor(random_state = 0)
regressor.fit(X, y)
"""## Predicting New Result"""
regressor.predict([[6.5]])
"""## Visualizing the descision tree regression results"""
plt.scatter(X, y, color ='red')
plt.plot(X, regressor.predict(X), color = 'blue')
plt.title('Truth or Bluff (Decision Tree Regression)')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()
"""## High 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 ="blue")
plt.plot(X_grid, regressor.predict(X_grid), color ='red')
plt.title('Truth or Bluff (Decision Tree Regression)')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()