MXNet NDArray - Convert A NumPy multidimensional array to an MXNet NDArray so that it retains the specific data type

MXNet NDArray - Convert A NumPy multidimensional array to an MXNet NDArray so that it retains the specific data type

We start by importing NumPy as np

```
import numpy as np
```

And then we print the NumPy version that we are using.

```
print(np.__version__)
```

We are using version 1.13.3.

Next, we import MXNet as mx

```
import mxnet as mx
```

And then we print the MX version that we are using.

```
print(mx.__version__)
```

We’re using MXNet version 0.12.1.

First, we’re going to create a NumPy example integer array using the np.array functionality.

```
numpy_ex_int_array = np.array([
[[1,2,3,4], [2,3,4,5], [3,4,5,6]],
[[4,5,6,7], [5,6,7,8], [6,7,8,9]]
], dtype=np.int32)
```

It’s going to be 1, 2, 3, 4, so on and so forth, and the data type that we are using is np.int32.

We can look at the shape of this example variable

```
numpy_ex_int_array.shape
```

And we see that it is 2x3x4.

Next, we check the data type of the numpy_ex_int_array

```
numpy_ex_int_array.dtype
```

And see that it is int32.

Finally, we can print it

```
print(numpy_ex_int_array)
```

And we see that it is in fact a 2x3x4 tensor or 2x3x4 multidimensional array.

To convert this NumPy multidimensional array to an MXNet NDArray, we’re going to use the mx.nd.array functionality and pass in our numpy_ex_int_array and then we assign that to the mx_ex_int_array Python variable.

```
mx_ex_int_array = mx.nd.array(numpy_ex_int_array)
```

Once we have this, we can check the shape

```
mx_ex_int_array.shape
```

And we see that it is in fact 2x3x4 which is what we would expect.

Next, we can check the data type

```
mx_ex_int_array.dtype
```

and see that it is numpy.float32.

Finally, we can print the converted array

```
print(mx_ex_int_array)
```

And we see that it is an MXNet NDArray that is 2x3x4 and the context is currently my CPU.

The second example that we’re going to do is we’re going to do a floating NumPy multidimensional array.

```
numpy_ex_float_array = np.array([
[[.1,.2,.3,.4], [.2,.3,.4,.5], [.3,.4,.5,.6]],
[[.4,.5,.6,.7], [.5,.6,.7,.8], [.6,.7,.8,.9]]
], dtype=np.float32)
```

We use the np.array functionality again.

This time there is a decimal point in front of all the numbers.

So 0.1, 0.2, 0.3, 0.4, so on and so forth, and we’re going to assign that to the Python variable, numpy_ex_float_array.

We can check the shape of this multidimensional array

```
numpy_ex_float_array.shape
```

And we see that it is 2x3x4.

Next, we can check the data type

```
numpy_ex_float_array.dtype
```

And see that it is float32 which is what we would expect given we defined it as np.float32.

Then we can print this multidimensional array.

```
print(numpy_ex_float_array)
```

Because it is a floating point number, we see 0.1, 0.2, 0.30000001.

So we see that they are all floating numbers for this multidimensional array.

Then to convert this NumPy multidimensional array to an MXNet NDArray, we use the mx.nd.array functionality and pass in our numpy_ex_float_array and assign it to the Python variable, mx_ex_float_array.

```
mx_ex_float_array = mx.nd.array(numpy_ex_float_array)
```

We can check the shape of this NDArray

```
mx_ex_float_array.shape
```

And we see that it is 2x3x4 which is what we expect.

Next, we can check the data type of our converted array

```
mx_ex_float_array.dtype
```

and see that it is a numpy.float32 data type.

Lastly, we can print this converted NumPy multidimensional array

```
print(mx_ex_float_array)
```

And we see that it is an MXNet NDArray that is 2x3x4.

The context is the CPU.

If we compare the numbers in the tensor, we see 0.1, 0.2, 0.3 with a bunch of zeros and then a 1 (0.30000001), we see 0.1, 0.2, 0.3 with a bunch of zeros and then a 1 (0.30000001).

So this is the way we convert a NumPy multidimensional array to an MXNet NDArray using the mx.nd.array functionality.

Receive the Data Science Weekly Newsletter every Thursday

Easy to unsubscribe at any time. Your e-mail address is safe.