Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading numpy array from http response without saving a file

Tags:

python

numpy

I have a bunch of files containing numpy arrays at some url (e.g., https://my_url/my_np_file.npy) and I am trying to load them in my computer.

If I download the file manually, I can properly load the numpy array using np.load('file_path'). If I take the url reponse (using the code below), save the content to a file and then use np.load(), it also works.

response, content = http.request('https://my_url/my_np_file.npy')

If I try to load the array from the content string, I get the error bellow. This is likely because the np.load is interpreting the string input as a name for the file, rather than the data itself.

File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 370, in load fid = open(file, "rb") TypeError: file() argument 1 must be encoded string without null bytes, not str

Is there any way to load the array without having to save a file?

like image 961
Gabriel Ilharco Avatar asked Oct 19 '18 01:10

Gabriel Ilharco


People also ask

How do I save and load NumPy arrays?

You can save your NumPy arrays to CSV files using the savetxt() function. This function takes a filename and array as arguments and saves the array into CSV format.

What does .all do in NumPy?

all() in Python. The numpy. all() function tests whether all array elements along the mentioned axis evaluate to True.

Why do we use NumPy for array before saving data in Dataframe?

NumPy contains a multi-dimensional array and matrix data structures. It can be utilised to perform a number of mathematical operations on arrays such as trigonometric, statistical, and algebraic routines. Therefore, the library contains a large number of mathematical, algebraic, and transformation functions.


1 Answers

You are missing io.BytesIO to make the string appear like a file object to np.load!

The following is what you're looking for:

import requests
import io

response = requests.get('https://my_url/my_np_file.npy')
response.raise_for_status()
data = np.load(io.BytesIO(response.content))  # Works!
like image 186
LucasB Avatar answered Sep 22 '22 00:09

LucasB