The Programming Language For Learning Machine Learning

In post #3 of the series taking you from the very basics to the advanced techniques of machine learning, we’ll take about the programming language we’ll be using to actually implement the machine learning algorithms.

We’ll also look at the basic features of this language and how to start writing simple programs in it.

And it is…

Octave!

If your new to machine learning, you’ve probably never heard of it. You may have heard of MATLAB; basically, octave is pretty much like MATLAB but it is open source and free to use.

So, that’s one important reason we’re going to be using this language rather than MATLAB, to make following this tutorial open to everyone and free from all costs.

Why not a more popular language?

However, you may be wondering why we did not choose a more well-known language such as Python, Java or C++?

Well, for starters, each of the more well known languages have their own technicalities. If we had decided to use Python, for example, a C++ developer would have found it a little hard to follow.

By choosing a generalized language that is extremely easy to learn and takes no time to pick up (in fact, by the end of this post, you will already become pretty familiar with it yourself!), everyone can follow along.

As this language is extremely high-level, you can quickly get your machine learning algorithm up and running for testing purposes. When you are satisfied with its performance, you can convert it to a program in the programming language your comfortable with.

In many places, people first test the basics of their algorithm by implementing it in Octave to get an overview of it, and, if it is feasible and practical for implementation, they’ll go ahead and create it in a lower level language such as C++ for the performance benefits.

Another important reason is that it comes built-in with powerful linear algebra libraries that are crucial to speeding up our machine learning programs.

As you’ll come to see, machine learning relies heavily on linear algebra operations such as matrix multiplication, finding the transposition of matrices, and etc.

By using a powerful linear algebra, we will be able to optimize our machine learning algorithms by “vectorising” the operations: instead of going through a loop for example, we can convert it to a simple multiplication between two vectors or matrices.

Oh, and due to the similarity between Octave and MATLAB, if you pick one up, picking the other one up will become easy-peasy.

Setting it up

Just head over to the Octave downloads section and pick the download according to the platform you’re on.

The setup is really easy; for Windows, there’s an installer that will do it all for you.

The basics

You’ll notice, it will have added two applications a GUI one and a command line (CLI) one.

We’ll be using the GUI one as the GUI is the overpowered one that includes the CLI inside it.

Basically, the CLI is for executing commands (similar to the Python one).

You’ll see the GUI version includes the features of CLI inside it, the “Command Window” tab.

Now, the reason the GUI is more powerful is that, as you can see, it has an inbuilt editor where you can create and edit Octave files.

However, just like any other programming language, you can do this with any text editor; it’s just a matter of convenience.

Basic commands

You can do basic arithmetic:

You can create variables. If you haven’t programmed before, think of them as containers to store values. Here we’re storing the results from the arithmetic:

In Octave, you don’t have to specify the type of a variable (int, float, bool, etc.)

If you want to see all of the variables that you’ve created, you can use the whos command:

You’ll notice two important things here:

  • Octave automatically set the type of the variable
  • Everything is stored as a matrix; here, as it just contains one value, it is a 1×1 matrix

Creating matrices

You use “[” and “]” to denote a matrix. Everything is a matrix as seen above; however, if it consists of just one column, we refer to it as simply a vector (even though it “technically” is a matrix with just one column).

Observe this:

Now, you’ll note several things:

  • A comma and a space ( “, “) is used to add numbers to the same row — it adds another column as seen in mat1 above.
  • A semicolon (“;“) is used to add numbers to the next row — it adds another row as can be seen in mat2
  • Now, if you combine the two, you can create a 2×2 matrix

Note: the comma is optional; you can simply do and it will be the same thing:

Of course, you can use these symbols how many times you want to create a matrix of your desired dimensions:

Manipulating Matrices

We can also access and change the values of the matrices.

What we did was get the specific number we wanted from the matrix we had created above.

mat(a, b) — Gets the number in row a and column b

A number in a matrix is sometimes referred to as an element in the matrix.

Above, we got the first element (row 1, column 1), the last element (row 4, column 3), and the element in the 2nd row and 2nd column.

Note, unlike other programming languages, in Octave, the first element starts with 1 and not a 0.

What if we want to change a number in the matrix? We can do the following:

Not only can we access a matrix, we can also change it as well.

Sometimes it is required for us to convert an entire matrix into a single column vector? We do this like so:

The “:” basically means all. Doesn’t make sense? What if we want to get all of the rows but just column 1? What if we want to get all of the columns but just row 3? We can do this:

Neat, huh?

Functions

So, we’ve been using functions until now, but we haven’t had a formal introduction yet. Functions are just predefined lines of code.

What if we want to drink coffee? We can do something like this (each line below represents a command):

walk to kitchen

find coffee ingredients

make coffee

drink coffee

Or, we can put it all under a function called “drinkCoffee()” and just write it once. Now, after we’ve written it, whenever we want to drink coffee, we can just call the function rather than writing all of those lines of code above again.

Above, we used functions to access and manipulate matrix values.

As they are pre-written by the developers of the language, there are some useful functions that we can use to help ourselves:

What if we want to create a 3×3 identity matrix? There’s a function for that:

What if we want to create a 2×5 matrix of random numbers between 0 and 1? There’s a function for that:

What if we want to create a matrix of just ones? What about just zeroes? You guessed it! There are functions for that:

There are many other functions that exist, but we’ll get introduced to them as we progress along. It is more important to be familiar with the fundamentals right now.

So basically the format of a function is as follows:

function_name + “()” + values in the “()”

The values are formally called arguments, and they specify the technical information you want the function to do. For example, for the zeros function, the “(5, 5)”, specified that we wanted a 5×5 matrix.

Useful stuff

One thing you will have noticed it is that it outputs the result each time we do something. If you don’t want this, you can just put a semicolon at the end:

Although octave stored the result of the function inside the mat variable, there’s no output. Sometimes when your working with functions that return a lot of data and you don’t want to clutter your workspace, you might find this useful.

Another useful function is the clear function. It takes no arguments and what it does is simply removes all of the variables created.

And, one of the most helpful of them all, is the help <function name>. If you’re ever unsure what a particular function does, you can call help + name of the function, and it will give you the documentation about that function:

Conclusion

There’s a lot more functions that needed to be learned to start writing algorithms in Octave. However, I don’t want to overload you with everything in one post, so I posted all that is required to start the flame of learning inside you.

This information will help you be up and running with Octave and start tinkering on your own. After all, practice by doing is the best way to learn!

But don’t be worried! In the next post, we’ll look at more intermediate topics in Octave, making you fluent enough to start writing your algorithms!

Until then, adios!

(As usual, if you didn’t understand something, feel free to comment down below!)

Follow us on Twitter (@TrollgenStudios) to be the first to know when the next post comes out

2 thoughts on “The Programming Language For Learning Machine Learning

  1. Pingback: Linear Algebra Review For Machine Learning – Trollgen Tech

  2. Pingback: An Intro To Linear Regression – Trollgen Tech

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s