Last modified: May 16, 2025

This article is written in: 🇺🇸

Creating Arrays

NumPy, short for Numerical Python, is an important library for scientific and numerical computing in Python. It introduces the ndarray, a powerful multi-dimensional array object that allows for efficient storage and manipulation of large datasets. Unlike standard Python lists, NumPy arrays support vectorized operations, which significantly enhance performance, especially for mathematical computations.

Mathematical Foundations

Before we create arrays in NumPy, it helps to frame them in the language of mathematics:

Dimensionality Mathematical Object Notation Typical Size Symbol
0-D Scalar aR
1-D Vector vRn n
2-D Matrix ARm×n m,n
k-D Tensor TRn1×n2××nk n1,,nk

Vectorised code ≈ mathematical notation — concise, readable, and orders-of-magnitude faster.

Structure

Addition & Scalar Multiplication:

u+v,cv

closed under the usual vector-space axioms.

Inner/Dot Product (1-D):

u,v=i=1nuivi

measuring length and angles.

Matrix–Vector & Matrix–Matrix Product (2-D):

Av,AB

composing linear transformations or solving systems Ax=b.

Creating Arrays from Lists and Tuples

NumPy facilitates the conversion of Python lists and tuples into its own array format seamlessly. This interoperability ensures that you can leverage existing Python data structures while benefiting from NumPy's optimized performance for numerical operations.

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'="">

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'="">

Initializing Arrays with Default Values

Initializing arrays with predefined values is a fundamental step in many computational tasks. NumPy offers several functions to create arrays filled with specific default values, providing a solid foundation for further data manipulation and analysis.

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.]]

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.]]

Generating Arrays with Random Values

Creating arrays populated with random values is essential for simulations, statistical sampling, and initializing parameters in machine learning models. NumPy provides robust functions to generate arrays with different distributions of random numbers.

# 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]]

Arrays with Evenly Spaced Values

In many applications, it's necessary to generate arrays with numbers that are evenly spaced within a specific range. NumPy's linspace function is designed to create such sequences with precise control over the number of samples and the range.

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. ]

Creating Identity Matrix

An identity matrix is a special type of square matrix where all the elements on the main diagonal are ones, and all other elements are zeros. Identity matrices are fundamental in linear algebra, serving as the multiplicative identity in matrix operations.

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.]]

Creating Arrays with Specific Sequences

Generating arrays with specific numerical sequences is a common requirement in programming, especially when dealing with iterations, indexing, or setting up test cases. NumPy's arange function provides a straightforward method to create such sequences with defined start, stop, and step values.

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]

Summary Table

Method Function Description (incl. shape) Example Code Example Output
From List np.array() Converts a Python list to a 1-D array, shape (4,) np.array([1, 2, 3, 4]) [1 2 3 4]
From Tuple np.array() Converts a Python tuple to a 1-D array, shape (4,) np.array((5, 6, 7, 8)) [5 6 7 8]
Array of Zeros np.zeros() Initializes an array of zeros, shape (2, 3) np.zeros((2, 3)) [[0. 0. 0.] [0. 0. 0.]]
Array of Ones np.ones() Initializes an array of ones, shape (2, 3) np.ones((2, 3)) [[1. 1. 1.] [1. 1. 1.]]
Random Values np.random.rand() Uniform random floats in [0, 1), shape (2, 3) np.random.rand(2, 3) [[0.54 0.71 0.60] [0.54 0.42 0.64]]
Evenly Spaced np.linspace() 9 evenly spaced values from 1 to 5, shape (9,) np.linspace(1, 5, 9) [1. 1.5 2. 2.5 3. 3.5 4. 4.5 5.]
Identity Matrix np.eye() Identity matrix of order 3, shape (3, 3) np.eye(3) [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
Specific Sequence np.arange() Even numbers 0 ≤ n < 10, step 2, shape (5,) np.arange(0, 10, 2) [0 2 4 6 8]

Table of Contents

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