Tôi nghĩ rằng matlab
định dạng là một cách thực sự thuận tiện để lưu trữ và lấy mảng NumPy. Thực sự là nhanh và dấu chân đĩa và bộ nhớ hoàn toàn giống nhau.
(hình ảnh từ mverleg benchmarks)
Nhưng nếu vì lý do nào đó bạn cần lưu trữ các mảng NumPy vào SQLite Tôi đề nghị thêm một số khả năng nén.
Các dòng thêm từ unutbu code đang khá đơn giản
compressor = 'zlib' # zlib, bz2
def adapt_array(arr):
"""
http://stackoverflow.com/a/31312102/190597 (SoulNibbler)
"""
# zlib uses similar disk size that Matlab v5 .mat files
# bz2 compress 4 times zlib, but storing process is 20 times slower.
out = io.BytesIO()
np.save(out, arr)
out.seek(0)
return sqlite3.Binary(out.read().encode(compressor)) # zlib, bz2
def convert_array(text):
out = io.BytesIO(text)
out.seek(0)
out = io.BytesIO(out.read().decode(compressor))
return np.load(out)
Kết quả thử nghiệm với cơ sở dữ liệu MNIST cho đó là:
$ ./test_MNIST.py
[69900]: 99% remain: 0 secs
Storing 70000 images in 379.9 secs
Retrieve 6990 images in 9.5 secs
$ ls -lh example.db
-rw-r--r-- 1 agp agp 69M sep 22 07:27 example.db
$ ls -lh mnist-original.mat
-rw-r--r-- 1 agp agp 53M sep 20 17:59 mnist-original.mat
```
sử dụng zlib
, và
$ ./test_MNIST.py
[69900]: 99% remain: 12 secs
Storing 70000 images in 8536.2 secs
Retrieve 6990 images in 37.4 secs
$ ls -lh example.db
-rw-r--r-- 1 agp agp 19M sep 22 03:33 example.db
$ ls -lh mnist-original.mat
-rw-r--r-- 1 agp agp 53M sep 20 17:59 mnist-original.mat
sử dụng bz2
So sánh định dạng Matlab V5
với bz2
trên SQLite, nén bz2 là khoảng 2.8, nhưng thời gian truy cập khá dài so với định dạng Matlab (gần như ngay lập tức so với hơn 30 giây). Có lẽ chỉ đáng giá đối với các cơ sở dữ liệu thực sự khổng lồ mà quá trình học tập tốn nhiều thời gian hơn thời gian truy cập hoặc nơi mà dấu chân cơ sở dữ liệu là cần thiết càng nhỏ càng tốt.
Cuối cùng lưu ý rằng tỷ lệ bipz/zlib
là khoảng 3.7 và zlib/matlab
yêu cầu thêm 30% không gian.
Mã đầy đủ nếu bạn muốn chơi mình là:
import sqlite3
import numpy as np
import io
compressor = 'zlib' # zlib, bz2
def adapt_array(arr):
"""
http://stackoverflow.com/a/31312102/190597 (SoulNibbler)
"""
# zlib uses similar disk size that Matlab v5 .mat files
# bz2 compress 4 times zlib, but storing process is 20 times slower.
out = io.BytesIO()
np.save(out, arr)
out.seek(0)
return sqlite3.Binary(out.read().encode(compressor)) # zlib, bz2
def convert_array(text):
out = io.BytesIO(text)
out.seek(0)
out = io.BytesIO(out.read().decode(compressor))
return np.load(out)
sqlite3.register_adapter(np.ndarray, adapt_array)
sqlite3.register_converter("array", convert_array)
dbname = 'example.db'
def test_save_sqlite_arrays():
"Load MNIST database (70000 samples) and store in a compressed SQLite db"
os.path.exists(dbname) and os.unlink(dbname)
con = sqlite3.connect(dbname, detect_types=sqlite3.PARSE_DECLTYPES)
cur = con.cursor()
cur.execute("create table test (idx integer primary key, X array, y integer);")
mnist = fetch_mldata('MNIST original')
X, y = mnist.data, mnist.target
m = X.shape[0]
t0 = time.time()
for i, x in enumerate(X):
cur.execute("insert into test (idx, X, y) values (?,?,?)",
(i, y, int(y[i])))
if not i % 100 and i > 0:
elapsed = time.time() - t0
remain = float(m - i)/i * elapsed
print "\r[%5d]: %3d%% remain: %d secs" % (i, 100 * i/m, remain),
sys.stdout.flush()
con.commit()
con.close()
elapsed = time.time() - t0
print
print "Storing %d images in %0.1f secs" % (m, elapsed)
def test_load_sqlite_arrays():
"Query MNIST SQLite database and load some samples"
con = sqlite3.connect(dbname, detect_types=sqlite3.PARSE_DECLTYPES)
cur = con.cursor()
# select all images labeled as '2'
t0 = time.time()
cur.execute('select idx, X, y from test where y = 2')
data = cur.fetchall()
elapsed = time.time() - t0
print "Retrieve %d images in %0.1f secs" % (len(data), elapsed)
if __name__ == '__main__':
test_save_sqlite_arrays()
test_load_sqlite_arrays()
Không biết, nhưng cố gắng để chuyển đổi vào danh sách? np.arange (1000) .tolist() – reptilicus
hoặc có thể là json.dumps (np.arange (1000) .tolist()) – reptilicus