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
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
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.