Skip to content

Instantly share code, notes, and snippets.

@bitsnaps
Forked from devamitranjan/UCB.py
Last active July 30, 2022 14:08
Show Gist options
  • Save bitsnaps/5da224f42be53572fffac826ab2ae465 to your computer and use it in GitHub Desktop.
Save bitsnaps/5da224f42be53572fffac826ab2ae465 to your computer and use it in GitHub Desktop.
Upper Confidence Bound Implementation
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math
class UpperConfidenceBound:
def __init__(self, dataframe, N, m):
self.__dataset = dataframe
self.__N = N
self.__m = m
self.__movie_selected = []
self.__number_of_selection = [0] * self.__m
self.__sum_of_movie_rank = [0]*self.__m
def implement_ucb(self):
for user in range(1, self.__N + 1):
movie = -1
max_upper_bound = 0
for movie_index in range(0, self.__m):
if self.__number_of_selection[movie_index] > 0:
avg_rank = self.__sum_of_movie_rank[movie_index] / self.__number_of_selection[movie_index]
delta_i = math.sqrt(1.5 * (math.log(user) / self.__number_of_selection[movie_index]))
upper_bound = avg_rank + delta_i
else:
upper_bound = 1e500
if upper_bound > max_upper_bound:
max_upper_bound = upper_bound
movie = movie_index
self.__movie_selected.append(movie)
self.__number_of_selection[movie] += 1
ranks = self.__dataset.values[user-1, movie]
self.__sum_of_movie_rank[movie] += ranks
def visualization(self, title, xlabel='', ylabel=''):
plt.hist(self.__movie_selected)
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.show()
path = "../MovieReviews.csv"
movies_df = pd.read_csv(path)
N = 100
m = 5
ucb = UpperConfidenceBound(movies_df,N,m)
ucb.implement_ucb()
ucb.visualization(title='Histogram of Movies Ranks', xlabel='Movies', ylabel='Number of times each Movie was liked')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment