<jupyter_start><jupyter_text>Audible Book Recommender> Finding similar books using simple text countvectorizer model on audible dataset- toc: false- badges: true- comments: true- categories: [Books, CountVectorizer]- image:<jupyter_code>import numpy as np
import pandas as pd

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
audible_data = pd.read_csv("https://github.com/sparsh-ai/reco-data/raw/audible/audible/audible.csv",
                           encoding='latin1')
audible_data.head()
audible_data.info()
# Selecting 4 columns: Title, Author, Narrator,Categories(Genre)
audible_data = audible_data[['Book Title', 'Book Author', 'Book Narrator', 'Categories']]

# Remove all 'Categories', and 'Book Narrator' NaN records
audible_data = audible_data[audible_data['Categories'].notna()]
audible_data = audible_data[audible_data['Book Narrator'].notna()]

# lower case and split on commas or &-sign 'Categories'
audible_data['Categories'] = audible_data['Categories'].map(
    lambda x: x.lower().replace(' &', ',').replace('genre', '').split(','))
# Book Author
audible_data['Book Author'] = audible_data['Book Author'].map(lambda x: x.lower().replace(' ', '').split(' '))
# Book Narrator
audible_data['Book Narrator'] = audible_data['Book Narrator'].map(lambda x: x.lower().replace(' ', '').split(' '))

for index, row in audible_data.iterrows():
    # row['Book Narrator'] = [x.replace(' ','') for x in row['Book Narrator']]
    row['Book Author'] = ''.join(row['Book Author'])
# make 'Book Title' as an index
audible_data.set_index('Book Title', inplace=True)

audible_data['bag_of_words'] = ''
for index, row in audible_data.iterrows():
    words = ''
    for col in audible_data.columns:
        if col != 'Book Author':
            words = words + ' '.join(row[col]) + ' '
        else:
            words = words + row[col] + ' '
    row['bag_of_words'] = words

audible_data.drop(columns=[x for x in audible_data.columns if x != 'bag_of_words'], inplace=True)
recommendation_movies = []

# Vectorizing the entire matrix as described above!
count = CountVectorizer(stop_words='english')
count_matrix = count.fit_transform(audible_data['bag_of_words'])

# running pairwise cosine similarity
cosine_sim2 = cosine_similarity(count_matrix, count_matrix)  # getting a similarity matrix
def recommend(k=5):
    # gettin the index of the book that matches the title
    indices = pd.Series(audible_data.index)
    idx = indices.sample(1)

    # creating a Series with the similarity scores in descending order
    score_series = pd.Series(cosine_sim2[idx.index[0]]).sort_values(ascending=False)

    # getting the indexes of the k most similar audiobooks
    top_k_indexes = list(score_series.iloc[1:k+1].index)

    topk = indices[top_k_indexes].tolist()

    print("For '{}', Top {} similar audiobooks are {}".format(idx.values[0], k, topk))
recommend()
recommend()
recommend()
recommend()
recommend(10)<jupyter_output>For 'Broken (in the Best Possible Way)', Top 10 similar audiobooks are ['<NAME>: Short Spree Killer and Versace Nemesis', '<NAME>', 'Say Nothing', 'Billion Dollar Loser', 'Unbroken', 'Nothing Personal', 'The Immortal Life of Henrietta Lacks', 'The Splendid and the Vile', 'Red Notice', 'We Few']
{'fraction_non_alphanumeric': 0.1396835057821059, 'fraction_numerical': 0.00426049908703591, 'mean_word_length': 5.064575645756458, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '329134', 'license': 'Apache-2.0', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1041, 'n_words': 283, 'path': '_notebooks/2021-07-28-audible-audiobook-recommender.ipynb', 'repo_name': 'recohut/notebook', 'size': '3298'}