NumPy Tutorial for Beginners – Arrays

In this tutorial, we will learn about one of the most powerful scientific computing libraries Numpy. We will also learn what are Numpy’s n-dimensional arrays (ndarrays) and how to create, analyze, and manipulate them using Python.

If you are interested in a video version of this Numpy tutorial, then check out this course NumPy Data Science Essential Training from LinkedIn Learning.

To follow this article you, will need basic knowledge of the Python programming language. If you need a little refresher on Python, then check out this guide for reference:

What is Numpy?

Numpy is a scientific computing package used for performing mathematical and logical operations on arrays. And arrays are data structures that store the value of the same data type.

Numpy itself is written in C. However, Numpy extends Python by integrating powerful data structures by allowing us to use multi-dimensional arrays and matrices.

One of the reasons why Numpy is so useful for data analysis, machine learning & AI is that it already comes with powerful multidimensional array objects to work with large data sets.

Related: What is Machine Learning? – Supervised & Unsupervised

Installing Numpy

To install Numpy I suggest that you download the Anaconda distribution of Python, which is an open-source data science platform that comes with Numpy & other Python scientific libraries.

If you don’t want to use the Anaconda, then you can also install Numpy from your terminal using the following command:

pip install numpy

Numpy Arrays from List

Before we jump into Numpy arrays, let’s define again what actually is an array. Well, an array is simply a data structure that contains a group of elements that are all of the same data types.

A Numpy array is also a data structure similar to an array but they are the core data structure of the whole Numpy library and comes in two forms. They are vectors and matrices.

Vectors are strictly 1-dimensional arrays. Where matrix or matrices are 2-dimensional arrays that can hold rows and columns.

One of the most common ways to create a Numpy array from a Python list is by using the np.array() function. The example below shows how you can create a 1-dimensional Numpy array.

# Creating an 1-d array from Python list.

import numpy as np

myList = [0,1,2,3,4,5,6,7,8,9]
myArray = np.array(myList)

print(myArray)

Over here we are creating a 1-dimensional Numpy array using the function np.array().

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

It may look like a regular Python list, but it’s not. And we can verify that using the type() function.

print(type(myArray))

Output:

class 'numpy.ndarray'

After running the previous line, we see that myArray is of the type numpy.ndarray.

Now, let’s create a 2-d Numpy array.

In order to create a 2-d array, we have to create a list of lists. And then similar to before, we will pass that list onto np.array() function to generate our 2-d Numpy array.

# Creating an 2-d array from Python list.

import numpy as np 

myList = [[1,2,3],[4,5,6],[7,8,9]]
myArray = np.array(myList)

print(myArray)

So, what we get is a 2-dimensional array with 3 rows and 3 columns.

Output:

[[1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]]

We can also check the type of the array to verify that it is of type numpy.ndarray.

print(type(myArray))

Output:

class 'numpy.ndarray'

Here we see that our 2-d array or matrix is also an object of type numpy.ndarray. And nd refers to an n-dimensional or multi-dimensional array.

Numpy Arrays Using Built-In Functions

Next up on this Numpy tutorial is creating Numpy arrays using built-functions.

Now that we know, how to create Numpy arrays from regular Python lists, I will go ahead and show you how to create them using built-in functions that Numpy provides.

arange()

Numpy has a built-in function called the arange() function. It is similar to the Python built-in range() function.

Let’s say we want to create an array that contains 5 digits from index 0 to 5. We can type:

myList = np.arange(0,5)
print(myList)

Output:

[0, 1, 2, 3, 4]

Numpy created an array of 5 digits starting from index 0 to 5.

Let’s take another example. Say that we want to generate an array starting from 2 to 10.

myList = np.arange(2,10)
print(myList)

Output:

[2, 3, 4, 5, 6, 7, 8, 9]

Notice how that our output on the previous line gave us an array starting from 2 till 10. But the 10 is actually not included. If you want to grab 10 as well then you can type:

myList = np.arange(2,11)
print(myList)

Output:

[ 2, 3, 4, 5, 6, 7, 8, 9, 10]

The arange() function can also take a third argument which specifies the number of steps or spacing between values.

For example, if you want to generate all the even numbers between 0 to 10, pass 2 as the third argument. This will tell Python how many steps to take between the values inside your array.

myArray = np.arange(0,11,2)
print(myArray)

Output:

[ 0,  2,  4,  6,  8, 10]

All you have to add is to add a step size of 2 to get an array of even numbers. By default, the step size in arange() function is set to 1.

zeros()

There is also a function known as zeros() to generate an array of all zeroes. And it looks like this:

# Generating 1-d array of zeros. 

myArray = np.zeros(10)
print(myArray)

Output:

[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]

What we got here is a 1-dimensional array of 10 zeros. On the other hand, we can also create a 2-d array by passing a tuple.

# Generating 2-d array of zeros. 

myArray = np.zeros((3,3))
print(myArray)

Output:

[[0., 0., 0.],
 [0., 0., 0.],
 [0., 0., 0.]]

The first number that we passed represents the number of rows and the 2nd number represents the number of columns. In this case, we passed (3,3) to generate an array of 3 rows and 3 columns.

ones()

The same way we can also generate an array of ones.

# Generating 1-d array of ones. 

myArray = np.ones(8)
print(myArray)

Output:

[1., 1., 1., 1., 1., 1., 1., 1.]

And for generating a 2-d array of ones:

# Generating 2-d array of ones. 

myArray = np.ones((3,3))
print(myArray)

Output:

[[1., 1., 1.],
 [1., 1., 1.],
 [1., 1., 1.]]

linspace()

There is another useful built-in function that comes with Numpy. We call it the linspace().

And what it does is, it will return evenly spaced numbers over a specific interval. Let’s say we want 5 evenly spaced points between 1 to 2. For that I will type:

linArr = np.linspace(1,2,5)
print(linArr)

Output:

[1.  , 1.25, 1.5 , 1.75, 2.  ]

So our output tells us that, our array starts from 1 and stops at 2. I specified the third argument to be 5 to get 5 evenly spaced points between my start and stop.

I can also generate an array of 50 evenly spaced points between 1 to 2.

linArr = np.linspace(1,2,50)
print(linArr)

Output:

[1.        , 1.02040816, 1.04081633, 1.06122449, 1.08163265,
 1.10204082, 1.12244898, 1.14285714, 1.16326531, 1.18367347,
 1.20408163, 1.2244898 , 1.24489796, 1.26530612, 1.28571429,
 1.30612245, 1.32653061, 1.34693878, 1.36734694, 1.3877551 ,
 1.40816327, 1.42857143, 1.44897959, 1.46938776, 1.48979592,
 1.51020408, 1.53061224, 1.55102041, 1.57142857, 1.59183673,
 1.6122449 , 1.63265306, 1.65306122, 1.67346939, 1.69387755,
 1.71428571, 1.73469388, 1.75510204, 1.7755102 , 1.79591837,
 1.81632653, 1.83673469, 1.85714286, 1.87755102, 1.89795918,
 1.91836735, 1.93877551, 1.95918367, 1.97959184, 2.]       

Do remember that our output here is a 1-d vector or 1-d array as we have only one set of square brackets.

Now here is an important thing, don’t confuse linspace() with arange(). The linspace() gives you evenly spaced precise data points between two values. On the other hand, arange() takes the third argument that specifies the increments after each value.

The way you can see this is that you can use linspace() to get more precise evenly spaced data points between two numbers where arange() gives you more larger intervals between two numbers.

eye()

We can also create an identity matrix using Numpy. An identity matrix is a 2-dimensional square matrix where the number of rows is equal to the number of columns.

To create an identity matrix, we have to use the function np.eye() and pass just one argument. It looks something like this:

identityMat = np.eye(8)
print(identityMat)

Output:

[[1., 0., 0., 0., 0., 0., 0., 0.],
 [0., 1., 0., 0., 0., 0., 0., 0.],
 [0., 0., 1., 0., 0., 0., 0., 0.],
 [0., 0., 0., 1., 0., 0., 0., 0.],
 [0., 0., 0., 0., 1., 0., 0., 0.],
 [0., 0., 0., 0., 0., 1., 0., 0.],
 [0., 0., 0., 0., 0., 0., 1., 0.],
 [0., 0., 0., 0., 0., 0., 0., 1.]]

The output shows that we have a 2-dimensional matrix with 8 tows and 8 columns. And it only consists of 1s and 0s.

Also, notice how the diagonals are the 1s and the rest are just 0s.

Numpy Arrays of Random Numbers

You can also generate random numbers using Numpy.

random.normal()

To generate a random number from the normal distribution you can type:

randomNum = np.random.normal()
print(randomNum)

Output:

0.4788677086535386

Obviously the result is most likely to be different for you since Numpy generates this number randomly.

We can also specify how many random numbers we want from the normal distribution.

randomNums = np.random.normal(size=5)
print(randomNums) 

Output:

[ 1.80517058, -1.00428261,  0.55339115,  1.28505388, -0.76103492]

We have an array of 5 random values from the normal distribution.

random.uniform()

Now, let’s say we want values from the uniform distribution. For that you can type:

randomNum = np.random.uniform()
print(randomNum)

Output:

0.7361103066273562

And the same way we can generate an array of random numbers from the uniform distribution.

randomNums = np.random.uniform(size=5)
print(randomNums)

Output:

[0.72582994, 0.59795509, 0.47816143, 0.49102028, 0.60275951]

random.rand()

Okay, so what if we want to generate a 2-d matrix with two rows and two columns? I can type:

randomMat = np.random.rand(2,2)
print(randomMat)

Output:

[0.72914557 0.28649648]
[0.57465855 0.55108253]

random.randint()

One last thing that I want to talk about this post is how to generate a certain amount of random integers between certain values.

Let’s say I want to generate a Numpy array of four random integers between 1 and 50. I can type:

randomNums = np.random.randint (low = 1, high = 100, size = 4)
print(randomNums)

Output:

[68, 23, 23, 81]

I passed three arguments. The first two arguments specify that I want the array to be between 1 & 100. And the last one specifies that I only want four values.

Vectors to Matrices

So far in this Numpy tutorial, we saw how to create 1-dimensional vectors or arrays and 2-d matrices. But there’s one more thing that I want to talk about is converting a vector into a matrix in Numpy.

reshape()

Let’s start by generating a random 1-d array of 16 integers.

myArray = np.random.rand(16)
print(myArray)

Output:

[0.98603364, 0.47461217, 0.12793579, 0.75083417, 0.08093329,
 0.68316509, 0.95807723, 0.89187899, 0.06593721, 0.72183955,
 0.71391102, 0.35244838, 0.4700743 , 0.71870386, 0.78268923,
 0.73916623]

We only have one set of brackets and thus our output is a 1-d array.

In order to convert this into a 2-d array or a matrix, we have to use the reshape() function. And this is how it goes:

myArray = myArray.reshape(4,4)
print(myArray)

Output:

[[0.98603364, 0.47461217, 0.12793579, 0.75083417],
 [0.08093329, 0.68316509, 0.95807723, 0.89187899],
 [0.06593721, 0.72183955, 0.71391102, 0.35244838],
 [0.4700743 , 0.71870386, 0.78268923, 0.73916623]]

So our matrix has 4 rows and 4 columns. Since we have converted a vector with 16 integers, I passed 4 and 4 as arguments. If you have an array of 25 values then you have to pass 5 and 5 as arguments.

The reason for this is, the reshape() function can only convert to equal numbers of rows and columns. Since the vector myArray consists of 16 values, we can only use the reshape () function to create a 4×4 matrix.

Conclusion

Well, that’s it for this post. Our next Numpy tutorial will be on indexing & slicing Numpy arrays. Make sure to go over this post again if any of the concepts confuse you.

Here’s the link to the tutorial for Indexing & Slicing in Numpy:

Don’t forget to check out the course NumPy Data Science Essential Training to learn Numpy through interactive videos. I highly recommend checking it out.

Did you find any part of this Numpy tutorial confusing? If so. which part? Make sure to comment below.

Leave a Reply