1
$\begingroup$

I have four numpy arrays X_train, y_train, X_test, y_test.

I want to know how do I export and import them all together.

I tried exporting them using

numpy.save("data.npy",list, [X_train, y_train, X_test, y_test])

I then tried importing using

np_data = numpy.load("data.npy", allow_pickle=True)

But I can't access the data, and get the following errors:

np_data says array(<class 'list'>, dtype=object)
np_data[0] says IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

and also I don't understand why allow_pickle is necessary ?

$\endgroup$
2
  • $\begingroup$ Have you tried using numpy.savez, i.e. numpy.savez("data.npy", X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test)? $\endgroup$
    – Oxbowerce
    Commented Mar 30, 2022 at 16:51
  • $\begingroup$ Yeah, numpy.savez did the job. Didn't know about it. Thanks. $\endgroup$
    – mehant_kr
    Commented Mar 30, 2022 at 17:35

1 Answer 1

1
$\begingroup$

You can simply use numpy.savez to save multiple numpy arrays to a single file. This would look as follows in your case:

numpy.savez("data.npy", X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.