How to Use Decision Trees in Python to Predict Transportation Choices
Step 1: Introduction Decision Trees are one of the most intuitive and powerful machine learning algorithms.

I am an IT student with a strong passion and proficiency in programming. I have solid experience working with.
Step 2: Problem Statement
We have a dataset containing people's information and how they usually travel.
However, for a few new people, we only know their details not how they travel.
Our goal is to predict their mode of transportation using a trained decision tree model.
Data to Predict (Unknown Transportation)

Step 2. Training Data…..

Step 4: Loading the Data in Python.

Expected Output preview:

Step 5: Preparing Input and Target Data
Before training our Decision Tree model, we need to separate our dataset into
Inputs (features) the columns used to make predictions
Target (label) the column we want to predict (
Transportation)
Expected Only inputs
the our target is:
Step 6: Encoding Categorical Values..
Machine learning models can only work with numerical data like 1s and 0s, but our dataset contains text labels such as Male, Cheap, and High.
To fix this, we use Label Encoding a technique that converts each unique text value into a numeric code.

Step 7: Applying Label Encoding to the Dataset..
Now that we have created our label encoders in step 6, let’s apply them to the respective columns in our inputs DataFrame.
This process replaces text data with numeric codes that the model can understand.

Output Preview

Step 8: Preparing the Final Input Data for Training..
After encoding, our inputs DataFrame now contains both the original text columns Gender and their numeric versions like gender.
Since the model can only work with numbers, we will remove the text columns and keep only the numeric ones.

Output Overview:

Step 9: Import the Decision Tree Library..
To train our model, we’ll use the Decision Tree Classifier from the sklearn.tree module.

Step 10: Train the Decision Tree Model..
Now that we have prepared our inputs input_n and the target variable Transportation, let’s train our Decision Tree Classifier to learn patterns from the data.

Step 11: Evaluate the Model Accuracy..
After training our Decision Tree model, let’s test how accurately it performs on the data it was trained on.

The score represents accuracy the ratio of correct predictions to total predictions.
Since we are using the same dataset for training and testing, the accuracy might be very high (even 100%), but this doesn’t necessarily mean it will perform as well on unseen data.
Step 12: Making Predictions with the Model..
After training and testing our Decision Tree, let’s use it to predict the mode of transportation for a new individual.

Summary Table of Predictions..




