GOALS

This tutorial will show you how to:

  • Create and run a GAUSS program from scratch.
  • Use GAUSS functions, operators, and indexing.

By the end, you will have a simple GAUSS program which performs a nearest neighbor search. We recommend looking over Getting Started with GAUSS, before working through this tutorial.

 

PROBLEM DESCRIPTION 

 

GAUSS nearest neighbors plot.

Suppose you have been given the following data about some automobiles:

 

and that your task is to create a GAUSS program which can find the most similar car from your list when given the mpgweight, and displacement for a new car.

 

STEP 1: CREATE A NEW GAUSS PROGRAM FILE 

The first thing we need to do is to create a new file for this GAUSS program. We can do this by entering the following statement in the GAUSS Program Input/Output window:

 

edit find_neighbors.gss

 

This command will open the file find_neighbors.gss in our current GAUSS working directory. Since the file does not yet exist, GAUSS will create a new file for us.

 

GAUSS does not require any particular file extension for GAUSS program files, however, .gss is recommended.

 

STEP 2: ENTER THE DATA 

We will start our program by entering the data from the table at the top of this tutorial as well as data for a car that we want to compare.

 

 // Create a 5x3 matrix containing
 // our main 'auto' data
 autos = { 16 3880 231,
 41 2040 90,
 21 4290 350,
 28 1800 98,
 14 4130 302 };

// A 1x3 matrix with the data
 // for the new car
 x = { 17 3170 163 };

 

Note that lines which start with // are comments in GAUSS.

 

STEP 3: RUN THE PROGRAM 

Now that we have some code in the file, let’s run it and create the matrices autos and x. Select the downward pointing triangle next to the Run button on the GAUSS Toolbar and select Current File as shown in the image below.

Run a GAUSS program.

Since the program only creates two matrices, we will not see any output. We can verify that the data was created, by using the print keyword as shown in the above image.

 

STEP 4: COMPUTE THE EUCLIDEAN DISTANCE 

The distance metric we will use is Euclidean distance, shown in the equation below:

We can accomplish this task in one line of code, like this:

 

 // Compute the Euclidean distance between 'x' and 'autos'
 dist = sqrt(sumr((x - autos).^2));

 

However, to make things simpler, we will work through each part of that statement separately.

 

A. FIND THE DIFFERENCES

 // Find the difference between
 // the 3 variables in 'x' and 'autos'
 diff = x - autos;  

 

GAUSS will automatically expand the row vector, x to have the same number of rows as the matrix autos, so the code above will perform the computation below:

 

B. SQUARE THE DIFFERENCES

Next we use the element-by-element exponentiation operator, .^ to square each element in the matrix of differences.

 

© 2024 Aptech Systems, Inc. All rights reserved.

 // Square each element in 'diff'
 diff_sq = diff .^2;

C. SUM THE ROWS

We can sum across the rows of our squared difference matrix with the sumr function.

 

 // Sum across the rows
 sum_diff_sq = sumr(diff_sq);

D. COMPUTE THE SQUARE ROOTS

Now we can compute the distance by taking the square root of each element of our vector.

 

 // Compute the square root of each element
 dist = sqrt(sum_diff_sq);

STEP 5:FIND THE SMALLEST DISTANCE 

It is clear from looking at the computations shown above, that in this case, the car from the first row of the autos matrix has the shortest distance from the sample car represented by the row vector, x. However, to compare larger datasets, we need our program to search for us.

 

The GAUSS function, minindc, returns the index of the smallest element in each column of a matrix.

 

 // Find the index of the smallest distance
 idx = minindc(dist);

// Find the smallest distance
 dist_min = dist[idx];

 

STEP 6: PRINT THE RESULTS 

Now that we have found the index of the row with the smallest distance, we should add some print statements so that our program can tell us what it found.

 

 print "The closest car is from row " idx;
 print "with a distance of: " dist_min;

 

Run your program again and you should see the following output:

 

The closest car is from row 1.00
with a distance of: 713.25

 

Looking over the original data, this answer appears correct.

 

CONCLUSION 

Congratulations! You have:

  • Created and run a GAUSS program.
  • Used GAUSS functions, operators and indexing to perform a nearest neighbor search.
  • Printed the program results.

For convenience, the full program text is reproduced below.

 

 // Create a 5x3 matrix containing
 // our main 'auto' data
 autos = { 16 3880 231,
 41 2040 90,
 21 4290 350,
 28 1800 98,
 14 4130 302 };

// A 1x3 matrix with the data
 // for the new car
 x = { 17 3170 163 };

// Find the difference between
 // the 3 variables in 'x' and 'autos'
 diff = x - autos;

// Square each element in 'diff'
 diff_sq = diff .^2;

// Sum across the rows
 sum_diff_sq = sumr(diff_sq);

// Compute the square root of each element
 dist = sqrt(sum_diff_sq);

// Find the index of the smallest distance
 idx = minindc(dist);

// Find the smallest distance
 dist_min = dist[idx];

print "The closest car is from row " idx;
 print "with a distance of: " dist_min;