Skip to content

Use a Numpy array as output

A common data structure in Data Science is the NumPy Array. You cannot directly use them as an output, some conversion is needed.

There are two options: - A 1-dimensional Array can be passed on as a string - A multi-dimensional array should be passed as a Blob (file) .

Convert to string

Numpy has a built in functionality which you can use.

It can be as simple as:

import numpy as np

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

string_array = np.array2string(a)

However take a look at the functionlaities of the function. There are quite a few options to use.

Output as Blob (file)

The best way to pass a multi-dimensional Array between deployments is to output the Array as either a NumPy Binary file or a text file. The example shows how to output as a NumPy binary file:

np.save('data.npy', np.array([[1, 2, 3], [4, 5, 6]]))
return {
    "np_file": "data.npy"
}

To read the data back in as an `Array` in the next deployment, use:
```python
# Load the array
arr = np.load(data['np_file'])
```