halme commited on
Commit
e78f32a
·
verified ·
1 Parent(s): 98824cc

Create inference_data_pipeline.py

Browse files
Files changed (1) hide show
  1. inference_data_pipeline.py +92 -0
inference_data_pipeline.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Class to fetch news and stock data from the web for a specific ticker and combine them into a dataframe.
2
+
3
+ import pandas as pd
4
+ import yfinance as yf
5
+ from pygooglenews import GoogleNews
6
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
7
+ from transformers import pipeline
8
+ class InferenceDataPipeline:
9
+ def __init__(self, ticker, time_period_news, time_period_stock):
10
+ self.ticker = ticker
11
+ self.time_period_news = time_period_news
12
+ self.time_period_stock = time_period_stock
13
+
14
+ def get_data(self):
15
+ stock_data = self.get_stock_data()
16
+ news_data = self.get_news_data()
17
+ news_sentiment = self.get_sentiment(news_data)
18
+ combined_data = self.combine_data(stock_data, news_sentiment)
19
+
20
+ return combined_data
21
+
22
+
23
+ def get_stock_data(self):
24
+ data = yf.download(self.ticker , period = self.time_period_stock)
25
+ df = pd.DataFrame()
26
+ df['Open'] = data['Open']
27
+ df['Close'] = data['Close']
28
+ df['High'] = data['High']
29
+ df['Low'] = data['Low']
30
+ df['Volume'] = data['Volume']
31
+
32
+ return df
33
+
34
+ def get_news_data(self):
35
+ googlenews = GoogleNews()
36
+ news_data = googlenews.search(self.ticker, when=self.time_period_news)
37
+ news_data = pd.DataFrame(news_data['entries'])
38
+ return news_data
39
+
40
+ def get_sentiment(self, news_data):
41
+ tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
42
+ model = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert")
43
+ classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
44
+
45
+ news_sentiment = []
46
+ for i in range(len(news_data)):
47
+ sentiment = classifier(news_data['title'][i], top_k=None)
48
+ postive_score = sentiment[0]['score']
49
+ negative_score = sentiment[1]['score']
50
+ neutral_score = sentiment[2]['score']
51
+ reformmated_time_stamp = pd.to_datetime(news_data['published'][i]).date()
52
+ news_sentiment.append({'Date': reformmated_time_stamp, 'positive_score': postive_score, 'negative_score': negative_score, 'neutral_score': neutral_score})
53
+ return pd.DataFrame(news_sentiment)
54
+
55
+ def combine_data(self, stock_data, news_sentiment):
56
+ news_sentiment = (
57
+ news_sentiment
58
+ .groupby('Date')
59
+ .mean()
60
+ .fillna(0)
61
+ .reset_index()
62
+ .set_index('Date')
63
+ .sort_index()
64
+ )
65
+
66
+ common_index = pd.date_range(
67
+ start=pd.Timestamp(min(pd.Timestamp(stock_data.index[0]), pd.Timestamp(news_sentiment.index[0]))),
68
+ end=pd.Timestamp(max(pd.Timestamp(stock_data.index[-1]), pd.Timestamp(news_sentiment.index[-1]))),
69
+ freq='D'
70
+ )
71
+ stock_data = stock_data.reindex(common_index).fillna(-1)
72
+
73
+ news_sentiment = news_sentiment.reindex(common_index).fillna(0)
74
+
75
+ #Ensure stock_data and news_sentiment have combatile indices
76
+ stock_data.index = pd.to_datetime(stock_data.index).normalize()
77
+ news_sentiment.index = pd.to_datetime(news_sentiment.index).normalize()
78
+
79
+ combined_data = pd.merge(
80
+ stock_data,
81
+ news_sentiment,
82
+ how='left',
83
+ left_index=True,
84
+ right_index=True
85
+ )
86
+
87
+ #Drop all close values that are -1
88
+ combined_data = combined_data[combined_data['Close'] != -1]
89
+ #fill all missing values with 0
90
+ combined_data = combined_data.fillna(0)
91
+
92
+ return combined_data