Last modified: September 21, 2024

This article is written in: 🇺🇸

Creating Arrays with NumPy

NumPy, an abbreviation for Numerical Python, offers a powerful array object named ndarray. This object is a multi-dimensional array providing high-speed operations without the need for Python loops. In this guide, we will walk through various methods for creating NumPy arrays, from basic to advanced techniques.

Creating Arrays from Lists and Tuples

NumPy arrays can be created from both Python lists and tuples. Using the np.array() function, the process is seamless.

From a List

import numpy as np

# Creating an array from a list
arr_from_list = np.array([1, 2, 3, 4])
print(arr_from_list)
print(type(arr_from_list))

Expected output:

[1 2 3 4]
<class 'numpy.ndarray'="">

Explanation: - np.array([1, 2, 3, 4]) converts a Python list to a NumPy array. - The type() function confirms that the object is indeed a NumPy ndarray.

From a Tuple

# Creating an array from a tuple
arr_from_tuple = np.array((5, 6, 7, 8))
print(arr_from_tuple)
print(type(arr_from_tuple))

Expected output:

[5 6 7 8]
<class 'numpy.ndarray'="">

Explanation:

Initializing Arrays with Default Values

There are instances where initializing arrays with predefined values can be useful. NumPy provides functions like np.zeros(), np.ones(), and more for such cases.

Array of Zeros

# Initializing an array with zeros
zeros_arr = np.zeros((2, 3))
print(zeros_arr)

Expected output:

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

Explanation:

Array of Ones

# Initializing an array with ones
ones_arr = np.ones((2, 3))
print(ones_arr)

Expected output:

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

Explanation:

Generating Arrays with Random Values

Populating an array with random numbers can be especially handy during tasks like data simulation or initialization in machine learning algorithms.

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

Expected output (values will vary):

[[0.5488135  0.71518937 0.60276338]
 [0.54488318 0.4236548  0.64589411]]

Explanation:

Arrays with Evenly Spaced Values

Sometimes, you need an array with numbers evenly spaced between two endpoints. np.linspace() is the function for this purpose.

Using np.linspace()

# Creating an array with evenly spaced values
evenly_spaced_arr = np.linspace(1, 5, 9)
print(evenly_spaced_arr)

Expected output:

[1.  1.5 2.  2.5 3.  3.5 4.  4.5 5. ]

Explanation:

Creating Identity Matrix

An identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere. It is useful in various linear algebra computations.

Using np.eye()

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

Expected output:

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

Explanation:

Creating Arrays with Specific Sequences

Using np.arange()

# Creating an array with a specific sequence
sequence_arr = np.arange(0, 10, 2)
print(sequence_arr)

Expected output:

[0 2 4 6 8]

Explanation:

Summary Table

Method Function Description Example Code Example Output
From List np.array() Converts a list to a NumPy array. np.array([1, 2, 3, 4]) [1 2 3 4]
From Tuple np.array() Converts a tuple to a NumPy array. np.array((5, 6, 7, 8)) [5 6 7 8]
Array of Zeros np.zeros() Creates an array filled with zeros. np.zeros((2, 3)) [[0. 0. 0.] [0. 0. 0.]]
Array of Ones np.ones() Creates an array filled with ones. np.ones((2, 3)) [[1. 1. 1.] [1. 1. 1.]]
Random Values np.random.rand() Creates an array with random values between 0 and 1. np.random.rand(2, 3) [[0.54 0.71 0.60] [0.54 0.42 0.64]]
Evenly Spaced np.linspace() Creates an array with evenly spaced values between two endpoints. np.linspace(1, 5, 9) [1. 1.5 2. 2.5 3. 3.5 4. 4.5 5.]
Identity Matrix np.eye() Creates an identity matrix. np.eye(3) [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
Specific Sequence np.arange() Creates an array with a specific sequence. np.arange(0, 10, 2) [0 2 4 6 8]

Table of Contents

    Creating Arrays with NumPy
    1. Creating Arrays from Lists and Tuples
      1. From a List
      2. From a Tuple
    2. Initializing Arrays with Default Values
      1. Array of Zeros
      2. Array of Ones
    3. Generating Arrays with Random Values
    4. Arrays with Evenly Spaced Values
      1. Using np.linspace()
    5. Creating Identity Matrix
      1. Using np.eye()
    6. Creating Arrays with Specific Sequences
      1. Using np.arange()
    7. Summary Table