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.
import numpy as np
np.array([1, 2, 3, 4])
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:
import numpy as np
# create 1D array with 5 zeros
print(np.zeros(5))
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.
import numpy as np
# create 2D array
print(np.zeros((2,3)))
# create 3D array
print(np.zeros((2,2,2)))
np.ones(shape)
does the same as np.zeros(shape)
but fills the array with 1s.
import numpy as np
# create 1D array
print(np.ones(6))
# create 2D array
print(np.ones((2,3)))
# create 3D array
print(np.ones((2,2,2)))
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:
import numpy as np
# create array of ones
ones = np.ones((4,3))
# create array of zeros with same shape
zeros = np.zeros(ones.shape)
print(ones, zeros)
If you want to fill the array with any other number, you can use np.full(shape, num)
.
For example:
import numpy as np
# create 1D array of nines
print(np.full(3, 9))
# create 2D array of elevens
print(np.full((2,3), 11)))
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:
import numpy as np
# create 1D array with random numbers
print(np.random.sample(4))
# create 2D array with random numbers
print(np.random.sample((2,3)))
What will be the output?
import numpy as np
arr = np.zeros((5,4))
print(arr.shape)
What will be the output?
import numpy as np
arr = np.zeros((5,4))
print(arr.size)
What will be the output?
import numpy as np
a = np.array([
[1, 2, 3],
[4, 5, 6]
])
b = np.random.sample(a.shape)
print(b.shape)
Occasionally, you may need to combine two or more arrays.
To combine arrays, you can use the np.concatenate()
function:
np.concatenate((arr1, arr2, ...), axis=0)
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:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# concatenate a and b
c = np.concatenate((a,b))
print(c)
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):
import numpy as np
a = np.array([
[1, 2],
[3, 4]
])
b = np.array([
[5, 6],
[7, 8]
])
# concatenate a and b along rows
c = np.concatenate((a,b)) # axis = 0 (default)
print(c)
Since we have two dimensions, we can define another dimension to concatenate along. Let's combine the arrays along their columns:
import numpy as np
a = np.array([
[1, 2],
[3, 4]
])
b = np.array([
[5, 6],
[7, 8]
])
# concatenate a and b along columns
c = np.concatenate((a,b), axis = 1)
print(c)
What will be the output?
import numpy as np
a = np.ones(2)
b = np.zeros(2)
c = np.concatenate((a,b))
print(c)
What will be the output?
import numpy as np
a = np.ones((2,2))
b = np.zeros((2,2))
c = np.concatenate((a,b))
print(c)
What will be the output?
import numpy as np
a = np.ones((2,2))
b = np.zeros((2,2))
c = np.concatenate((a,b), axis=1)
print(c)
What will be the output?
import numpy as np
a = np.ones((3,2))
b = np.zeros((3,2))
c = np.concatenate((a,b))
print(c.shape)
What will be the output?
import numpy as np
a = np.ones((3,2))
b = np.zeros((3,2))
c = np.concatenate((a,b),axis=1)
print(c.shape)
What will be the output?
import numpy as np
a = np.ones((3,4,5))
b = np.zeros((3,4,5))
c = np.concatenate((a,b),axis=2)
print(c.shape)
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:
import numpy as np
a = np.array([
[1, 2],
[3, 4],
[5, 6]
])
b = np.array([
[7, 8],
[9, 10]
])
# what's the shape of a and b?
print(a.shape, b.shape)
You can combine them along the first dimension (rows):
import numpy as np
a = np.array([
[1, 2],
[3, 4],
[5, 6]
])
b = np.array([
[7, 8],
[9, 10]
])
# concatenate a and b?
print(np.concatenate((a,b)))
However, joining them along their columns would result in an inconsistent shape and is therefore not possible:
# inhomogeneous shape
[
[1 2 7 8]
[3 4 9 10]
[5 6]
]
What will be the output?
import numpy as np
a = np.ones((4,3))
b = np.zeros((5,3))
c = np.concatenate((a,b))
print(c.shape)
What will be the output?
import numpy as np
a = np.ones((4,3))
b = np.zeros((5,3))
c = np.concatenate((a,b),axis=1)
print(c.shape)
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.