{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"mount_file_id":"1-xYBhRBy5YB4NVcKMbEtX0JK2PCdJlwn","authorship_tag":"ABX9TyPb5ABsL3lKeFpOiQHW+MOK"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","source":["from google.colab import drive\n","drive.mount('/content/drive')"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"FoCP7yMfWiJE","executionInfo":{"status":"ok","timestamp":1711708986004,"user_tz":-420,"elapsed":35961,"user":{"displayName":"Nhật Quang Đoàn","userId":"10175964550021301622"}},"outputId":"666b3789-6c87-4dd5-dece-00e681655aa8"},"execution_count":1,"outputs":[{"output_type":"stream","name":"stdout","text":["Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"]}]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"HvTyGwNh31Zp","executionInfo":{"status":"ok","timestamp":1709738807268,"user_tz":-420,"elapsed":481,"user":{"displayName":"Nhật Quang Đoàn","userId":"10175964550021301622"}},"outputId":"03207652-4d5a-4710-c1a9-2be08b779b91"},"outputs":[{"output_type":"execute_result","data":{"text/plain":["Ad 1 1703\n","Ad 2 1295\n","Ad 3 728\n","Ad 4 1196\n","Ad 5 2695\n","Ad 6 126\n","Ad 7 1112\n","Ad 8 2091\n","Ad 9 952\n","Ad 10 489\n","dtype: int64"]},"metadata":{},"execution_count":12}],"source":["# Importing the libraries\n","import numpy as np\n","import matplotlib.pyplot as plt\n","import pandas as pd\n","from __future__ import division\n","import random\n","import math\n","# Importing the dataset\n","dataset = pd.read_csv('Ads_Optimisation.csv')\n","len(dataset)\n","dataset.sum(axis=0)"]},{"cell_type":"code","source":["# Implementing Random Selection\n","N = 10000\n","def random_selection(tstart, tstop):\n"," ads_selected = []\n"," total_reward = 0\n"," for n in range(tstart, tstop):\n"," ad = random.randrange(10) # random select 0 - 9 (ad1 - ad10)\n"," ads_selected.append(ad)\n"," reward = dataset.values[n, ad]\n"," total_reward = total_reward + reward\n"," return ads_selected, total_reward;\n","# Observe reward of choosing ads randomly and never exploiting the optimal one\n","[ads, random_result] = random_selection(0,10000)\n","print(random_result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"eSxY-_ltXE4j","executionInfo":{"status":"ok","timestamp":1709742614212,"user_tz":-420,"elapsed":2,"user":{"displayName":"Nhật Quang Đoàn","userId":"10175964550021301622"}},"outputId":"959cb50f-880a-49eb-8e0d-dd06872c0d1c"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["1209\n"]}]},{"cell_type":"code","source":["# Implementing Greedy Algorithm\n","# t is the number of random exploration, greedy algorithm is applied for t+1\n","t = 500\n","[ads, total_reward] = random_selection(0,t)\n","print(\"The reward after\",t, \"actions is\", total)\n","sum_ads_ex = np.zeros((1,10))\n","\n","for i in range(0,t):\n"," if dataset.values[i,ads[i]] == 1:\n"," sum_ads_ex[0,ads[i]] = sum_ads_ex[0,ads[i]] + 1;\n","# print the accumulated reward for each ad\n","\n","print(sum_ads_ex)\n","ad_maxreward = sum_ads_ex.argmax()\n","total_rewardG = total_reward;\n","print(\"Ad\",ad_maxreward+1,\"provides max reward for\",t,\"actions\")\n","for i in range(t+1,N):\n"," reward = dataset.values[i, ad_maxreward];\n"," total_rewardG = total_rewardG + reward;\n","print(total_rewardG)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"pItzxtEwYSmh","executionInfo":{"status":"ok","timestamp":1709743969526,"user_tz":-420,"elapsed":517,"user":{"displayName":"Nhật Quang Đoàn","userId":"10175964550021301622"}},"outputId":"f14407fe-4062-49ad-e9e7-65ca18020d8e"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["The reward after 500 actions is 62\n","[[16. 9. 3. 8. 14. 2. 5. 17. 4. 3.]]\n","Ad 8 provides max reward for 500 actions\n","2070\n"]}]},{"cell_type":"code","source":["# Implementing epsilon- Greedy Algorithm\n","\n","epsilon = 0.03\n","if sum_ads_ex.max()/t < epsilon:\n"," # do random selection 500 times more\n"," [adse, total_rewarde] = random_selection(t,t*2)\n","\n","\n","total_reward = total_reward+total_rewarde\n","print(total_reward)\n","\n","\n","for i in range(t,t*2):\n"," if dataset.values[i,ads[i-t]] == 1:\n"," sum_ads_ex[0,ads[i-t]] = sum_ads_ex[0,ads[i-t]] + 1;\n","# print the accumulated reward for each ad\n","\n","print(sum_ads_ex)\n","ad_maxreward = sum_ads_ex.argmax()\n","total_rewardG = total_reward;\n","print(\"Ad\",ad_maxreward+1,\"provides max reward for\",t,\"actions\")\n","for i in range(t+1,N):\n"," reward = dataset.values[i, ad_maxreward];\n"," total_rewardG = total_rewardG + reward;\n","print(total_rewardG)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"VgYeR1Lhj_KF","executionInfo":{"status":"ok","timestamp":1709744006623,"user_tz":-420,"elapsed":475,"user":{"displayName":"Nhật Quang Đoàn","userId":"10175964550021301622"}},"outputId":"1c7cc938-48bf-4f51-8473-96f293c53c40"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["293\n","[[48. 29. 23. 24. 74. 2. 29. 65. 28. 15.]]\n","Ad 5 provides max reward for 500 actions\n","2857\n"]}]}]}