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.

Figure 4: After the learning phase, the decision tree identifies the driver from their driving behavior.

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

  1. Vo.T.H, Phuong, Martin Czygan, Ashish Kumar, and Kirthi Raman. Python: Data Analytics and Visualization. Packt Publishing, 2017
  2. Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/204/
  3. AlphaGo: https://deepmind.com/research/alphago/
  4. Joshi, Prateek. Artificial Intelligence with Python. Packt Publishing, 2017
  5. "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
  6. "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

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • R For Science

    The R programming language is a universal tool for data analysis and machine learning.

  • Machine Learning

    We explore some machine learning techniques with a simple missing person app.

  • Ethereum and Smart Contracts

    The Ethereum cryptocurrency system lets you build programs into the blockchain to orchestrate complex transactions and document results. We'll show you how to get started with Ethereum's smart contracts.

  • Kernel News

     

  • Command Line: tree

    The tree command can provide a clear view of all your files and directories and offers a few advantages over the usual ls command.

comments powered by Disqus
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.

Learn More

News