Numpyアレイを検討しましたか?
PyTablesは、データが大きすぎてメモリに収まらない場合に最適ですが、8バイトのfloatの200x2000マトリックスには、約3MBのメモリしか必要ありません。ですから、PyTablesはやり過ぎかもしれないと思います。
numpy配列は、np.savetxt
または(圧縮用)を使用してファイルに保存でき、またはを使用np.savez
してファイルから読み取ることができます。np.loadtxt
np.load
ディスクに格納するそのような配列が多数ある場合は、numpy.npz
ファイルの代わりにデータベースを使用することをお勧めします。ちなみに、データベースに200x2000の行列を格納するには、行、列、値の3つのテーブル列のみが必要です。
import sqlite3
import numpy as np
db = sqlite3.connect(':memory:')
cursor = db.cursor()
cursor.execute('''CREATE TABLE foo
(row INTEGER,
col INTEGER,
value FLOAT,
PRIMARY KEY (row,col))''')
ROWS=4
COLUMNS=6
matrix = np.random.random((ROWS,COLUMNS))
print(matrix)
# [[ 0.87050721 0.22395398 0.19473001 0.14597821 0.02363803 0.20299432]
# [ 0.11744885 0.61332597 0.19860043 0.91995295 0.84857095 0.53863863]
# [ 0.80123759 0.52689885 0.05861043 0.71784406 0.20222138 0.63094807]
# [ 0.01309897 0.45391578 0.04950273 0.93040381 0.41150517 0.66263562]]
# Store matrix in table foo
cursor.executemany('INSERT INTO foo(row, col, value) VALUES (?,?,?) ',
((r,c,value) for r,row in enumerate(matrix)
for c,value in enumerate(row)))
# Retrieve matrix from table foo
cursor.execute('SELECT value FROM foo ORDER BY row,col')
data=zip(*cursor.fetchall())[0]
matrix2 = np.fromiter(data,dtype=np.float).reshape((ROWS,COLUMNS))
print(matrix2)
# [[ 0.87050721 0.22395398 0.19473001 0.14597821 0.02363803 0.20299432]
# [ 0.11744885 0.61332597 0.19860043 0.91995295 0.84857095 0.53863863]
# [ 0.80123759 0.52689885 0.05861043 0.71784406 0.20222138 0.63094807]
# [ 0.01309897 0.45391578 0.04950273 0.93040381 0.41150517 0.66263562]]
このような200x2000の行列が多数ある場合は、どの行列を指定するためにもう1つのテーブル列が必要です。