<jupyter_start><jupyter_text>Calculating Similaritycreate some transformer embedded vectors, then use a cosine similarity<jupyter_code># # imdb_top_10 = pd.read_csv('../data/imdb_top_1000.csv').head(10)

# # p.dump(imdb_top_10, open('imdb_top_10.p', 'wb'))

# # df = p.load(open('/Users/b/movie-rec/model/imdb_top_10.p', 'rb'))

# sentences = df['Overview']
# [x for x, y in h.compute_similarity(0, sentences)]
import h
import pandas as pd
pd.set_option('display.max_colwidth', None)
# use movies dataset
df = pd.read_csv('../data/imdb_top_1000.csv').head(10)
df['title_overview'] = df['Series_Title'] + ' ' + df['Overview']
df[['Series_Title', 'Overview']].head(10)
h.compute_similarity(0, sentences)
sentences = df['Overview']
h.compute_similarity(0, sentences)

df['similar_movies'] = pd.Series(df.index).apply(lambda x: h.compute_similarity(x, df['Overview']))
df['similar_movies']
sentence_embeddings = h.sentence_transformer_similarity(df['title_overview'])
sentence_embeddings
from sklearn.metrics.pairwise import cosine_similarity
def list_similarity_scores(main_index, embeddings):
    similarity_scores = cosine_similarity(
        [sentence_embeddings[main_index]],
        sentence_embeddings)
    ranked_list = sorted([(i, x) for i, x in enumerate(similarity_scores[0])], key=lambda x: x[1], reverse=True)
    return ranked_list[:10]
df['similarity_scores'] = pd.Series(df.index).apply(lambda x: list_similarity_scores(x, df['Overview']))
df['similarity_scores'][0]
df['similar_movies'] = pd.Series(df.index).apply(lambda y: [(df['Series_Title'][int(x)], df['Overview'][int(x)]) for x in [x[0] for x in df['similarity_scores'][y]]])
df['similar_movies'][0]
df[['similar_movies', 'similarity_scores', 'Series_Title',
 'Released_Year',
 'Certificate',
 'Runtime',
 'Genre',
 'IMDB_Rating',
 'Overview',
 'Meta_score',
 'Director',
 'Star1',
 'Star2',
 'Star3',
 'Star4',
 'No_of_Votes',
 'Gross',
 'Poster_Link']].to_csv('similar_movies_df.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17522658610271905, 'fraction_numerical': 0.01812688821752266, 'mean_word_length': 7.279166666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '4668', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 783, 'n_tokens_neox': 721, 'n_words': 124, 'path': 'model/pairwise_similarity.ipynb', 'repo_name': 'byukan/movie-rec', 'size': '1986'}