The NP on MXNet cheat sheet

To begin, import the np and npx module and update MXNet to run in NumPy-like mode.

from mxnet import np, npx
npx.set_np()  # Change MXNet to the numpy-like mode.

NDArray figure (TODO)

Creating arrays

np.array([1, 2, 3])  # default datatype is float32
np.array([(1.5, 2, 3), (4, 5, 6)], dtype='float16')
np.array([[(15,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype='int32')

Initial placeholders

np.zeros((3, 4))  # Create an array of zeros
np.ones((2, 3, 4), dtype='int8')  # Create an array of ones
np.arange(10, 25, 5)  # Create an array of evenly spaced values (step value)
# Create an array of evenly spaced values (number of samples)
# np.linspace(0, 2, 9)
# np.full((2, 2), 7)  # Create a constant array
# np.eye(2)  # Create a 2X2 identity matrix
# np.random.random((2, 2))  # Create an array with random values
np.empty((3,2))  # Create an empty array

I/O

Saving and loading on disk

# Save one array
a = np.array([1, 2, 3])
npx.save('my_array', a)
npx.load('my_array')
# Save a list of arrays
b = np.array([4, 6, 8])
npx.savez('my_arrays', *[a, b])
npx.load('my_arrays')

Saving and loading text files

# np.loadtxt("myfile.txt")
# np.genfromtxt("my_file.csv", delimiter=',')
# np.savetxt("myarray.txt", a, delimiter=" ")

Data types

# np.int64    # Signed 64-bit integer types
# np.float32  # Standard double-precision floating point
# np.complex  # Complex numbers represented by 128 floats
# np.bool     # Boolean type storing TRUE and FALSE values
# np.object   # Python object type
# np.string_  # Fixed-length string type
# np.unicode_ # Fixed-length unicode type

Inspecting your array

a.shape # Array dimensions
len(a) # Length of array
b.ndim # Number of array dimensions
b.size # Number of array elements
b.dtype # Data type of array elements
# b.dtype.name # Name of data type
b.astype('int') # Convert an array to a different type

Asking For Help

# np.info(np.ndarray.dtype)

Array mathematics

Arithmetic operations

a - b # Subtraction
np.subtract(a, b) # Subtraction
b + a # Addition
np.add(b, a) # Addition
a / b # Division
np.divide(a,b) # Division
a * b # Multiplication
np.multiply(a, b) # Multiplication
np.exp(b) # Exponentiation
np.sqrt(b) # Square root
np.sin(a) # Sines of an array
np.cos(b) # Element-wise cosine
np.log(a) # Element-wise natural logarithm
a.dot(b) # Dot product

Comparison

Aggregate functions

a.sum() # Array-wise sum
# a.min() # Array-wise minimum value
c = np.array(([[1,2,3], [2,3,4]]))
# c.max(axis=0) # Maximum value of an array row
# c.cumsum(axis=1) # Cumulative sum of the elements
a.mean() # Mean
# b.median() # Median
# a.corrcoef() # Correlation coefficient
# np.std(b) # Standard deviation

Copying arrays

# a.view() # Create a view of the array with the same data
np.copy(a) # Create a copy of the array
a.copy() # Create a deep copy of the array

Sorting Arrays

# a.sort() # Sort an array
# c.sort(axis=0) # Sort the elements of an array's axis

Subsetting, slicing, indexing

Subsetting

a[2] # Select the element at the 2nd index 3
c[0,1] # Select the element at row 1 column 2

Slicing

a[0:2] # Select items at index 0 and 1
c[0:2,1] # Select items at rows 0 and 1 in column 1
c[:1] # Select all items at row 0
# c[1,...] # Same as [1,:,:]
a[ : :-1] #Reversed array a array([3, 2, 1])

Boolean Indexing

# a[a<2] # Select elements from a less than 2

Fancy indexing

c[[1,0,1,0], [0,1,2,0]] # Select elements (1,0),(0,1),(1,2) and (0,0)
c[[1,0,1,0]][:,[0,1,2,0]] # Select a subset of the matrix’s rows

Array manipulation

Transposing array

np.transpose(c) # Permute array dimensions
c.T # Permute array dimensions

Changing array shape

# b.ravel() # Flatten the array
# c.reshape(3,-2) # Reshape, but don’t change data

Adding and removing elements

# c.resize((6,2)) # Return a new array with shape (6, 2)
# np.append(h,g) # Append items to an array
# np.insert(a, 1, 5) # Insert items in an array
# np.delete(a, [1]) # Delete items from an array

Combining arrays

np.concatenate((a,b),axis=0) # Concatenate arrays
# np.vstack((a,b)) # Stack arrays vertically (row-wise)
# np.r_[e,f] # Stack arrays vertically (row-wise)
# np.hstack((e,f)) # Stack arrays horizontally (column-wise)
# np.column_stack((a,d)) # Create stacked column-wise arrays
# np.c_[a,d] # Create stacked column-wise arrays

Splitting arrays

# np.hsplit(a,3) # Split the array horizontally at the 3rd index
# np.vsplit(c,2) # Split the array vertically at the 2nd index

Use GPUs

Prerequisites: A GPU exists and GPU-enabled MXNet is installed.

npx.num_gpus()  # Query number of GPUs
npx.gpu(0), npx.gpu(1)  # Context for the first and second GPUs
gpu_0 = npx.gpu(0) if npx.num_gpus() > 1 else npx.cpu()
g0 = np.zeros((2,3), device=gpu_0)  # Create array on GPU 0
g0
gpu_1 = npx.gpu(1) if npx.num_gpus() > 2 else npx.cpu()
g1 = np.random.uniform(size=(2,3), device=gpu_1)  # Create array on GPU 1
g1
# Copy to another GPU
g1.copyto(gpu_0)
# Return itself if matching the device, otherwise copy
g1.copyto(gpu_0), g1.copyto(gpu_0)
g1.device  # Query the device an array is on
## The computation is performed by the devices on which the input arrays are
g0 + g1.copyto(gpu_0)

Auto differentiation

a.attach_grad() # Allocate gradient for a variable
a.grad # access the gradient

Compute the $\nabla_a b=\exp(2a)^T a$

from mxnet import autograd

with autograd.record():
    b = np.exp(2*a).dot(a)
b.backward()
a.grad

Acknowledgement

Adapted from www.datacamp.com.