Step 1: Manipulate data with NP on MXNet

This getting started exercise introduces the np package, which is the primary tool for storing and transforming data on MXNet. If you’ve worked with NumPy before, you’ll notice np is, by design, similar to NumPy.

Import packages and create an array

To get started, run the following commands to import the np package together with the NumPy extensions package npx. Together, np with npx make up the NP on MXNet front end.

from mxnet import np, npx
npx.set_np()  # Activate NumPy-like mode.

In this step, create a 2D array (also called a matrix). The following code example creates a matrix with values from two sets of numbers: 1, 2, 3 and 4, 5, 6. This might also be referred to as a tuple of a tuple of integers.

np.array(((1,2,3),(5,6,7)))
array([[1., 2., 3.],
       [5., 6., 7.]])

You can also create a very simple matrix with the same shape (2 rows by 3 columns), but fill it with 1s.

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

You can create arrays whose values are sampled randomly. For example, sampling values uniformly between -1 and 1. The following code example creates the same shape, but with random sampling.

y = np.random.uniform(-1,1, (2,3))
y
array([[0.09762704, 0.18568921, 0.43037868],
       [0.6885315 , 0.20552671, 0.71589124]])

As with NumPy, the dimensions of each ndarray are shown by accessing the .shape attribute. As the following code example shows, you can also query for size, which is equal to the product of the components of the shape. In addition, .dtype tells the data type of the stored values.

(x.shape, x.size, x.dtype)
((2, 3), 6, dtype('float32'))

Performing operations on an array

An ndarray supports a large number of standard mathematical operations. Here are three examples. You can perform element-wise multiplication by using the following code example.

x * y
array([[0.09762704, 0.18568921, 0.43037868],
       [0.6885315 , 0.20552671, 0.71589124]])

You can perform exponentiation by using the following code example.

np.exp(y)
array([[1.1025515, 1.204048 , 1.5378398],
       [1.9907899, 1.2281718, 2.0460093]])

You can also find a matrix’s transpose to compute a proper matrix-matrix product by using the following code example.

np.dot(x, y.T)
array([[0.71369493, 1.6099495 ],
       [0.71369493, 1.6099495 ]])

Indexing an array

The ndarrays support slicing in many ways you might want to access your data. The following code example shows how to read a particular element, which returns a 1D array with shape (1,).

y[1,2]
array(0.71589124)

This example shows how to read the second and third columns from y.

y[:,1:3]
array([[0.18568921, 0.43037868],
       [0.20552671, 0.71589124]])

This example shows how to write to a specific element.

y[:,1:3] = 2
y
array([[0.09762704, 2.        , 2.        ],
       [0.6885315 , 2.        , 2.        ]])

You can perform multi-dimensional slicing, which is shown in the following code example.

y[1:2,0:2] = 4
y
array([[0.09762704, 2.        , 2.        ],
       [4.        , 4.        , 2.        ]])

Converting between MXNet ndarrays and NumPy ndarrays

You can convert MXNet ndarrays to and from NumPy ndarrays, as shown in the following example. The converted arrays do not share memory.

a = x.asnumpy()
(type(a), a)
(numpy.ndarray, array([[1., 1., 1.],
        [1., 1., 1.]], dtype=float32))
np.array(a)
array([[1., 1., 1.],
       [1., 1., 1.]])

Next steps

Learn how to construct a neural network with the Gluon module: Step 2: Create a neural network.