Mastering NumPy: Part 1 (Array Creation)

Nandeda Narayan
10 min readMay 24, 2023

--

This is the 2nd blog post in our comprehensive series (Access all posts of this series here) on NumPy!

Note: Don’t forget to bookmark the full 50 examples series — Mastering NumPy: A 5 Parts Series to Master Numpy

Introduction

In the world of data manipulation, creating arrays is often the first step towards unlocking the power of NumPy. Whether you need to generate random data, create specific patterns, or convert existing data structures into arrays, NumPy provides an array of functions to facilitate array creation.

In this blog post, we will explore 10 practical examples of array creation using NumPy, covering various scenarios and techniques.

1. Create n-dimensional array using numpy.array()

One-Dimensional Array: To create a one-dimensional array, you can pass a Python list or tuple as an argument to numpy.array(). Here's an example:

import numpy as np

# Create a one-dimensional array from a list
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1)
# Output: [1 2 3 4 5]

# Create a one-dimensional array from a tuple
arr2 = np.array((6, 7, 8, 9, 10))
print(arr2)
# Output: [ 6 7 8 9 10]

Two-Dimensional Array: To create a two-dimensional array, you can pass a nested list or a tuple of lists to numpy.array(). Here's an example:

import numpy as np

# Create a two-dimensional array from a nested list
arr3 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr3)
# Output:
# [[1 2 3]
# [4 5 6]]

# Create a two-dimensional array from a tuple of lists
arr4 = np.array(([7, 8, 9], [10, 11, 12]))
print(arr4)
# Output:
# [[ 7 8 9]
# [10 11 12]]

N-Dimensional Array: To create an n-dimensional array, you can pass nested lists or tuples to numpy.array(). Each level of nesting represents a dimension. Here's an example of creating a three-dimensional array:

import numpy as np

# Create a three-dimensional array from nested lists
arr5 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr5)
# Output:
# [[[ 1 2 3]
# [ 4 5 6]]
#
# [[ 7 8 9]
# [10 11 12]]]

# Create a three-dimensional array from nested tuples
arr6 = np.array((((1, 2, 3), (4, 5, 6)), ((7, 8, 9), (10, 11, 12))))
print(arr6)
# Output:
# [[[ 1 2 3]
# [ 4 5 6]]
#
# [[ 7 8 9]
# [10 11 12]]]

You can extend this pattern to create arrays with higher dimensions by adding more levels of nesting in the lists or tuples passed to numpy.array().

2. Generating a random n-dimensional array using numpy.random.rand()

To generate a random n-dimensional array using numpy.random.rand(), you can specify the desired shape of the array as arguments to the function. Each argument represents the size of a dimension. Here's an example:

import numpy as np

# Generate a random one-dimensional array of size 5
arr1 = np.random.rand(5)
print(arr1)
# Output: [0.15874053 0.44624684 0.75599155 0.02987582 0.60988211]

# Generate a random two-dimensional array of size 3x4
arr2 = np.random.rand(3, 4)
print(arr2)
# Output:
# [[0.71174805 0.45297981 0.44254204 0.4064917 ]
# [0.21969794 0.35210944 0.59062307 0.07150712]
# [0.81421881 0.48293367 0.7450353 0.90812841]]

# Generate a random three-dimensional array of size 2x3x4
arr3 = np.random.rand(2, 3, 4)
print(arr3)
# Output:
# [[[0.08482633 0.86020764 0.62325012 0.36515048]
# [0.27499426 0.66428432 0.21667982 0.72208236]
# [0.82998224 0.74818181 0.13246212 0.05748734]]
#
# [[0.73338497 0.56415953 0.19009826 0.98622494]
# [0.60764863 0.28710149 0.74842579 0.47887174]
# [0.60206446 0.19808553 0.29593465 0.2859526 ]]]

In each case, numpy.random.rand() generates random numbers between 0 and 1, following a uniform distribution, and creates an array with the specified shape. The generated values are pseudorandom and can be different each time you run the code.

3. To generate the same random values every time you run the code

You need to set a seed value for the random number generator in NumPy. Setting the seed ensures that the sequence of random numbers generated remains the same across different runs of the code. Here’s an example:

import numpy as np

# Set the seed value
np.random.seed(123)

# Generate a random one-dimensional array of size 5
arr1 = np.random.rand(5)
print(arr1)
# Output: [0.69646919 0.28613933 0.22685145 0.55131477 0.71946897]

By setting np.random.seed(123) before generating the random numbers, the generated values will be the same every time you run the code. Feel free to change the seed value to any other integer to obtain a different sequence of random numbers.

4. Creating an array of zeros using numpy.zeros()

To create an array of zeros using numpy.zeros(), you can specify the desired shape of the array as an argument to the function. The function will create an array filled with zeros of the specified shape.

import numpy as np

# Create a one-dimensional array of zeros with size 5
arr1 = np.zeros(5)
print(arr1)
# Output: [0. 0. 0. 0. 0.]

# Create a two-dimensional array of zeros with size 3x4
arr2 = np.zeros((3, 4))
print(arr2)
# Output:
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]

# Create a three-dimensional array of zeros with size 2x3x4
arr3 = np.zeros((2, 3, 4))
print(arr3)
# Output:
# [[[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]
#
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]]

In each case, numpy.zeros() creates an array filled with zeros, and the shape of the array is determined by the argument passed to the function. The resulting array will have the specified dimensions, and all the elements will be set to zero.

5. Creating an identity matrix using numpy.eye()

To create an identity matrix using numpy.eye(), you can specify the size or shape of the identity matrix as arguments to the function. The identity matrix is a square matrix with ones on the diagonal and zeros elsewhere.

import numpy as np

# Create a 3x3 identity matrix
identity_matrix_3x3 = np.eye(3)
print(identity_matrix_3x3)
# Output:
# [[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]

# Create a 4x4 identity matrix
identity_matrix_4x4 = np.eye(4)
print(identity_matrix_4x4)
# Output:
# [[1. 0. 0. 0.]
# [0. 1. 0. 0.]
# [0. 0. 1. 0.]
# [0. 0. 0. 1.]]

# Create a 2x5 identity matrix
identity_matrix_2x5 = np.eye(2, 5)
print(identity_matrix_2x5)
# Output:
# [[1. 0. 0. 0. 0.]
# [0. 1. 0. 0. 0.]]

In each case, numpy.eye() creates an identity matrix with the specified size or shape. The diagonal elements of the matrix are set to 1, and all other elements are set to 0. The resulting matrix will have the same number of rows and columns as specified in the arguments, with ones on the diagonal and zeros elsewhere.

6. Creating an array with a range of values using numpy.arange()

To create an array with a range of values using numpy.arange(), you can specify the start, stop, and step size as arguments to the function. The function will generate a one-dimensional array with values ranging from the start value to the stop value (exclusive) in increments defined by the step size.

import numpy as np

# Create an array with values from 0 to 4 (exclusive)
arr1 = np.arange(5)
print(arr1)
# Output: [0 1 2 3 4]

# Create an array with values from 2 to 10 (exclusive) in steps of 2
arr2 = np.arange(2, 10, 2)
print(arr2)
# Output: [2 4 6 8]

# Create an array with values from 1 to 10 (exclusive) in steps of 1.5
arr3 = np.arange(1, 10, 1.5)
print(arr3)
# Output: [1. 2.5 4. 5.5 7. 8.5]

# Create an array with values from 5 to 1 (exclusive) in steps of -1
arr4 = np.arange(5, 1, -1)
print(arr4)
# Output: [5 4 3 2]

In each case, numpy.arange() generates an array with values ranging from the start value (inclusive) to the stop value (exclusive) in increments defined by the step size. The resulting array will have a length determined by the range of values and the step size specified in the arguments.

7. Creating an array with evenly spaced values using numpy.linspace()

To create an array with evenly spaced values using numpy.linspace(), you can specify the start, stop, and the number of values you want in the array. The function will generate a one-dimensional array with values evenly spaced between the start and stop values

import numpy as np

# Create an array with 5 evenly spaced values from 0 to 1 (inclusive)
arr1 = np.linspace(0, 1, 5)
print(arr1)
# Output: [0. 0.25 0.5 0.75 1. ]

# Create an array with 10 evenly spaced values from 2 to 4 (inclusive)
arr2 = np.linspace(2, 4, 10)
print(arr2)
# Output: [2. 2.22222222 2.44444444 2.66666667 2.88888889 3.11111111 3.33333333 3.55555556 3.77777778 4. ]

# Create an array with 3 evenly spaced values from 1 to 2 (inclusive)
arr3 = np.linspace(1, 2, 3)
print(arr3)
# Output: [1. 1.5 2. ]

# Create an array with 6 evenly spaced values from 5 to 1 (inclusive)
arr4 = np.linspace(5, 1, 6)
print(arr4)
# Output: [5. 4. 3. 2. 1.]

In each case, numpy.linspace() generates an array with evenly spaced values between the start and stop values. The number of values in the resulting array is determined by the third argument, which specifies the total number of values you want in the array. The resulting array will have values evenly distributed between the start and stop values, inclusive of both.

8. Creating a multidimensional array using numpy.reshape()

To create a multidimensional array using numpy.reshape(), you can start with an existing array and specify the desired shape of the new array as an argument to the function. The new shape should be compatible with the total number of elements in the original array

import numpy as np

# Create a one-dimensional array with values from 1 to 12
arr1 = np.arange(1, 13)
print(arr1)
# Output: [ 1 2 3 4 5 6 7 8 9 10 11 12]

# Reshape the one-dimensional array into a two-dimensional array of size 3x4
arr2 = np.reshape(arr1, (3, 4))
print(arr2)
# Output:
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]

# Reshape the one-dimensional array into a three-dimensional array of size 2x2x3
arr3 = np.reshape

In each case, numpy.reshape() transforms the original array into a new array with the specified shape. The resulting array will have the same total number of elements as the original array. The order of the elements in the original array determines the order of elements in the reshaped array. The reshaped array will have dimensions and sizes according to the new shape specified in the arguments.

9. Creating an array from a Python list using numpy.asarray()

To create an array from a Python list using numpy.asarray(), you can pass the list as an argument to the function. The function will convert the list into a NumPy array.

import numpy as np

# Create a Python list
my_list = [1, 2, 3, 4, 5]

# Convert the list to a NumPy array
arr = np.asarray(my_list)
print(arr)
# Output: [1 2 3 4 5]

In this example, numpy.asarray() takes the my_list Python list as an argument and returns a NumPy array arr containing the same elements. The resulting array will have the same shape and data type as the input list.

10. Creating a boolean array using numpy.full() and logical conditions

To create a boolean array using numpy.full() and logical conditions, you can use the logical operators (<, <=, >, >=, ==, !=, etc.) to define the condition and then pass the condition to numpy.full() to create the boolean array.

import numpy as np

# Create a boolean array based on a condition using numpy.full()
arr1 = np.full((5,), True) # Create a boolean array of size 5, filled with True
print(arr1)
# Output: [True True True True True]

# Create a boolean array based on a condition using logical operators
my_list = [1, 2, 3, 4, 5]
arr2 = np.array(my_list) > 3 # Create a boolean array based on the condition "element > 3"
print(arr2)
# Output: [False False False True True]

In the first example, numpy.full() is used to create a boolean array arr1 of size 5, filled with True. This can be useful when you want to initialize an array with a specific boolean value.

In the second example, a Python list my_list is converted to a NumPy array using numpy.array(). Then, a condition is defined using the logical operator >, creating a boolean array arr2 where each element is True if the corresponding element in my_list is greater than 3, and False otherwise.

Bonus Example: Creating a repeating pattern of values using numpy.tile()

To create a repeating pattern of values using numpy.tile(), you can specify the array or list of values you want to repeat and the number of repetitions in each dimension. The function will replicate the values in the desired pattern

import numpy as np

# Create a repeating pattern using numpy.tile()
pattern1 = np.tile([1, 2, 3], 3)
print(pattern1)
# Output: [1 2 3 1 2 3 1 2 3]

# Create a repeating pattern with a 2D array using numpy.tile()
pattern2 = np.tile([[1, 2], [3, 4]], (2, 3))
print(pattern2)
# Output:
# [[1 2 1 2 1 2]
# [3 4 3 4 3 4]
# [1 2 1 2 1 2]
# [3 4 3 4 3 4]]

# Create a repeating pattern with a 3D array using numpy.tile()
pattern3 = np.tile([[[1, 2], [3, 4]]], (2, 3, 2))
print(pattern3)
# Output:
# [[[1 2 1 2]
# [3 4 3 4]
# [1 2 1 2]
# [3 4 3 4]
# [1 2 1 2]
# [3 4 3 4]]
#
# [[1 2 1 2]
# [3 4 3 4]
# [1 2 1 2]
# [3 4 3 4]
# [1 2 1 2]
# [3 4 3 4]]]

In each case, numpy.tile() creates a repeating pattern of values based on the specified input and repetition pattern. The resulting array will have the shape determined by the number of repetitions in each dimension. You can adjust the input values and repetition pattern to generate different repeating patterns as needed.

Conclusion

In this blog post, we delved into the array creation capabilities of NumPy. We covered different methods to create arrays, ranging from simple one-dimensional arrays to complex multidimensional structures. By mastering array creation techniques, you can effectively prepare your data for subsequent analysis and manipulation with NumPy.

Next Blog Post

In the next blog post Mastering NumPy: Part 2 (Array Manipulation), we will explore the powerful array manipulation features offered by NumPy. We will learn how to reshape, slice, concatenate, and split arrays to transform and organize our data efficiently.

Note: Don’t forget to bookmark the full 50 examples series — Mastering NumPy: A 5 Parts Series to Master Numpy

--

--