File size: 2,803 Bytes
33d05f9
efa7e3e
33d05f9
 
 
 
 
 
 
efa7e3e
 
33d05f9
efa7e3e
33d05f9
 
 
 
 
efa7e3e
33d05f9
 
 
 
 
efa7e3e
 
33d05f9
 
 
5790634
 
33d05f9
efa7e3e
33d05f9
 
 
efa7e3e
33d05f9
 
 
 
 
 
 
 
 
efa7e3e
33d05f9
5790634
 
33d05f9
5790634
33d05f9
 
efa7e3e
33d05f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# %%
# Importing necessary libraries
import pandas as pd               # For data manipulation using DataFrames
import numpy as np                # For numerical operations
import matplotlib.pyplot as plt   # For data visualization
import os                         # For operating system-related tasks
import joblib                     # For saving and loading models
import hopsworks                  # For getting access to hopsworks

from feature_pipeline import tesla_fg   #Loading in the tesla_fg
from feature_pipeline import news_sentiment_fg  #Loading in the news_fg

#Making the notebook able to fetch from the .env file
from dotenv import load_dotenv
import os

load_dotenv()

#Getting connected to hopsworks
api_key = os.environ.get('hopsworks_api')
project = hopsworks.login(api_key_value=api_key)
fs = project.get_feature_store()

# %%
#Defining the function to create feature view

def create_stocks_feature_view(fs, version):

    # Loading in the feature groups
    tesla_fg = fs.get_feature_group('tesla_stock', version=5)
    news_sentiment_fg = fs.get_feature_group('news_sentiment_updated', version=5)

    # Defining the query
    ds_query = tesla_fg.select(['date', 'open', 'ticker'])\
        .join(news_sentiment_fg.select(['sentiment']))

    # Creating the feature view
    feature_view = fs.create_feature_view(
        name='tesla_stocks_fv',
        query=ds_query,
        labels=['open']
    )

    return feature_view, tesla_fg

# %%
#Creating the feature view
try:
    feature_view = fs.get_feature_view("tesla_stocks_fv", version=5)
    tesla_fg = fs.get_feature_group('tesla_stock', version=5)
except:
    feature_view, tesla_fg = create_stocks_feature_view(fs, 5)

# %%
#Defining a function to get fixed data from the feature view
def fix_data_from_feature_view(df,start_date,end_date):
    df = df.sort_values("date")
    df = df.reset_index()
    df = df.drop(columns=["index"])

    # Create a boolean mask for rows that fall within the date range
    mask = (pd.to_datetime(df['date']) >= pd.to_datetime(start_date)) & (pd.to_datetime(df['date']) <= pd.to_datetime(end_date))
    len_df = np.shape(df)
    df = df[mask] # Use the boolean mask to filter the DataFrame
    print('From shape {} to {} after cropping to given date range: {} to {}'.format(len_df,np.shape(df),start_date,end_date))

    # Get rid off all non-business days
    isBusinessDay, is_open = extract_business_day(start_date,end_date)
    is_open = [not i for i in is_open] # Invert the mask to be able to drop all non-buisiness days

    filtered_df = df.drop(df[is_open].index) # Use the mask to filter the rows of the DataFrame
    print('From shape {} to {} after removing non-business days'.format(np.shape(df),np.shape(filtered_df)))
    print(filtered_df)
    
    return filtered_df