This file contains hidden or 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 pandas as pd |
This file contains hidden or 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
| finance_news = pd.read_csv('news.csv', encoding = 'ISO-8859-1') | |
| finance_news.head() |
This file contains hidden or 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
| X = finance_news['headline'].to_list() | |
| y = finance_news['sentiment'].to_list() |
This file contains hidden or 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
| from transformers import BertTokenizer, BertForSequenceClassification | |
| finbert = BertForSequenceClassification.from_pretrained('yiyanghkust/finbert-tone',num_labels=3) | |
| tokenizer = BertTokenizer.from_pretrained('yiyanghkust/finbert-tone') |
This file contains hidden or 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
| labels = {0:'neutral', 1:'positive',2:'negative'} |
This file contains hidden or 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
| sent_val = list() | |
| for x in X: | |
| inputs = tokenizer(x, return_tensors="pt", padding=True) | |
| outputs = finbert(**inputs)[0] | |
| val = labels[np.argmax(outputs.detach().numpy())] | |
| print(x, '----', val) | |
| print('#######################################################') | |
| sent_val.append(val) |
This file contains hidden or 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
| from sklearn.metrics import accuracy_score | |
| print(accuracy_score(y, sent_val)) |
This file contains hidden or 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
| from bs4 import BeautifulSoup | |
| import pandas as pd | |
| from urllib.request import urlopen, Request |
This file contains hidden or 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
| def get_ticks(url): | |
| req = Request(url=url,headers={"User-Agent": "Chrome"}) | |
| response = urlopen(req) | |
| html = BeautifulSoup(response,"html.parser") | |
| ticks_table = html.find(class_ = 'page') | |
| ticks = list() | |
| stocks = list() | |
| for name_box in ticks_table.find_all('a', href = True): | |
| stocks.append(name_box.text.strip()) |
This file contains hidden or 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
| url = "https://www.tickertape.in/stocks" | |
| tick_df = get_ticks(url) | |
| tick_df.head() |
OlderNewer