मुझे लगता है कि matlab
प्रारूप की दुकान और NumPy सरणी प्राप्त करने के लिए वास्तव में आसान तरीका है में काम करने लगता है। वास्तव में तेज़ और डिस्क और मेमोरी पदचिह्न काफी समान है।
(mverleg benchmarks से छवि)
लेकिन मैं कुछ संपीड़न क्षमताओं को जोड़ने के लिए सुझाव है कि यदि किसी भी कारण से आप SQLite में NumPy सरणी के स्टोर करने के लिए की जरूरत है।
unutbu कोड से अतिरिक्त लाइनें बहुत सरल है
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)
MNIST डेटाबेस के साथ परिणाम परीक्षण किए गए थे देता है:
$ ./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
```
zlib
, और
$ ./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
का उपयोग कर bz2
का उपयोग कर
SQLite पर bz2
के साथ Matlab V5
प्रारूप की तुलना में, bz2 संपीड़न लगभग 2.8 है, लेकिन पहुंच का समय Matlab प्रारूप (लगभग तुरंत 30 सेकंड से अधिक बनाम) की तुलना में काफी लंबा है। हो सकता है कि केवल वास्तव में विशाल डेटाबेस के लिए योग्य हो जहां सीखने की प्रक्रिया एक्सेस समय से अधिक समय लेती है या जहां संभव हो सके डेटाबेस पदचिह्न की आवश्यकता होती है।
अंत में ध्यान दें कि bipz/zlib
अनुपात लगभग 3.7 और zlib/matlab
के लिए 30% अधिक स्थान की आवश्यकता है।
पूर्ण कोड आप अपने आप को खेलने के लिए चाहते हैं:
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()
पता नहीं है, लेकिन सूची बदलने की कोशिश? np.arange (1000) .tolist() – reptilicus
या शायद json.dumps (np.arange (1000) .tolist()) – reptilicus