Skip to content

Instantly share code, notes, and snippets.

View devamitranjan's full-sized avatar

Amit Ranjan devamitranjan

  • Druva Data Solutions
  • Pune
View GitHub Profile
@devamitranjan
devamitranjan / UCB.py
Last active July 30, 2022 14:07
Upper Confidence Bound Implementation
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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import random
class ThompsonSampling:
def __init__(self,path,N,m):
self.__N = N
self.__m = m
self.__machine_selected = []
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium
def ScrapComments(path):
authors = []
try:
# Configuring the ChromeDriver
options = webdriver.ChromeOptions()
options.add_argument('-headless')
options.add_argument('-no-sandbox')
options.add_argument('-disable-dev-shm-usage')
#wd =
@devamitranjan
devamitranjan / ImportingSeleniumLibraries
Created December 12, 2020 18:44
Importing Libraries of Selenium which will be needed to perform scrapping of youtube comments
import time
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re
import pandas as pd
def create_df_author_comments1(data):
author_comment = []
author=[]
for item in data:
s = re.sub('\\n[0-9]+ (days|weeks|months|years|week|month|year|hours|hour|day|minutes|minute) ago' ,'',item)
s = re.sub('\\nREPLY','',s)
s = re.sub('\.\\n[0-9][\.]*[0-9]*[A-Z]*','',s)
t = s.split('\n')
import os
import googleapiclient.discovery
def google_api(id):
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
import pandas as pd
def create_df_author_comments():
authorname = []
comments = []
for i in range(len(response["items"])):
authorname.append(response["items"][i]["snippet"]["topLevelComment"]["snippet"]["authorDisplayName"])
comments.append(response["items"][i]["snippet"]["topLevelComment"]["snippet"]["textOriginal"])
df_1 = pd.DataFrame(comments, index = authorname,columns=["Comments"])
return df_1
df_1 = create_df_author_comments()
@devamitranjan
devamitranjan / ImportingDataFromJsonFiles.py
Created December 13, 2020 16:30
Loading the data into response variable from json file
import json
with open('response.json') as f:
response = json.load(f)
f.close()
import pandas as pd
def create_df_author_comments(response):
authorname = []
comments = []
for i in range(len(response["items"])):
authorname.append(response["items"][i]["snippet"]["topLevelComment"]["snippet"]["authorDisplayName"])
comments.append(response["items"][i]["snippet"]["topLevelComment"]["snippet"]["textOriginal"])
df_1 = pd.DataFrame(comments, index = authorname,columns=["Comments"])
return df_1
df = create_df_author_comments(response)