The world’s leading publication for data science, AI, and ML professionals.

Data Science with Julia: Linear Algebra

Linear Algebra in Julia – Hint: It's even easier than Numpy

When it comes to Data Science, there are not so many programming languages to choose from. You can go with Python or R, or more recently, with Julia. In case you don’t know what Julia is, please refer back to one of my previous articles:

Introducing Julia: An Alternative to Python and R for Data Science

If you don’t want to read the whole thing (that’s cool too), here’s the one-sentence summation:

Julia is a programming language that writes like Python but runs like C.

Because of those reasons I want to give you, my fellow reader, a primer in Linear algebra using Julia programming language. Now, this isn’t intended to be a full course on linear algebra, as it is impossible to teach one in 5 minutes (if you’re not you know who), but the article will cover how to deal with linear algebra data structures – vectors and matrices – and how to do cool stuff with them.

So yeah, the article assumes you know at least something on the topic, and are familiar with linear algebra operations in let’s say Numpy, but are eager to see what Julia has to offer.

Without further ado, let’s get started! I’m assuming you have Julia installed and running in some environment like Jupyter (if you don’t refer to the article above). You’ll only need to import one library, and you can probably guess which one is it gonna be:


Declaring Vectors and Matrices

There’s no point in talking about eigendecomposition and Euclidean norm if we don’t know how to declare basic vectors and matrices. It’s quite easy though, I’d say a bit easier than in Python (not sure about R).

Row vector

To make a row vector, you’ll need to surround the elements with square brackets, and NOT to put commas in between (if you put commas you will not get a vector):

Column vector

In a similar fashion, to make a column vector all you need to do additionally is to put a single quotation mark at the end, everything else is the same as with the row vectors:

Matrix

The syntax for creating a matrix is very similar – you will declare it row by row, putting semicolon (;) to indicate the elements should go on a new row:

Matrix of zeros

The syntax to create an *nm** matrix of zeros is very similar to the one in Python, just without the Numpy prefix:

Matrix of ones

Same goes for the *nm** matrix of ones:

Diagonal matrix

If you want to make a diagonal matrix, ergo a matrix where elements that are not on the diagonal are 0, you can do it via Diagonal() and passing in the array for values of elements on the diagonal:

Identity matrix

In a similar fashion you can create an *nn** identity matrix:

Matrix of random numbers

To create an *nm** matrix with elements drawn from a uniform distribution, you can use the rand() method:

Matrix of random numbers (normal)

Or use randn() to draw elements from the standard normal distribution (mean = 0, var = 1):

Now that was quite a bit we did already but still haven’t covered any linear algebra operations – that’s coming in the next section.


Linear Algebra Operations

In this section, we’ll cover stuff like matrix multiplication, inverse, determinant, eigendecomposition, and much more. Let’s get started!

Transpose

To calculate the matrix transpose you can use the convenient transpose() function:

Reshaping

An array can also be reshaped – here I’m creating an array with elements from 1 to 10, both included, and reshaping them into a matrix of 2 rows and 5 columns:

Flattening

You can flatten *nm** matrix into a column vector easily – this will have so many use cases later when covering data science:

Dot product

I’m sure you can guess the name of a method for calculating the dot product:

Matrix multiplication

To perform matrix multiplication you can use the multiplication symbol, as opposed with Python where you’ve used the @ sign:

Element-wise multiplication

To multiply elements element-wise, use **.*** symbols:

Matrix inverse

To calculate the inverse of a matrix, use the inv() method:

Determinant

And to calculate determinant use the det() method:

Eigendecomposition

You can also perform the eigendecomposition of a matrix. It will return eigenvalues and eigenvectors, so if you want to store those you’ll need two variables:

Vector norm

And finally, to calculate simple Euclidean norm you can use the norm() method:

And I think this will be enough, maybe even a bit too much. I hope it wasn’t a big problem for you. For me, linear algebra feels natural in Julia, maybe even a bit more than in Python. Let’s hope this claim remains true in the future when we’ll deal with real datasets.


What’s Next

Before jumping into some "concrete" data science, where concrete means EDA, data visualization, and Machine Learning, I want to make sure the basics are covered properly. The main reason is – you need to know the language you’re working with. Seeking for answers online is cool, but what’s not cool is constantly doubting yourself because you’re not sure if you’ve done things properly.

That’s why you can expect three more articles similar to this one, covering:

  • Calculus
  • Statistics/Probability
  • Basic DataFrames

And only then will we take a step towards some applicative data science. Thanks for reading, I hope you’ve liked it.


Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.

Join Medium with my referral link – Dario Radečić


Related Articles