<jupyter_start><jupyter_text>Introduction Small database about user and their friend.<jupyter_code>users = [ { "id": 0, "name": "Hero" }, { "id": 1, "name": "Dunn" }, { "id": 2, "name": "Sue" }, { "id": 3, "name": "Chi" }, { "id": 4, "name": "Thor" }, { "id": 5, "name": "Clive" }, { "id": 6, "name": "Hicks" }, { "id": 7, "name": "Devin" }, { "id": 8, "name": "Kate" }, { "id": 9, "name": "Klein" } ]
friendship_pairs = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4), (4, 5), (5, 6), (5, 7), (6, 8), (7, 8), (8, 9)]<jupyter_output><empty_output><jupyter_text>We want to create a small database of user's friends<jupyter_code># Create a Dict for each user
friend_finder = lambda user: [pair_ids[0] if pair_ids[0] != user["id"] else pair_ids[1] for pair_ids in friendship_pairs if user["id"] in pair_ids]
friendship = {user["id"]:friend_finder(user) for user in users}
friendship<jupyter_output><empty_output><jupyter_text>We want to know average friend number.<jupyter_code>sum([len(x) for x in friendship.values()]) / len(friendship)<jupyter_output><empty_output><jupyter_text>We want to know the most friend user has.<jupyter_code>friend_count = [(key, len(value)) for key, value in friendship.items()]
friend_count.sort(key=lambda x: x[1], reverse=True)
friend_count<jupyter_output><empty_output><jupyter_text>We want to know friend of user's friend. This is helpful to make a friend suggestion.<jupyter_code>def find_mutual(_id, friends):
    mutual = []
    for friend in friends:
        for fof in friendship[friend]:
            if fof not in mutual and fof != _id and fof not in friends:
                mutual.append(fof)
    return mutual
mutual = {_id:find_mutual(_id, friends) for _id, friends in friendship.items()}
mutual<jupyter_output><empty_output><jupyter_text>User's interest<jupyter_code>interests = [ (0, "Hadoop"), (0, "Big Data"), (0, "HBase"), (0, "Java"), (0, "Spark"), (0, "Storm"), (0, "Cassandra"), (1, "NoSQL"), (1, "MongoDB"), (1, "Cassandra"), (1, "HBase"), (1, "Postgres"), (2, "Python"), (2, "scikit-learn"), (2, "scipy"), (2, "numpy"), (2, "statsmodels"), (2, "pandas"), (3, "R"), (3, "Python"), (3, "statistics"), (3, "regression"), (3, "probability"), (4, "machine learning"), (4, "regression"), (4, "decision trees"), (4, "libsvm"), (5, "Python"), (5, "R"), (5, "Java"), (5, "C++"), (5, "Haskell"), (5, "programming languages"), (6, "statistics"), (6, "probability"), (6, "mathematics"), (6, "theory"), (7, "machine learning"), (7, "scikit-learn"), (7, "Mahout"), (7, "neural networks"), (8, "neural networks"), (8, "deep learning"), (8, "Big Data"), (8, "artificial intelligence"), (9, "Hadoop"), (9, "Java"), (9, "MapReduce"), (9, "Big Data") ]
def who_like(target):
    return [user_id for user_id, user_interest in interests if user_interest == target]
def interests_by_id(id):
    return [user_interest for user_id, user_interest in interests if user_id == id]
who_like("Java")
def similar_interest(target_id):
    interest_dict = {}
    for interest in interests_by_id(target_id):
        for user_id in who_like(interest):
            if user_id != target_id:
                if user_id not in interest_dict.keys():
                    interest_dict[user_id] = 1
                else:
                    interest_dict[user_id] += 1
    return interest_dict
import time
lim = 10
c = 0
for i in range(lim):
    start = time.time()
    similar_interest(0)
    end = time.time()

    c += (end-start)*1000
c / lim
from collections import Counter
lim = 10
c = 0
for i in range(lim):
    start = time.time()
    Counter(user_id for interest in interests_by_id(0) for user_id in who_like(interest) if user_id != 0)
    end = time.time()

    c += (end-start)*1000
c / lim<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1921946003742315, 'fraction_numerical': 0.028334669874365143, 'mean_word_length': 4.519174041297935, 'pattern_counts': {'":': 20, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '127591', 'license': 'MIT', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1358, 'n_words': 406, 'path': 'notebook/ch1-introduction.ipynb', 'repo_name': 'rogueanx9/data-science-from-scratch', 'size': '3741'}