"

Chapter 4 – Visual Processing and Distance Measurement Techniques

4.3 – Regression and Calculating Distance

If you desire Zumi to move ahead, there are two commands you can issue: duration (time) and speed. However, what if you possess knowledge about the distance to your intended destination? How can you calculate the time required and the optimal speed to reach that destination? This lesson will demonstrate how to gather data from your Zumi and utilize rudimentary machine learning techniques to forecast the travel time necessary for a given distance.

Step 1: Import Libraries

from zumi.zumi import Zumi
zumi = Zumi()

Step 2: Plotting data

When graphing data, we typically have a set of ordered pairs or a table of corresponding x and y values that we can plot on a graph. For example, in a math class, you may encounter a table such as the one provided below.

image

After obtaining the ordered pairs, the next step is to plot them on a graph and determine the correlation between the two sets of data. This is achieved by drawing a line or curve that best represents the data, commonly referred to as the line of best fit. For instance, in the given scenario, there is a linear correlation between temperature and ice cream sales. By utilizing the plotted data, it is possible to forecast ice cream sales based on the temperature!

image

Step 3: Lists

Although you may create a table in a math class, conveying to a computer that you wish to plot two sets of data necessitates the use of data structures such as lists in Python. Lists are capable of storing large quantities of data and are denoted with brackets [] instead of variables. Below are examples of lists that can be utilized in lieu of a table. These lists can be employed to store the data gathered when measuring distances using Zumi:

x = [60,62,64,65,70,74,82]
y = [15,20,22,26,34,39,59]

Step 4: Regression Training Wizard

We can train Zumi using Machine Learning and have it predict how long it needs to travel to drive a certain distance. In the explore page on zumidashboard.ai, there is a module called Learning to Drive the Distance.

image

After clicking Learning to Drive the Distance, we can test how far Zumi travels for each duration and after collecting all the data, it automatically uses linear regression to create a model that can make a prediction on duration given distance.

image

Afterwards, you will be given a slope and intercept that is important to know so dont close the screen!

Step 5: Equation for a line

To depict the correlation between Zumi’s driving speed and distance, an equation for a line must be established. Once this equation is determined, it becomes possible to estimate how far Zumi can travel for any given duration. However, to determine the duration required for a desired distance, the equation must be rearranged.

It is crucial to keep in mind that “x” represents the duration in seconds, whereas “y” represents the distance in inches. The following code exemplifies the function once translated. Replace the slope and intercept values with those obtained from the Linear Regression Wizard and execute the cell to incorporate the function into the program:

def move_inches(distance):
intercept = #Insert your intercept value here
slope = #Insert your slope value here
seconds = (distance + intercept)/slope
zumi.forward(speed=40,duration=seconds)

After training the model, we are given an intercept and slope. Using these, we can fill in the empty variables.

def move_inches(distance):
intercept = -5.4845588235293885
slope = 15.817647058823512
seconds = (distance + intercept)/slope
zumi.forward(speed=40,duration=seconds)

Now, we can call the function with a certain distance, and it will then calculate the the seconds as shown in the third line of the move_inches(distance) function and then move forward for a duration of that calculated time.

move_inches(5):

Activity

Change this function to take seconds as a parameter instead of distance, and then rearrange the seconds formula to solve for distance.

def move_inches(distance):
intercept = -5.4845588235293885
slope = 15.817647058823512
seconds = (distance + intercept)/slope
zumi.forward(speed=40,duration=seconds)

Solution:

def move_inches(seconds):
intercept = -5.4845588235293885
slope = 15.817647058823512
distance = (seconds*slope) + intercept
zumi.forward(speed=40,duration=seconds)

Conclusion:
Congratulations, you can start using Linear Regression to train a model that can calculate distance in Python!

Demo

Questions