<jupyter_start><jupyter_text>Beer RecommenderThis notebook uses a dataset of beer reviews to recommend similar beers according to user's taste  Imports<jupyter_code># Importing libraries
import numpy as np 
import pandas as pd<jupyter_output><empty_output><jupyter_text>loading data and selecting the first 300 beersThere more than 1M reviews in this dataset. For this reason, we are limiting the number of entries, otherwise it can easily run out of memory<jupyter_code>#df = pd.read_csv('data/beer_reviews/beer_reviews.csv') Full dataset at https://drive.google.com/file/d/1LatWjs4ghvv9xATkDeaSeDRoSObTwiAv/view?usp=sharing
# Reading data
df = pd.read_csv('data/beer_reviews.csv')

# The number of beers to select
number_of_beers = 300

# Selecting the name of the top 300 beers
top_n = df['beer_name'].value_counts().index[:number_of_beers]

# Applying the filter to the dataframe
df = df[df['beer_name'].isin(top_n)]<jupyter_output><empty_output><jupyter_text>Visualizing the top 5 rows<jupyter_code># Showing the head of dataframe
df.head()<jupyter_output><empty_output><jupyter_text>Size of dataset with the specified number of beers<jupyter_code># Length of dataframe
len(df)<jupyter_output><empty_output><jupyter_text>Showing the unique beers of the dataset<jupyter_code># Showing the unique beer names
df['beer_name'].unique()
#df[df['beer_name'] == 'Budweiser']<jupyter_output><empty_output><jupyter_text>Computing the pivot table, creating a comparison table for each reviewer and each beer. Values with 0 represents beers that haven't been rated by a specific user<jupyter_code>matrix = df.pivot_table(index=['review_profilename'], columns=['beer_name'], values='review_overall').fillna(0)
# Showing the head of the table
matrix.head()<jupyter_output><empty_output><jupyter_text>Creating a correlation matrix, comparing rating of each pair of beers. Excluding beers with less than 100 rates to create a more consistent model<jupyter_code>corr_matrix = matrix.corr(method='pearson', min_periods=100)
# Printing the correlation matrix
corr_matrix<jupyter_output><empty_output><jupyter_text>Compute the similarity of the selected beers to the rest of entries, excluding beers that the user already like<jupyter_code>def get_similar_beers(arr_favorite_beers):
    
    '''
        Description: Based on user reviews, this function provides the most similar beers to the target, selected beer.
        
        Args:
            - arr_favorite_beers (list): list of target beers to recommend similar items
        
        Returns: 
            - top_10_most_similar (list): returns a list of length 10 with the 10 most similar beers to the target
    
    '''
    
    # Selected the favorite (target) beers passed as argument
    df_selected = corr_matrix[arr_favorite_beers]
    
    # Finding the most similar items
    df_ranked = df_selected.sum(axis=1).sort_values(ascending=False)
    
    # Removing items the customer already likes
    similar_beers_df = df_ranked[df_ranked.index.isin(favorite_beers)==False].index
    
    # Selecting the top 10 most similar beers
    top_10_most_similar = similar_beers_df[:10].tolist()
    
    # Returns the result
    return top_10_most_similar<jupyter_output><empty_output><jupyter_text>List of beers I like and want to get similar items<jupyter_code># The 'target' beer
favorite_beers = ["Budweiser"]
# Returns the top most similar beers to the target
ranked_beers = get_similar_beers(favorite_beers)<jupyter_output><empty_output><jupyter_text>Printing the most similar beers to the ones I selected, based on the reviews of the other items<jupyter_code># Printing top 10 most similar beers to the target
for _ in range(len(ranked_beers)):
    print("{}. {}".format((_+1), ranked_beers[_]))<jupyter_output>1. Bud Light
2. Heineken Lager Beer
3. <NAME> Boston Lager
4. Miller High Life
5. Corona Extra
6. Pabst Blue Ribbon (PBR)
7. Coors Light
8. Guinness Draught
9. Red Stripe Jamaican Lager
10. Miller Lite
{'fraction_non_alphanumeric': 0.08045397225725094, 'fraction_numerical': 0.012862547288776798, 'mean_word_length': 5.265402843601896, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '127074', 'license': 'Unlicense', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1244, 'n_words': 395, 'path': 'Recommender Systems/Beer Recommender .ipynb', 'repo_name': 'luizhsda10/luizhsda10.github.io', 'size': '3971'}