我的应用程序需要存储和访问一个非常大的矩阵。我虽然会为此使用 dok_matrix ,但我似乎无法对文件进行内存映射。我收到错误:数组无法进行内存映射:dtype 中的 Python 对象。
这是代码:
def __init__(self, ratings_file):
try:
self.ratings = np.load(ratings_file, mmap_mode='r+')
self.n_users = self.ratings.shape[0]
self.n_items = self.ratings.shape[1]
except IOError:
self.ratings = None
self.n_users = 0
self.n_items = 0
def add_ratings(self, ratings):
# update the number of users and items
self.n_users = max(self.n_users, max([r[0] for r in ratings]) + 1)
self.n_items = max(self.n_items, max([r[1] for r in ratings]) + 1)
# reshape (or create) the matrix
if not self.ratings:
self.ratings = dok_matrix((self.n_users, self.n_items), dtype=np.dtype(np.float32))
else:
self.ratings.resize((self.n_users, self.n_items))
print 'Num. users: ', self.n_users
print 'Num. items: ', self.n_items
def update_model(self):
raise NotImplementedError()
def save(self):
np.save(self._ratings_file, self.ratings)
有什么帮助吗?谢谢