Skip to main content

save

save(fn, data)

Save the data using the automatically determined format.

If fn ends with “.npy”, save as npy file. If fn ends with “.npz” and data is sparse, save as npz file. If dict and fn ends with “.npz”, save as npz file. If fn ends with “.pkl”, save using pickle.dump. If fn ends with “.png”, save as PNG file (requires the Pillow package). If fn ends with “.json”, save as JSON file.

The function also automatically creates the directory fn is in if it does not exist.

Parameters

Name Type Description Default
fn str The filename (including path) to save the data to. required
data ndarray or spmatrix or dict or object The data to be saved. required

Returns

Name Type Description
ret Returned value of numpy.save, numpy.savez, scipy.sparse.save_npz, or pickle.dump.

Raises

Name Type Description
TypeError Raised when type(data) is not supported.