在EucledianScore的for循环-> j在train_user中运行我的项目的此代码时,出现“类型对象不可迭代”错误。此代码用于使用MovieLens.I中的数据集进行用户-用户协作过滤。曾经看到过许多“类型对象不可迭代”的问题解决方案,但是这些不适用于我的代码。
def user_collabo():
def EucledianScore(train_user, test_user):
sum = 0
count = 0
for i in test_user:
score = 0
for j in train_user:
if(int(i[1]) == int(j[1])):
score= ((float(i[2])-float(j[2]))*(float(i[2])-float(j[2])))
count= count + 1
sum = sum + score
if(count<4):
sum = 1000000
return(math.sqrt(sum))
data_cols=['user id','movie id','rating','timestamp']
item_cols=['movie id','movie title','release date',
'video release date','IMDb URL','unknown','Action',
'Adventure','Animation','Childrens','Comedy','Crime',
'Documentary','Drama','Fantasy','Film-Noir','Horror',
'Musical','Mystery','Romance ','Sci-Fi','Thriller',
'War' ,'Western']
user_cols = ['user id','age','gender','occupation',
'zip code']
users = pd.read_csv('u.user', sep='|', names=user_cols, encoding='latin-1')
item = pd.read_csv('u.item', sep='|', names=item_cols, encoding='latin-1')
data = pd.read_csv('u.data', sep='\t', names=data_cols, encoding='latin-1')
utrain = (data.sort_values('user id'))[:99832]
print(utrain.tail())
utest = (data.sort_values('user id'))[99833:]
print(utest.head())
utrain = utrain.as_matrix(columns = ['user id', 'movie id', 'rating'])
utest = utest.as_matrix(columns = ['user id', 'movie id', 'rating'])
users_list = []
for i in range(1,943):
list1 = []
for j in range(0,len(utrain)):
if utrain[j][0] == i:
list1.append(utrain[j])
else:
break
utrain = utrain[j:]
users_list.append(list1)
score_list = []
for i in range(0,942):
score_list.append([i+1,EucledianScore(users_list[i], utest)])
score = pd.DataFrame(score_list, columns = ['user id','Eucledian Score'])
score = score.sort_values(by = 'Eucledian Score')
#print(score)
score_matrix = score.as_matrix()
user= int(score_matrix[0][0])
common_list = []
full_list = []
for i in utest:
for j in users_list[user-1]:
if(int(i[1])== int(j[1])):
common_list.append(int(j[1]))
full_list.append(j[1])
common_list = set(common_list)
full_list = set(full_list)
recommendation = full_list.difference(common_list)
item_list = (((pd.merge(item,data).sort_values(by = 'movie id')).groupby('movie title')))['movie id', 'movie title', 'rating']
item_list = item_list.mean()
item_list['movie title'] = item_list.index
item_list = item_list.as_matrix()
recommendation_list = []
for i in recommendation:
recommendation_list.append(item_list[i-1])
recommendation = (pd.DataFrame(recommendation_list,columns = ['movie id','mean rating' ,'movie title'])).sort_values(by = 'mean rating', ascending = False)
print(recommendation[['mean rating','movie title']])
user_collabo()
输出:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-24f9c9703462> in <module>
71 recommendation = (pd.DataFrame(recommendation_list,columns = ['movie id','mean rating' ,'movie title'])).sort_values(by = 'mean rating', ascending = False)
72 print(recommendation[['mean rating','movie title']])
---> 73 user_collabo()
<ipython-input-20-24f9c9703462> in user_collabo()
43 score_list = []
44 for i in range(0,942):
---> 45 score_list.append([i+1,EucledianScore(users_list[i], utest)])
46
47 score = pd.DataFrame(score_list, columns = ['user id','Eucledian Score'])
<ipython-input-20-24f9c9703462> in EucledianScore(train_users, test_user)
5 for i in test_user:
6 score = 0
----> 7 for j in train_users:
8 if(int(i[1]) == int(j[1])):
9 score= ((float(i[2])-float(j[2]))*(float(i[2])-float(j[2])))
TypeError: 'type' object is not iterable
答案 0 :(得分:0)
错误来自此行:
users_list.append(list)
这将附加python内置类型list
。我认为您的意思是users_list.append(list1)
。这会将users_list
建立到列表列表中。