{ } Raw JSON

bundles / scipy 1.17.1 / scipy / sparse / _matrix_io / save_npz

function

scipy.sparse._matrix_io:save_npz

source: /scipy/sparse/_matrix_io.py :11

Signature

def   save_npz ( file matrix compressed = True )

Summary

Save a sparse matrix or array to a file using .npz format.

Parameters

file : str or file-like object

Either the file name (string) or an open file (file-like object) where the data will be saved. If file is a string, the .npz extension will be appended to the file name if it is not already there.

matrix: spmatrix or sparray

The sparse matrix or array to save. Supported formats: csc, csr, bsr, dia or coo.

compressed : bool, optional

Allow compressing the file. Default: True

Examples

Store sparse matrix to disk, and load it again:
import numpy as np
import scipy as sp
sparse_matrix = sp.sparse.csc_matrix([[0, 0, 3], [4, 0, 0]])
sparse_matrix
sparse_matrix.toarray()
sp.sparse.save_npz('/tmp/sparse_matrix.npz', sparse_matrix)
sparse_matrix = sp.sparse.load_npz('/tmp/sparse_matrix.npz')
sparse_matrix
sparse_matrix.toarray()

See also

numpy.savez

Save several arrays into a .npz archive.

numpy.savez_compressed

Save several arrays into a compressed .npz archive.

scipy.sparse.load_npz

Load a sparse matrix from a file using .npz format.

Aliases

  • scipy.sparse.save_npz

Referenced by

This package