我尝试建立基于内容的过滤模型,但出现TypeError“'numpy.float64'对象不可迭代”。我是Python的新手,如果您给我一些建议,我应该编辑些什么,将不胜感激。
对于其他数据集,此代码运行良好,但与此相同,这可能是个问题吗?
def get_item_profile(item_id):
idx = item_ids.index(item_id)
item_profile = tfidf_matrix[idx:idx+1]
return item_profile
def get_item_profiles(ids):
item_profiles_list = [get_item_profile(x) for x in ids]
item_profiles = scipy.sparse.vstack(item_profiles_list)
return item_profiles
def build_users_profile(person_id, interactions_indexed_df):
interactions_person_df = interactions_indexed_df.loc[person_id]
user_item_profiles = get_item_profiles(interactions_person_df['contentId'])
user_item_strengths = np.array(interactions_person_df['eventStrength']).reshape(-1,1)
#Weighted average of item profiles by the interactions strength
user_item_strengths_weighted_avg = np.sum(user_item_profiles.multiply(user_item_strengths), axis=0) / np.sum(user_item_strengths)
user_profile_norm = sklearn.preprocessing.normalize(user_item_strengths_weighted_avg)
return user_profile_norm
def build_users_profiles():
interactions_indexed_df = interactions_full_df[interactions_full_df['contentId'] \
.isin(articles_df['contentId'])].set_index('personId')
user_profiles = {}
for person_id in interactions_indexed_df.index.unique():
user_profiles[person_id] = build_users_profile(person_id, interactions_indexed_df)
return user_profiles
错误消息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-126-1ae36c59638c> in <module>
----> 1 user_profiles = build_users_profiles()
<ipython-input-124-7a1dedc86c38> in build_users_profiles()
27 user_profiles = {}
28 for person_id in interactions_indexed_df.index.unique():
---> 29 user_profiles[person_id] = build_users_profile(person_id, interactions_indexed_df)
30 return user_profiles
<ipython-input-124-7a1dedc86c38> in build_users_profile(person_id, interactions_indexed_df)
13 def build_users_profile(person_id, interactions_indexed_df):
14 interactions_person_df = interactions_indexed_df.loc[person_id]
---> 15 user_item_profiles = get_item_profiles(interactions_person_df['contentId'])
16 print(interactions_person_df['contentId'])
17
<ipython-input-124-7a1dedc86c38> in get_item_profiles(ids)
6
7 def get_item_profiles(ids):
----> 8 item_profiles_list = [get_item_profile(x) for x in ids]
9 item_profiles = scipy.sparse.vstack(item_profiles_list)
10 return item_profiles
TypeError: 'numpy.float64' object is not iterable
答案 0 :(得分:0)
如果您输入的数字为ids
,
而不是一些可迭代的数字序列,
那么您将触发此类类型错误。
安排该数据集包含多个ID, 与您的其他数据集相似。
答案 1 :(得分:0)
运行以下代码以查看结果:
import numpy as np
#b is iterable object
b = np.array([1,2],dtype=np.float64)
print([i for i in b])
#a is not iterable object
a = np.array([1,2],dtype=np.float64)[0]
print([i for i in a])