NumPy specializes in fast operations on arrays and parts of arrays.
import numpy as np
Make an array from a list.
np.array([1, 2, 3])
np.array([[1, 2], [3, 4]])
Make an array using a function.
np.zeros((2, 3))
np.ones((3, 2))
# Make an array by starting at 0, incrementing by 2 and ending before 10
np.arange(0, 10, 2)
# Make an array of four evenly spaced numbers including both endpoints
np.linspace(0, 1, 4)
# Make an array evenly spaced on a log scale
x = np.logspace(0, 1, 4)
# Show that the two arrays are equivalent
np.allclose(x, 10 ** np.linspace(0, 1, 4))
# Check execution speed
%timeit np.logspace(0, 1, 4)
%timeit 10 ** np.linspace(0, 1, 4)
Access elements of a 1-dimensional array.
x = np.arange(4)
print(x)
print(x[0]) # Get the first element
print(x[-1]) # Get the last element
Access elements of a multidimensional array.
x = np.arange(6).reshape(2, 3)
print(x)
print(x[-1]) # Get the last row
print(x[-1, -1]) # Get the last element of the last row
%timeit x[0, 0] # Faster
%timeit x[0][0]
Access slices of an array using colons.
x = np.arange(10)
print(x)
print(x[:3]) # Get the first three elements
print(x[-3:]) # Get the last three elements
print(x[1:4]) # Get some elements in the middle
print(x[::2]) # Get every second element
print(x[::3]) # Get every third element
x = np.arange(25).reshape(5, 5)
print(x)
print(x[:2, :2]) # Get the first two columns and the first two rows
print(x[::2, ::2]) # Get every second column of every second row
Access parts of an array with index arrays.
# Generate six random integers between 0 and 9 inclusive
x = np.random.randint(0, 9, 6)
print(x)
# Get the first number and the last two numbers
indices = [0, -2, -1]
print(x[indices])
# Make a random set of indices and use it for two arrays
array_length = 5
x1 = np.arange(array_length)
x2 = np.arange(array_length) * -1
indices = np.random.permutation(array_length)
print(x1[indices])
print(x2[indices])
# Use a multidimensional index array
x = np.random.randint(0, 9, 3)
indices = np.array([[0, 1], [1, 2]])
print('Candidates = %s' % x)
print(x[indices])
Filter arrays by element value with boolean index arrays.
x = np.arange(5)
print(x)
print(x > 2)
print(x[x > 2])
Include whole dimensions with a colon or ellipsis.
x = np.arange(16).reshape(2, 2, 2, 2)
print(x)
print(x[0])
print(x[0, 0])
print(x[0, 0, 0])
print(x[0, 0, 0, 0])
print(x[0, 0, 1, 0])
print(x[0, 0, :, 0])
print(x[0, 1, 0, 0])
print(x[0, 1, 1, 0])
print(x[0, 1, :, 0])
print(x[0, :, :, 0])
print(x[0, ..., 0])
Build indices programmatically.
x = np.arange(16).reshape(2, 2, 2, 2)
indices = [0, slice(x.shape[1]), slice(x.shape[2]), 0]
print(x[indices]) # x[0, :, :, 0]
indices = [0, Ellipsis, 0]
print(x[indices]) # x[0, ..., 0]
x = np.zeros((5, 5))
print(x)
x[1:4, 1:4] = 1
print(x)
x = np.array([1, 2])
y = np.array([3, 4])
print('Concatenate along the first axis.')
print(np.r_[x, y])
print('Concatenate along the second axis.')
print(np.c_[x, y])
Broadcast a scalar over a matrix.
x = np.arange(9).reshape(3, 3)
print(x)
print(x + 10)
Broadcast a vector over a matrix.
y = np.ones(3)
print(y)
x + y
Broadcast scalars over vectors.
x = np.arange(5)
print(x)
print(x[:, None]) # Add a dimension
print(x[None, :]) # Add a dimension
print(x[:, None] + x[None, :])
x = np.arange(4)
print(x)
y = x.reshape(2, 2)
print(y)
print(y.ravel())
print(y.sum())
print(y.sum(axis=0))
print(y.sum(axis=1))
print(y.mean())
print(y.mean(axis=0))
print(y.mean(axis=1))
print(y.std())
print(y.std(axis=0))
print(y.std(axis=1))
np.dot([1, 2], [3,4])
file_path = '/tmp/matrix.npy'
# Save an array in .npy format
np.save(file_path, [1, 2, 3])
# Load an array in .npy format
np.load(file_path)
file_path = '/tmp/matrix.npz'
# Save multiple arrays in .npz format
np.savez(file_path, a=[1, 2], b=[3, 4])
# Load multiple arrays in .npz format
np.load(file_path).items()
file_path = '/tmp/matrix.hdf5'
# Save huge arrays in hdf5 format
import h5py
h5py_file = h5py.File(file_path, 'w')
h5py_file.create_dataset('a', data=[1, 2])
h5py_file.create_dataset('b', data=[3, 4])
h5py_file.close()
# Load huge arrays in hdf5 format
h5py_file = h5py.File(file_path, 'r')
print(h5py_file.items())
h5py_file.close()
ll -h /tmp/matrix.*