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
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.