Lưu ma trận thưa thớt vào một tệp bằng định dạng .npz
.
Tên tệp [chuỗi] hoặc một tệp mở [đối tượng giống như tệp] nơi dữ liệu sẽ được lưu. Nếu tệp là một chuỗi, tiện ích mở rộng .npz
sẽ được thêm vào tên tệp nếu nó chưa có.
Ma trận thưa thớt để lưu.
Cho phép nén tệp. Mặc định: Đúng
Ví dụ
Lưu trữ ma trận thưa thớt vào đĩa và tải lại:
>>> import scipy.sparse >>> sparse_matrix = scipy.sparse.csc_matrix[np.array[[[0, 0, 3], [4, 0, 0]]]] >>> sparse_matrix >>> sparse_matrix.toarray[] array[[[0, 0, 3], [4, 0, 0]], dtype=int64]
>>> scipy.sparse.save_npz['/tmp/sparse_matrix.npz', sparse_matrix] >>> sparse_matrix = scipy.sparse.load_npz['/tmp/sparse_matrix.npz']
>>> sparse_matrix >>> sparse_matrix.toarray[] array[[[0, 0, 3], [4, 0, 0]], dtype=int64]
Dưới đây là so sánh hiệu suất của ba câu trả lời được nâng cấp nhiều nhất bằng cách sử dụng Notebook Jupyter. Đầu vào là ma trận thưa thớt ngẫu nhiên 1m x 100k với mật độ 0,001, chứa 100m giá trị khác không:
from scipy.sparse import random
matrix = random[1000000, 100000, density=0.001, format='csr']
matrix
io.mmwrite
/ >>> scipy.sparse.save_npz['/tmp/sparse_matrix.npz', sparse_matrix]
>>> sparse_matrix = scipy.sparse.load_npz['/tmp/sparse_matrix.npz']
0
from scipy.sparse import io
%time io.mmwrite['test_io.mtx', matrix]
CPU times: user 4min 37s, sys: 2.37 s, total: 4min 39s
Wall time: 4min 39s
%time matrix = io.mmread['test_io.mtx']
CPU times: user 2min 41s, sys: 1.63 s, total: 2min 43s
Wall time: 2min 43s
matrix
Filesize: 3.0G.
[Lưu ý rằng định dạng đã được thay đổi từ CSR thành COO].
>>> scipy.sparse.save_npz['/tmp/sparse_matrix.npz', sparse_matrix]
>>> sparse_matrix = scipy.sparse.load_npz['/tmp/sparse_matrix.npz']
1 / >>> scipy.sparse.save_npz['/tmp/sparse_matrix.npz', sparse_matrix]
>>> sparse_matrix = scipy.sparse.load_npz['/tmp/sparse_matrix.npz']
2
import numpy as np
from scipy.sparse import csr_matrix
def save_sparse_csr[filename, array]:
# note that .npz extension is added automatically
np.savez[filename, data=array.data, indices=array.indices,
indptr=array.indptr, shape=array.shape]
def load_sparse_csr[filename]:
# here we need to add .npz extension manually
loader = np.load[filename + '.npz']
return csr_matrix[[loader['data'], loader['indices'], loader['indptr']],
shape=loader['shape']]
%time save_sparse_csr['test_savez', matrix]
CPU times: user 1.26 s, sys: 1.48 s, total: 2.74 s
Wall time: 2.74 s
%time matrix = load_sparse_csr['test_savez']
CPU times: user 1.18 s, sys: 548 ms, total: 1.73 s
Wall time: 1.73 s
matrix
Filesize: 1.1G.
>>> scipy.sparse.save_npz['/tmp/sparse_matrix.npz', sparse_matrix]
>>> sparse_matrix = scipy.sparse.load_npz['/tmp/sparse_matrix.npz']
3
import cPickle as pickle
def save_pickle[matrix, filename]:
with open[filename, 'wb'] as outfile:
pickle.dump[matrix, outfile, pickle.HIGHEST_PROTOCOL]
def load_pickle[filename]:
with open[filename, 'rb'] as infile:
matrix = pickle.load[infile]
return matrix
%time save_pickle[matrix, 'test_pickle.mtx']
CPU times: user 260 ms, sys: 888 ms, total: 1.15 s
Wall time: 1.15 s
%time matrix = load_pickle['test_pickle.mtx']
CPU times: user 376 ms, sys: 988 ms, total: 1.36 s
Wall time: 1.37 s
matrix
Filesize: 1.1G.
Lưu ý: CPickle không hoạt động với các đối tượng rất lớn [xem câu trả lời này]. Theo kinh nghiệm của tôi, nó không hoạt động cho một ma trận 2,7m x 50k với các giá trị khác không 270m. Giải pháp >>> scipy.sparse.save_npz['/tmp/sparse_matrix.npz', sparse_matrix]
>>> sparse_matrix = scipy.sparse.load_npz['/tmp/sparse_matrix.npz']
1 hoạt động tốt.: cPickle does not work with very large objects [see
this answer]. In my experience, it didn't work for a 2.7M x 50k matrix with 270M non-zero values.
>>> scipy.sparse.save_npz['/tmp/sparse_matrix.npz', sparse_matrix] >>> sparse_matrix = scipy.sparse.load_npz['/tmp/sparse_matrix.npz']1 solution worked well.
Sự kết luận
. Vì vậy,
>>> scipy.sparse.save_npz['/tmp/sparse_matrix.npz', sparse_matrix] >>> sparse_matrix = scipy.sparse.load_npz['/tmp/sparse_matrix.npz']1 là người chiến thắng ở đây.