错误内存映射文件:数组无法进行内存映射:dtype中的Python对象

时间:2012-12-10 14:32:11

标签: python matrix numpy scipy sparse-matrix

我的应用程序需要存储和访问一个非常大的矩阵。虽然我会使用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)

有任何帮助吗? 感谢

1 个答案:

答案 0 :(得分:0)

在线     self.ratings = dok_matrix((self.n_users,self.n_items),dtype = np.dtype(np.float32)) 为什么type等于这个复合事物而不仅仅是np.float32?你不只是想说你想要一个花车矩阵吗?