Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Sep 9 20:30:29 2019
  4.  
  5. @author: DASA0
  6. """
  7.  
  8. # Upper Confidence Bound in Reinforcement Learning
  9.  
  10. # Importing the libraries
  11. import numpy as np
  12. import matplotlib.pyplot as plt
  13. import pandas as pd
  14.  
  15. # Importing the dataset
  16. dataset = pd.read_csv('Ads_CTR_Optimisation.csv')
  17.  
  18. # Implementing Random Selection
  19. import math
  20. N = 10000
  21. d = 10
  22. ads_selected = []
  23. no_of_selections = [0]*d
  24. sum_rewards = [0]*d
  25. total_reward = 0
  26. for n in range(0, N):
  27. max_upper_bound = 0
  28. for i in range(0,d):
  29. if no_of_selections[i] > 0:
  30. avg_reward = sum_rewards[i] / no_of_selections[i]
  31. delta_i = math.sqrt(3/2 * math.log(n + 1) / no_of_selections[i])
  32. upper_bound = avg_reward + delta_i
  33. else:
  34. upper_bound = 1e400
  35. if upper_bound > max_upper_bound:
  36. max_upper_bound = upper_bound
  37. ad = i
  38. ads_selected.append(ad)
  39. no_of_selections[ad] = no_of_selections[ad] + 1
  40. reward = dataset.values[n, ad]
  41. sum_rewards[ad] = sum_rewards[ad] + reward
  42. total_reward = total_reward + reward
  43.  
  44.  
  45. # Visualising the results
  46. plt.hist(ads_selected)
  47. plt.title('Histogram of ads selections')
  48. plt.xlabel('Ads')
  49. plt.ylabel('Number of times each ad was selected')
  50. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement