2012-12-10 24 views
1

我的應用程序需要存儲和訪問非常大的矩陣。我雖然我會用這個dok_matrix,但我似乎無法記憶映射文件。我收到錯誤:數組無法被內存映射:dtype中的Python對象。錯誤內存映射文件:數組無法進行內存映射: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) 

任何幫助嗎? 由於

回答

0

在線路 self.ratings = dok_matrix((self.n_users,self.n_items),D型細胞= np.dtype(np.float32)) 爲什麼type等於該化合物的事情,而不是僅僅np.float32?你不只是想說你想要一個浮動矩陣嗎?

+0

我也嘗試過,但它不起作用。同樣的錯誤。 – user1491915