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 in UbiOps. 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 file.

Convert to string

Numpy has built in functionality which you can use for converting NumPy arrays to strings.

It can be as simple as:

import numpy as np

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

string_array = np.array2string(a)

Do take a look at the functionalities of the function if you plan to use it. There are quite a few options to use.

Output as 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:'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:

# Load the array
arr = np.load(data['np_file'])