Let's say I was at a zoo with my dad and went to the crocodile and alligators section of the zoo. When I would look at the crocodile and alligator, my mind would say they are both alligators or they are both crocodiles. But, even if they look exactly the same, is there a way to distinguish both of those reptiles into separate parts? Well then Decision Tree Classification can help you out!
In this blog, I will go through creating and training the Decision Tree Classification model step-by-step and if you want to follow along, you can use Google Colaboratory or Kaggle.
Before we get cracking with the step-by-step for the Decision Tree Classification, let me answer a tiny amount of not-so-long questions that could help during this blog.
What is Decision Tree CLASSIFICATION (Noticed I put classification all in caps? That's because there is also Decision Tree Regression)?
What coding languages is Decision Tree Classification compatible with?
How should you use Decision Tree Classification
Answer to Q1: Decision Tree Classification helps you classify your data into two parts by using leaf nodes. For example, let's say that I wanted to see whether a person is fit or not fit. The Decision Tree Classification would help you split the leaf node into smaller leaf nodes. Here's a picture.
Answer to Q2: Decision Tree Classification can be and is mostly used with Python/NumPY and R.
Answer to Q3: Decision Tree Classification is used for classifying nodes by using yes/no type outcomes.
Now let's crack on with the coding part!
1. Import the libraries (as always)
First, we will have to import all of the needed libraries.
import numpy as np import matplotlib.pyplot as plt import pandas as pd
2. Import the dataset (as always)
After the first step, we will now import the dataset.
dataset = pd.read_csv('Social_Network_Ads.csv') X = dataset.iloc[:, :-1].values y = dataset.iloc[:, -1].values
3. Splitting the dataset into the Training set and Test set
Now, let's split the dataset into two parts, the training set and the test set.
from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
Let's see if all the variables have any output at all. Starting with X_train....
If you ran it, you should have gotten an output that looks like this (in reality it looks much longer):
[[ 44 39000] [ 32 120000] [ 38 50000] [ 32 135000] [ 52 21000] [ 53 104000] [ 39 42000] [ 38 61000] [ 36 50000] [ 36 63000] [ 35 25000] [ 35 50000] [ 42 73000] [ 47 49000] [ 59 29000] [ 49 65000] [ 45 131000] [ 31 89000] [ 46 82000] [ 47 51000] [ 26 15000] [ 60 102000] [ 38 112000] [ 40 107000] [ 42 53000] [ 35 59000] [ 48 41000] [ 48 134000] [ 38 113000] [ 29 148000] [ 26 15000] [ 60 42000