AI learning with decision trees
Who Drove?
As a practical AI application, let's look at a decision tree based on car driving data to determine who operated a vehicle. As in the previous Programming Snapshot [5], I prepared data collected by my Automatic adapters [6] in my two cars, which recorded when, where, and how fast the vehicles were moving.
The only thing the adapter doesn't know is who was driving, and since my wife and I alternately drive both cars, I'd like to teach an algorithm to guess the driver by looking at the trip data. To get the project off the ground with some sample data from which to learn, I added driver abbreviations M
or A
in Listing 2 on selected trips where I knew who was driving at the time.
Listing 2
trips-learn.csv
Each line in the CSV file in Listing 2 represents a recorded drive; the second to last column, vehicle
, indicates whether commute mule Honda Fit (1
) or my sportier 1998 Acura Integra (2
) was used. My wife rarely drives the latter, but she often drives the mule to work during the week (dow = 1-5
), while I tend to take the Integra for a spin on the weekends (dow = 6-7
).
Whether the Honda or the Acura, the speed
column provided by the Automatic adapter seems to give me more points than my wife as a driver, for reasons that are incomprehensible to me. The brake or the acceleration ratings (brakes
and accels
) seem to be evenly distributed. Are these criteria enough to teach the system to guess the driver correctly on newly recorded trips with unknown drivers?
Driving Experience
Listing 3 loads the CSV file into a Python Pandas dataframe. The Y
list takes the entries that have been added to the driver
column manually as desired results as M
or A
. X
contains a two-dimensional list defined in line 9, in which the trip data are available in rows with the day of the week (dow
: 1-7), the number of miles traveled (miles
), hard brake and acceleration counters (brakes
and accels
), a speed rating (speed
), and the vehicle ID (vehicle
).
Listing 3
driver.py
As in the previously discussed, more academic case, the sklearn
class DecisionTreeClassifier
is also used in Listing 3; its fit()
method processes the training data to enable the model to later predict new results with predict()
.
Somewhat surprisingly, for new trip records shown in driver.py
(Figure 4), the model guesses pretty well, even when some columns like the mileage contain values that haven't been seen before. For the same car (1
: Honda Fit), only because of the driver's higher speed rating, the algorithm assigns the trip to M
, and to A
in the other case. After an in-depth analysis, the decision tree must have determined that this is the key distinguishing feature between the two drivers.
For a first attempt, the process produces very good results; more collected live data later on will show how reliable they really are. If improvements are needed, more training data will help produce a more accurate decision tree.
Infos
- Vo.T.H, Phuong, Martin Czygan, Ashish Kumar, and Kirthi Raman. Python: Data Analytics and Visualization. Packt Publishing, 2017
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/204/
- AlphaGo: https://deepmind.com/research/alphago/
- Joshi, Prateek. Artificial Intelligence with Python. Packt Publishing, 2017
- "Programming Snapshot – Mileage AI" by Mike Schilli, Linux Magazine, issue 203, October 2017, pg. 56: http://www.linux-magazine.com/Issues/2017/203/Programming-Snapshot-Mileage-AI
- "Programming Snapshot – Driving Data" by Mike Schilli, Linux Magazine, issue 202, September 2017, pg. 50, http://www.linux-magazine.com/Issues/2017/202/Programming-Snapshot-Driving-Data
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.