Error: 'numpy.int64' object is not iterable in Recommendation System

Viewed 410

I am working on a process for Clothing Recommendation System. Firstly, I got a problem with creating User Profiles.

Does any body know about it

  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_train_df[interactions_train_df['contentId'] \
                                            .isin(user_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

It showed the ERROR when I run this code.

user_profiles = build_users_profiles()
len(user_profiles)

ERROR:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-e5f681f1b3bb> in <module>()
----> 1 user_profiles = build_users_profiles()
      2 len(user_profiles)

2 frames
<ipython-input-23-0f847c74d142> in get_item_profiles(ids)
      5 
      6 def get_item_profiles(ids):
----> 7     item_profiles_list = [get_item_profile(x) for x in ids]
      8     item_profiles = scipy.sparse.vstack(item_profiles_list)
      9     return item_profiles

TypeError: 'numpy.int64' object is not iterable
1 Answers
  1. In user_item_profiles = get_item_profiles(interactions_person_df['contentId']) in build_users_profiles() function, as I may guess, you pass a specific user profile, which I suppose is of numpy.int64 type.
  2. Then in item_profiles_list = [get_item_profile(x) for x in ids] in the get_item_profiles(ids) function you try to iterate on it, which results in the upper mentioned error.

Cheers.

Related