import numpyas np
import pandasas pd
#1视哑、讀入數(shù)據(jù)集
header = ["user_id","item_id","rating","timestamp"]
data =pd.read_csv("u.data",sep="\t",names=header)
#2疚鲤、生成用戶(hù)—物品評(píng)分矩陣
#檢查是否有重復(fù)的用戶(hù)物品打分記錄
data.duplicated(subset = ["user_id","item_id"]).sum()
item_id_user = data.groupby("item_id").count()["user_id"]
#構(gòu)建用戶(hù)物品矩陣
users_num = data.user_id.max()
items_num = data.item_id.max()
user_item_rating = np.zeros((users_num,items_num))
for linein data.itertuples():#以元組的方式賦值
? ? user_item_rating[line[1]-1,line[2]-1] = line[3]
np.savetxt("評(píng)分12.csv", user_item_rating,delimiter =",")