Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9dbaf94

Browse files
authored
Create Day 25 Decision Tree.md
1 parent 10c9590 commit 9dbaf94

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Code/Day 25 Decision Tree.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Decision Tree Classification
2+
3+
### Importing the libraries
4+
```python
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
import pandas as pd
8+
```
9+
10+
### Importing the dataset
11+
```python
12+
dataset = pd.read_csv('Social_Network_Ads.csv')
13+
X = dataset.iloc[:, [2, 3]].values
14+
y = dataset.iloc[:, 4].values
15+
```
16+
### Splitting the dataset into the Training set and Test set
17+
```python
18+
from sklearn.cross_validation import train_test_split
19+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
20+
```
21+
22+
### Feature Scaling
23+
```python
24+
from sklearn.preprocessing import StandardScaler
25+
sc = StandardScaler()
26+
X_train = sc.fit_transform(X_train)
27+
X_test = sc.transform(X_test)
28+
```
29+
### Fitting Decision Tree Classification to the Training set
30+
```python
31+
from sklearn.tree import DecisionTreeClassifier
32+
classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
33+
classifier.fit(X_train, y_train)
34+
```
35+
### Predicting the Test set results
36+
```python
37+
y_pred = classifier.predict(X_test)
38+
```
39+
### Making the Confusion Matrix
40+
```python
41+
from sklearn.metrics import confusion_matrix
42+
cm = confusion_matrix(y_test, y_pred)
43+
```
44+
### Visualising the Training set results
45+
```python
46+
from matplotlib.colors import ListedColormap
47+
X_set, y_set = X_train, y_train
48+
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
49+
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
50+
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
51+
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
52+
plt.xlim(X1.min(), X1.max())
53+
plt.ylim(X2.min(), X2.max())
54+
for i, j in enumerate(np.unique(y_set)):
55+
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
56+
c = ListedColormap(('red', 'green'))(i), label = j)
57+
plt.title('Decision Tree Classification (Training set)')
58+
plt.xlabel('Age')
59+
plt.ylabel('Estimated Salary')
60+
plt.legend()
61+
plt.show()
62+
```
63+
### Visualising the Test set results
64+
```python
65+
from matplotlib.colors import ListedColormap
66+
X_set, y_set = X_test, y_test
67+
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
68+
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
69+
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
70+
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
71+
plt.xlim(X1.min(), X1.max())
72+
plt.ylim(X2.min(), X2.max())
73+
for i, j in enumerate(np.unique(y_set)):
74+
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
75+
c = ListedColormap(('red', 'green'))(i), label = j)
76+
plt.title('Decision Tree Classification (Test set)')
77+
plt.xlabel('Age')
78+
plt.ylabel('Estimated Salary')
79+
plt.legend()
80+
plt.show()
81+
```

0 commit comments

Comments
 (0)