使用python处理Movielens数据集

注意点:

  1. 由于电影id最大编号过大,为节省内存空间,使用movies中movieId相对应的index构建rating
  2. 遍历函数 iterrows()的使用:
    iterrows()是在数据框中的行进行迭代的一个生成器,它返回每行的索引及一个包含行本身的对象
for index, row in dataframe.iterrows():

代码:

import numpy as np
import pandas as pd
ratings_df = pd.read_csv('ratings.csv')
movies_df = pd.read_csv('movies.csv')
movies_df['movieRow'] = movies_df.index
movies_df = movies_df[['movieRow', 'movieId', 'title']]
ratings_df = pd.merge(ratings_df, movies_df,on='movieId')
ratings_df = ratings_df[['userId', 'movieRow','rating']]
userNo = ratings_df['userId'].max()+1
movieNo = ratings_df['movieRow'].max()+1
rating = np.zeros((movieNo, userNo))
rating_df_length = np.shape(ratings_df)[0]
flag = 0
for index,row in ratings_df.iterrows():
    rating[int(row['movieRow']),int(row['userId'])] = row['rating']
    flag += 1
    print('processed %d, %d left' % (flag, rating_df_length - flag))
rating

你可能感兴趣的:(使用python处理Movielens数据集)