Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8984689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:10:04+00:00 2026-06-15T21:10:04+00:00

I’m a new learner about python and there are some problems when I try

  • 0

I’m a new learner about python and there are some problems when I try to repeat the example provided in a guide book. This example is about recommendation algorithm. This example is trying to implement an item list which stores the users having rated the particular item.
this is the codes(python 2.7)

def UserSimilarity(train):
    #build inverse table for item_users
    item_users=dict()
    for u,items in train.items():
        for i in items.keys():
            if i not in item_users:
                item_users[i]=set()              
            item_users[i].add(u)


    #calculate co-rated items between users
    C=dict()
    N=dict()
    for i, users in item_users.items():
        print i,users
        #print N[u]
        for u in users:
            N[u]=N[u]+1
            print N[u]
            for v in users:
                print C[u][v]
                if u==v:
                    continue
                C[u][v]=C[u][v]+1

    #calculate finial similarity matrix W
    W=dict()
    for u, related_users in C.items():
        for v, cuv in related_users.items():
            W[u][v]=cuv/math.sqrt(N[u]*N[v])
    return W

ps: the data format of ‘train’ is a dictionary and like {UserId1:{ItemId1:Ratings1,ItemId2,Rating2,...},...}

The problem I met is that

Traceback (most recent call last):
  File "D:\Users\Administrator\workspace\GroupLens\src\test3.py", line 82, in <module>
    UserSimilarity(train_dic)
  File "D:\Users\Administrator\workspace\GroupLens\src\test3.py", line 66, in UserSimilarity
    N[u]=N[u]+1
KeyError: '3'

I don’t know how to improve it and hope someone would help me!
Thanks a lot!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:10:06+00:00Added an answer on June 15, 2026 at 9:10 pm

    The main issue is that you are defining a new dictionary (N = dict()), and then iterating through your users, trying to create a dictionary key based on a given user. That part is fine, but the issue arises when you do this:

    N[u]=N[u]+1
    

    Assigning a value to the dictionary is fine, but look at the right side – you are trying to assign to N[u] the value of N[u] + 1, when N[u] doesn’t exist yet (hence the error). I’m not 100% sure what the overall goal is (so this may be misguided), but if your aim is to increment a number based on how many times a user occurs, you could use a defaultdict, which is created with a type as an argument (here an int). This means that if the key is not found (as in your error above), the default value is based on the type you declared (here 0):

    In [1]: from collections import defaultdict
    
    In [2]: N = defaultdict(int)
    
    In [3]: users = [1, 2, 3, 2, 1, 2]
    
    In [4]: for u in users:
       ...:     N[u] += 1
       ...:     
       ...:     
    
    In [5]: N
    Out[5]: defaultdict(<type 'int'>, {1: 2, 2: 3, 3: 1})
    

    Alternatively, you could use a normal dictionary but with the get method, which returns a value if it is found but returns a default if not (a default that you can specify yourself):

    In [1]: N = dict()
    
    In [2]: users = [1, 2, 3, 2, 1, 2]
    
    In [3]: for u in users:
       ...:     N[u] = N.get(u, 0) + 1
       ...:     
       ...:     
    
    In [4]: N
    Out[4]: {1: 2, 2: 3, 3: 1}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.