Home » Programming Languages » Python

Introduction to Numpy – A Math Library for Python

This article was written by Vasudev.

Lets get started quickly. Numpy is a math library for python. It enables us to do computation efficiently and effectively. It is better than regular python because of it’s amazing capabilities.

In this article I’m just going to introduce you to the basics of what is mostly required for machine learning and datascience. I’m not going to cover everything that’s possible with numpy library. This is the part one of numpy tutorial series.

4035960368

A small illustrated summary of NumPy indexing and slicing (see here for details)

The first thing I want to introduce you to is the way you import it.

3978030418

Okay, now we’re telling python that “np” is the official reference to numpy from further on.

Let’s create python array and np array.

3978033631

If I were to print them, I wouldn’t see much difference.

3978036966

Okay, but why do I have to use an np array instead of a regular array?

The answer is that np arrays are better interms of faster computation and ease of manipulation.

Let’s proceed further with more cool stuff. Wait, there was nothing cool we saw yet! Okay, here’s something:

np.arange()

3978046526

What arange([start],stop,[step]) does is that it arranges numbers from starting to stop, in steps of step. Here is what it means for np.arange(0,10,2):

return an np list starting from 0 all the way upto 10 but don’t include 10 and increment numbers by 2 each time.

So, that’s how we get :

3978049559

important thing remember here is that the stopping number is not going to be included in the list.

another example:

3978053703

Before I proceed further, I’ll have to warn you that this “array” is interchangeably called “matrix” or also “vector”. So don’t get panicked when I say for example “Matrix shape is 2 X 3”. All it means is that array looks something like this:

3978055981

Now, Let’s talk about the shape of a default np array.

Shape is an attribute for np array. When a default array, say for example A is called with shape, here is how it looks.

3978058980

This is a rank 1 matrix(array), where it just has 9 elements in a row. 

Ideally it should be a 1 X 9 matrix right?

I agree with you, so that’s where reshape() comes into play. It is a method that changes the dimensions of your original matrix into your desired dimension.

Let’s look at reshape in action. You can pass a tuple of whatever dimension you want as long as the reshaped matrix and original matrix have the same number of elements.

3978062012

Notice that reshape returns a multi-dim matrix. Two square brackets in the beginning indicate that. [[1, 2, 3, 4, 5, 6, 7, 8, 9]] is a potentially multi-dim matrix as opposed to [1, 2, 3, 4, 5, 6, 7, 8, 9].

Another example:

3978065829

If I look at B’s shape, it’s going to be (3,3):

3978070146

To read the rest of the article, click here. More about Numpy can be found here

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *