Array Creation Routines β SolutionsΒΆ
These are the solutions to the array creation exercises. Each solution demonstrates the NumPy function that best fits the task, and in some cases shows alternative approaches. Pay attention to how dtype is specified, how shape tuples are passed, and the difference between functions like np.zeros() (takes a shape) vs. np.zeros_like() (takes an existing array as a template).
Ones and ZerosΒΆ
Solutions for creating arrays filled with zeros, ones, and constant values using np.empty(), np.empty_like(), np.eye(), np.ones(), np.ones_like(), np.zeros(), np.zeros_like(), np.full(), and np.full_like().
import numpy as np
Create a new array of 2*2 integers, without initializing entries.
np.empty([2,2], int)
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)
np.empty_like(X)
Create a 3-D array with ones on the diagonal and zeros elsewhere.
np.eye(3)
np.identity(3)
Create a new array of 3*2 float numbers, filled with ones.
np.ones([3,2], float)
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)
np.ones_like(x)
Create a new array of 3*2 float numbers, filled with zeros.
np.zeros((3,2), float)
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)
np.zeros_like(x)
Create a new array of 2*5 uints, filled with 6.
np.full((2, 5), 6, dtype=np.uint)
np.ones([2, 5], dtype=np.uint) * 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)
np.full_like(x, 6)
np.ones_like(x) * 6
From Existing DataΒΆ
Solutions for converting Python objects to NumPy arrays using np.array(), np.asarray(), np.asmatrix(), np.asfarray(), np.asscalar(), and np.copy().
Create an array of [1, 2, 3].
np.array([1, 2, 3])
Let x = [1, 2]. Convert it into an array.
x = [1,2]
np.asarray(x)
Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix.
X = np.array([[1, 2], [3, 4]])
np.asmatrix(X)
Let x = [1, 2]. Conver it into an array of float.
x = [1, 2]
np.asfarray(x)
np.asarray(x, float)
Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30.
x = np.array([30])
np.asscalar(x)
x[0]
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])
y = np.copy(x)
print id(x), x
print id(y), y
Numerical RangesΒΆ
Solutions for generating sequences with np.arange(), np.linspace(), and np.logspace().
Create an array of 2, 4, 6, 8, β¦, 100.
np.arange(2, 101, 2)
Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive.
np.linspace(3., 10, 50)
Create a 1-D array of 50 element spaced evenly on a log scale between 3. and 10., exclusive.
np.logspace(3., 10., 50, endpoint=False)
Building MatricesΒΆ
Solutions for constructing structured matrices using np.diag(), np.diagflat(), np.tri(), np.tril(), and np.triu().
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]])
np.diag(X)
X.diagonal()
Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0βs elsewhere.
np.diagflat([1, 2, 3, 4])
Create an array which looks like below. array([[ 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0.]])
np.tri(3, 5, -1)
Create an array which looks like below. array([[ 0, 0, 0], [ 4, 0, 0], [ 7, 8, 0], [10, 11, 12]])
np.tril(np.arange(1, 13).reshape(4, 3), -1)
Create an array which looks like below. array([[ 1, 2, 3], [ 4, 5, 6], [ 0, 8, 9], [ 0, 0, 12]])
np.triu(np.arange(1, 13).reshape(4, 3), -1)