In: Computer Science
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work.
Below is the starter template for the code:
def overlap(user1, user2, interests):
""" Return the number of interests that user1 and user2
have in common """
return 0
def most_similar(user, interests):
""" Determine the name of user who is most similar
to the input user defined by having the most interests
in common. Return a tuple: (most_similar_user,
num_interests_in_common) """
return ("", 0)
def recommendations(user, interests):
""" Find the user who shares the most interests with the specified
user.
Recommend to user any interests that his or her closest match
has
that are not already in the user's list of interests.
Return a list of recommendations. """
return []
#%% Don't change this:
interests = {
"Pam":["Hadoop", "Big Data", "HBase", "Java", "Spark", "Storm",
"Cassandra"],
"Ann":["NoSQL", "MongoDB", "Cassandra", "HBase", "Postgres"],
"Raj":["Python", "scikit-learn", "scipy", "numpy", "statsmodels",
"pandas"],
"Jim":["R", "Python", "statistics", "regression", "probability",
"programming languages"],
"Eve":["machine learning", "regression", "decision trees",
"libsvm"],
"Carl Yastrzemski":["Python", "R", "Java", "C++", "Haskell",
"programming languages"],
"Sam":["statistics", "probability", "mathematics", "theory"],
"Una":["machine learning", "scikit-learn", "Mahout", "neural
networks"],
"Liv":["neural networks", "deep learning", "Big Data", "artificial
intelligence"],
"Bob":["Hadoop", "Java", "MapReduce", "Big Data"],
"Jon":["statistics", "R", "statsmodels"],
"Tom":["pandas", "R", "Python"],
"Tim":["databases", "HBase", "Postgres", "MySQL", "MongoDB"],
"Hal":["libsvm", "regression", "support vector machines"],
"Joe":["C++", "deep learning", "artificial intelligence",
"probability"]
}
#%% Don't change this either!
for user in interests.keys():
print(user, recommendations(user, interests))
#%% Put output here
"""
Your output
"""
PYTHON CODE:
""" Return the number of interests that user1 and user2
have in common """
def overlap(user1, user2, interests):
num_of_interests = 0;
# Iterates through each value of user1
for user1_values in interests[user1]:
# if true, num_of_interests will be incremented by 1
if (user1_values in interests[user2] ):
num_of_interests += 1;
return num_of_interests;
""" Determine the name of user who is most similar
to the input user defined by having the most interests
in common. Return a tuple: (most_similar_user, num_interests_in_common) """
def most_similar(user, interests):
user_count = {};
# Iterates through each user in the interest dictionary
for dict_user in interests.keys():
# Ignores the input user
if (user != dict_user ):
count = overlap(user, dict_user, interests);
# All the count values with user names are stored as key and value in user_count
user_count[dict_user] = count;
count = 0;
# maximum value in the user_count dict is found and returned
most_similar_user = max(user_count, key = user_count.get);
return (most_similar_user, user_count[most_similar_user])
""" Find the user who shares the most interests with the specified user.
Recommend to user any interests that his or her closest match has
that are not already in the user's list of interests.
Return a list of recommendations. """
def recommendations(user, interests):
recommendation = [];
# input_user_interests has the interests of the input user of type set
input_user_interests = set(interests[user]);
# Calls the most_similar function
most_similar_user = most_similar(user, interests);
# most_similar_user_interests has the interests of the most similar user of type set
most_similar_user_interests = set(interests[most_similar_user[0]]);
# UnionSet now has all the interests of both the users
UnionSet = input_user_interests.union(most_similar_user_interests);
# UnionSet now has only the interest of most similar user (common interests not included)
UnionSet = UnionSet - input_user_interests;
return list(UnionSet);
interests = {
"Pam":["Hadoop", "Big Data", "HBase", "Java", "Spark", "Storm", "Cassandra"],
"Ann":["NoSQL", "MongoDB", "Cassandra", "HBase", "Postgres"],
"Raj":["Python", "scikit-learn", "scipy", "numpy", "statsmodels", "pandas"],
"Jim":["R", "Python", "statistics", "regression", "probability", "programming languages"],
"Eve":["machine learning", "regression", "decision trees", "libsvm"],
"Carl Yastrzemski":["Python", "R", "Java", "C++", "Haskell", "programming languages"],
"Sam":["statistics", "probability", "mathematics", "theory"],
"Una":["machine learning", "scikit-learn", "Mahout", "neural networks"],
"Liv":["neural networks", "deep learning", "Big Data", "artificial intelligence"],
"Bob":["Hadoop", "Java", "MapReduce", "Big Data"],
"Jon":["statistics", "R", "statsmodels"],
"Tom":["pandas", "R", "Python"],
"Tim":["databases", "HBase", "Postgres", "MySQL", "MongoDB"],
"Hal":["libsvm", "regression", "support vector machines"],
"Joe":["C++", "deep learning", "artificial intelligence", "probability"]
}
#%% Don't change this either!
for user in interests.keys():
print(user, recommendations(user, interests))
print("Pam and Ann have",overlap("Pam", "Ann", interests), "interests in common");
print(most_similar("Pam", interests)[0], "is the most similar to Pam");
OUTPUT: