NumPy

Other ways to create NumPy arrays

In this tutorial, you will learn more methods for creating NumPy arrays.


Up to this point, all our NumPy arrays were derived from lists.

Python

However, this is not the only way to create NumPy arrays.

In data analysis, it's common to initialize arrays with specific shapes for further computations.

np.zeros(shape) creates an array filled with zeros of the specified shape:

Python
Output

Here, we created a 1D array (or vector) of 5 zeros.

To add more dimensions, you need to pass a tuple specifying the desired shape.

Python
Output

np.ones(shape) does the same as np.zeros(shape) but fills the array with 1s.

Python
Output

You can also use these methods to create an array that has the same shape as another one.

Here, we first create an array of ones and use its shape attribute to generate an array of zeros with identical dimensions:

Python
Output

If you want to fill the array with any other number, you can use np.full(shape, num).

For example:

Python
Output

A common need in data science computations is to generate random numbers.

This can be achieved using the np.random.sample(shape) method from NumPy's random module.

np.random.sample(shape) creates an array of the specified shape filled with random numbers between 0 and 1.

Let's give it a try:

Python
Output

What will be the output?

Python

What will be the output?

Python

What will be the output?

Python

Occasionally, you may need to combine two or more arrays.

To combine arrays, you can use the np.concatenate() function:

Python

arr1 and arr2 are the arrays you want to combine. The axis parameter defines the axis along which the arrays will be joined (default=0).

Let's combine 2 simple vectors:

Python
Output

Since we only have 1 dimension, there's no need to define an axis.

Let's concatenate 2-dimensional arrays along their rows (axis = 0):

Python
Output

Since we have two dimensions, we can define another dimension to concatenate along. Let's combine the arrays along their columns:

Python
Output

What will be the output?

Python

What will be the output?

Python

What will be the output?

Python

What will be the output?

Python

What will be the output?

Python

What will be the output?

Python

Note that the shape of the arrays that you combine can vary.

But, only along the axis that you combine them along.

Consider the following arrays with different shapes:

Python
Output

You can combine them along the first dimension (rows):

Python
Output

However, joining them along their columns would result in an inconsistent shape and is therefore not possible:

Python

What will be the output?

Python

What will be the output?

Python

As you can see, NumPy gives you pretty nice error messages that help you find your mistakes.

I can tell you that you don't need to master all of NumPy's functionality perfectly.

And you don't have to know all of the hundreds of methods.

It's just important that you know the basics and have an idea of what's possible.

You can research the rest when you need it. NumPy's documentation is excellent, and there are countless resources available for every single NumPy method.