Last active
July 30, 2022 14:07
-
-
Save devamitranjan/3104c39306cabd43bcb040124aabca27 to your computer and use it in GitHub Desktop.
Upper Confidence Bound Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import math | |
class UpperConfidenceBound: | |
def __init__(self,path, N, m): | |
self.__dataset = pd.read_csv(path) | |
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): | |
plt.hist(self.__movie_selected) | |
plt.title('Histogram of Movies Ranks') | |
plt.xlabel('Movies') | |
plt.ylabel('Number of times each Movie was liked') | |
plt.show() | |
path = "MovieReview.csv" | |
N = 100 | |
m = 5 | |
ucb = UpperConfidenceBound(path,N,m) | |
ucb.implement_ucb() | |
ucb.visualization() |
Nice, I've just added some parameters to the plot:
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()
I also thought to make implement_ucb
return the computed values...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the dataset.
https://drive.google.com/file/d/1knPA93wUSR8b1V-vMApkVphOdzU9s8Vt