Introduction to Computational Analysis




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

NumPy is for multidimensional array operations

NumPy specializes in fast operations on arrays and parts of arrays.

In [ ]:
import numpy as np

Make an array

Make an array from a list.

In [ ]:
np.array([1, 2, 3])
In [ ]:
np.array([[1, 2], [3, 4]])

Make an array using a function.

In [ ]:
np.zeros((2, 3))
In [ ]:
np.ones((3, 2))
In [ ]:
# Make an array by starting at 0, incrementing by 2 and ending before 10
np.arange(0, 10, 2)
In [ ]:
# Make an array of four evenly spaced numbers including both endpoints
np.linspace(0, 1, 4)
In [ ]:
# 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))
In [ ]:
# Check execution speed
%timeit np.logspace(0, 1, 4)
%timeit 10 ** np.linspace(0, 1, 4)

Access parts of an array by indexing

Access elements of a 1-dimensional array.

In [ ]:
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.

In [ ]:
x = np.arange(6).reshape(2, 3)
print(x)
In [ ]:
print(x[-1])      # Get the last row
print(x[-1, -1])  # Get the last element of the last row
In [ ]:
%timeit x[0, 0]  # Faster
%timeit x[0][0]

Access slices of an array using colons.

In [ ]:
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
In [ ]:
print(x[::2])  # Get every second element
print(x[::3])  # Get every third element
In [ ]:
x = np.arange(25).reshape(5, 5)
print(x)
In [ ]:
print(x[:2, :2])    # Get the first two columns and the first two rows
In [ ]:
print(x[::2, ::2])  # Get every second column of every second row

Access parts of an array with index arrays.

In [ ]:
# Generate six random integers between 0 and 9 inclusive
x = np.random.randint(0, 9, 6) 
print(x)
In [ ]:
# Get the first number and the last two numbers
indices = [0, -2, -1]
print(x[indices])
In [ ]:
# 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])
In [ ]:
# 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.

In [ ]:
x = np.arange(5)
print(x)
In [ ]:
print(x > 2)
In [ ]:
print(x[x > 2])

Include whole dimensions with a colon or ellipsis.

In [ ]:
x = np.arange(16).reshape(2, 2, 2, 2)
print(x)
In [ ]:
print(x[0])
In [ ]:
print(x[0, 0])
In [ ]:
print(x[0, 0, 0])
In [ ]:
print(x[0, 0, 0, 0])
print(x[0, 0, 1, 0])
print(x[0, 0, :, 0])
In [ ]:
print(x[0, 1, 0, 0])
print(x[0, 1, 1, 0])
print(x[0, 1, :, 0])
In [ ]:
print(x[0, :, :, 0])
In [ ]:
print(x[0, ..., 0])

Build indices programmatically.

In [ ]:
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]

Change parts of an array by indexing

In [ ]:
x = np.zeros((5, 5))
print(x)
In [ ]:
x[1:4, 1:4] = 1
print(x)

Combine arrays by concatenating

In [ ]:
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])

Combine arrays of different dimensions by broadcasting

Broadcast a scalar over a matrix.

In [ ]:
x = np.arange(9).reshape(3, 3)
print(x)
In [ ]:
print(x + 10)

Broadcast a vector over a matrix.

In [ ]:
y = np.ones(3)
print(y)
In [ ]:
x + y

Broadcast scalars over vectors.

In [ ]:
x = np.arange(5)
print(x)
In [ ]:
print(x[:, None])  # Add a dimension
In [ ]:
print(x[None, :])  # Add a dimension
In [ ]:
print(x[:, None] + x[None, :])

Process arrays

In [ ]:
x = np.arange(4)
print(x)
In [ ]:
y = x.reshape(2, 2)
print(y)
In [ ]:
print(y.ravel())
In [ ]:
print(y.sum())
print(y.sum(axis=0))
print(y.sum(axis=1))
In [ ]:
print(y.mean())
print(y.mean(axis=0))
print(y.mean(axis=1))
In [ ]:
print(y.std())
print(y.std(axis=0))
print(y.std(axis=1))
In [ ]:
np.dot([1, 2], [3,4])

Save and load arrays

In [ ]:
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)
In [ ]:
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()
In [ ]:
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()
In [ ]:
ll -h /tmp/matrix.*