Machine Learning : Decision Tree (Part 15)

This time we are learning decision tree for the classification one

So, if the data is looking like this

we can split it to separate green and red data.

Splitting based on 60 on X2 & the decision tree

Then based on 50 on X1 data & the decision tree

Now on X1 with value 70 & the decision tree:

Finally on X2 with 20 & the decision tree

Done!

Let's code it down!

Problem statement: We are launching a new SUV in the market and we want to know which age people will buy it. Here is a list of people with their age and salary. Also, we have previous data of either they did buy any SUV before or not.

Now import libraries, dataset and split:

Feature Scaling

Training the Decision Tree Classification model on the Training set

from sklearn.tree import DecisionTreeClassifier

classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)

classifier.fit(X_train, y_train)

Predicting a new result

print(classifier.predict(sc.transform([[30,87000]])))

Predicting the Test set results

y_pred = classifier.predict(X_test)

print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

Making the Confusion Matrix & accuracy

Visualizing the Training set results

Visualizing the Test set results

Done!

Checkout the code and dataset