Array Creation RoutinesΒΆ

NumPy provides a rich set of functions for creating arrays with specific shapes, types, and initial values. Mastering these routines is essential because they form the starting point for nearly every numerical computation. Whether you need an empty placeholder, a matrix of ones for masking, a diagonal identity matrix for linear algebra, or a range of evenly spaced values for plotting, there is a dedicated NumPy function for it. These exercises test your knowledge of np.empty(), np.ones(), np.zeros(), np.full(), np.eye(), np.arange(), np.linspace(), np.diag(), and more.

Ones and ZerosΒΆ

The np.zeros(), np.ones(), np.empty(), and np.full() functions create arrays filled with a specific value. Each accepts a shape tuple and an optional dtype. The _like variants (np.zeros_like(), np.ones_like(), np.full_like()) create arrays with the same shape and type as an existing array, which is convenient when you need a result array that matches your input dimensions.

import numpy as np

Create a new array of 2*2 integers, without initializing entries.

Let X = np.array([1,2,3], [4,5,6], np.int32). Create a new array with the same shape and type as X.

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

Create a 3-D array with ones on the diagonal and zeros elsewhere.

Create a new array of 3*2 float numbers, filled with ones.

Let x = np.arange(4, dtype=np.int64). Create an array of ones with the same shape and type as X.

x = np.arange(4, dtype=np.int64)

Create a new array of 3*2 float numbers, filled with zeros.

Let x = np.arange(4, dtype=np.int64). Create an array of zeros with the same shape and type as X.

x = np.arange(4, dtype=np.int64)

Create a new array of 2*5 uints, filled with 6.

Let x = np.arange(4, dtype=np.int64). Create an array of 6’s with the same shape and type as X.

x = np.arange(4, dtype=np.int64)

From Existing DataΒΆ

These functions convert existing Python objects (lists, tuples, other arrays) into NumPy arrays. np.array() is the most common, but np.asarray() avoids copying if the input is already an array. np.copy() creates an independent copy, and .item() extracts a single element as a Python scalar. Understanding when data is copied vs. shared is critical for memory management in large-scale data processing.

Create an array of [1, 2, 3].

Let x = [1, 2]. Convert it into an array.

x = [1,2]

Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix.

X = np.array([[1, 2], [3, 4]])

Let x = [1, 2]. Conver it into an array of float.

x = [1, 2]

Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30.

x = np.array([30])

Let x = np.array([1, 2, 3]). Create a array copy of x, which has a different id from x.

x = np.array([1, 2, 3])

Numerical RangesΒΆ

np.arange(), np.linspace(), and np.logspace() generate sequences of numbers. arange() uses a step size, linspace() uses a count of points (both endpoints inclusive), and logspace() spaces points evenly on a logarithmic scale. These are fundamental for creating axis values in plots, defining grid points for numerical methods, and generating test data with known distributions.

Create an array of 2, 4, 6, 8, …, 100.

Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive.

Create a 1-D array of 50 element spaced evenly on a log scale between 3. and 10., exclusive.

Building MatricesΒΆ

These exercises focus on constructing matrices with specific structures using np.diag(), np.diagflat(), np.tri(), and np.tril()/np.triu(). Diagonal and triangular matrices appear frequently in linear algebra – Cholesky decomposition produces lower-triangular matrices, and many iterative algorithms exploit triangular structure for efficient solving. Being able to construct these matrices programmatically is a practical skill for implementing numerical methods.

Let X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]). Get the diagonal of X, that is, [0, 5, 10].

X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])

Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0’s elsewhere.

Create an array which looks like below. array([[ 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0.]])

Create an array which looks like below. array([[ 0, 0, 0], [ 4, 0, 0], [ 7, 8, 0], [10, 11, 12]])

Create an array which looks like below. array([[ 1, 2, 3], [ 4, 5, 6], [ 0, 8, 9], [ 0, 0, 12]])