NumPy Creating Arrays

Introduction

In this chapter, we will explore how to create arrays using NumPy. Arrays are the central data structure in NumPy, and understanding how to create and manipulate them is essential for effective numerical computing.

Importing NumPy

Before you start creating arrays, you need to import the NumPy library. It is common practice to import NumPy with the alias np:

import numpy as np

Creating Arrays from Lists

You can create a NumPy array from a Python list using the np.array function.

Example: Creating a 1D Array

import numpy as np

# Creating a 1D array from a list
arr = np.array([1, 2, 3, 4, 5])
print(arr)

Output:

[1 2 3 4 5]

Example: Creating a 2D Array

import numpy as np

# Creating a 2D array from a list of lists
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

Output:

[[1 2 3]
 [4 5 6]]

Creating Arrays with Initial Values

NumPy provides several functions to create arrays with initial placeholder values.

Example: Creating an Array of Zeros

import numpy as np

# Creating an array of zeros
zeros = np.zeros((3, 3))
print(zeros)

Output:

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

Example: Creating an Array of Ones

import numpy as np

# Creating an array of ones
ones = np.ones((2, 4))
print(ones)

Output:

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

Example: Creating an Array with a Constant Value

import numpy as np

# Creating an array with a constant value
full = np.full((3, 3), 7)
print(full)

Output:

[[7 7 7]
 [7 7 7]
 [7 7 7]]

Creating Arrays with a Range of Values

NumPy provides functions to create arrays with a range of values.

Example: Creating an Array with arange

import numpy as np

# Creating an array with a range of values
arr = np.arange(0, 10, 2)  # Start at 0, end before 10, step by 2
print(arr)

Output:

[0 2 4 6 8]

Example: Creating an Array with linspace

import numpy as np

# Creating an array with linearly spaced values
arr = np.linspace(0, 1, 5)  # 5 values from 0 to 1
print(arr)

Output:

[0.   0.25 0.5  0.75 1.  ]

Creating Arrays with Random Values

NumPy provides functions to create arrays with random values.

Example: Creating an Array with Random Values

import numpy as np

# Creating an array with random values
random_arr = np.random.rand(3, 3)
print(random_arr)

Output:

[[0.5488135  0.71518937 0.60276338]
 [0.54488318 0.4236548  0.64589411]
 [0.43758721 0.891773   0.96366276]]

Example: Creating an Array with Random Integers

import numpy as np

# Creating an array with random integers
random_ints = np.random.randint(0, 10, (3, 3))  # Random integers from 0 to 9
print(random_ints)

Output:

[[5 0 3]
 [3 7 9]
 [3 5 2]]

Creating Identity Matrices

An identity matrix is a square matrix with ones on the diagonal and zeros elsewhere.

Example: Creating an Identity Matrix

import numpy as np

# Creating an identity matrix
identity = np.eye(4)
print(identity)

Output:

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

Reshaping Arrays

You can change the shape of an array using the reshape function.

Example: Reshaping a 1D Array to a 2D Array

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

# Reshaping to a 2D array
reshaped_arr = arr.reshape((2, 3))
print(reshaped_arr)

Output:

[[1 2 3]
 [4 5 6]]

Conclusion

Creating arrays is a fundamental skill in NumPy, as arrays are the primary data structure used for numerical computations. This chapter covered various methods to create arrays, including from lists, with initial values, with ranges of values, with random values, and reshaping arrays.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top