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.
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:
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.
np.ones(shape)
does the same as np.zeros(shape)
but fills the array with 1s.
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:
If you want to fill the array with any other number, you can use np.full(shape, num)
.
For example:
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:
What will be the output?
What will be the output?
What will be the output?
Occasionally, you may need to combine two or more arrays.
To combine arrays, you can use the np.concatenate()
function:
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:
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):
Since we have two dimensions, we can define another dimension to concatenate along. Let's combine the arrays along their columns:
What will be the output?
What will be the output?
What will be the output?
What will be the output?
What will be the output?
What will be the output?
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:
You can combine them along the first dimension (rows):
However, joining them along their columns would result in an inconsistent shape and is therefore not possible:
What will be the output?
What will be the output?
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.