-
Notifications
You must be signed in to change notification settings - Fork 2
Predictions #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Predictions #15
Changes from 3 commits
1c65319
510aeef
c73590c
a963b3e
5b05bf1
e196bc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import numpy as np | ||
| import pandas as pd | ||
| import matplotlib.pyplot as plt | ||
| from fbprophet import Prophet | ||
| import yfinance as yf | ||
| import math | ||
| import os | ||
| import sys | ||
| import pandas as pd | ||
ApurvShah007 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pd.options.mode.chained_assignment = None | ||
|
||
|
|
||
|
|
||
| class suppress_stdout_stderr(object): | ||
| ''' | ||
| Used to supress the verbose output of the inner workings of the FB prophet model | ||
| ''' | ||
| def __init__(self): | ||
| self.null_fds = [os.open(os.devnull, os.O_RDWR) for x in range(2)] | ||
| self.save_fds = (os.dup(1), os.dup(2)) | ||
|
|
||
| def __enter__(self): | ||
| os.dup2(self.null_fds[0], 1) | ||
| os.dup2(self.null_fds[1], 2) | ||
|
|
||
| def __exit__(self, *_): | ||
| os.dup2(self.save_fds[0], 1) | ||
| os.dup2(self.save_fds[1], 2) | ||
| os.close(self.null_fds[0]) | ||
| os.close(self.null_fds[1]) | ||
|
Comment on lines
+16
to
+28
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, we shouldn't be interacting with any
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only way to supress the output seems to be this. This is on the official fbprophet repo issue
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about this? This is from the same issue thread. Can you try this out? |
||
|
|
||
| def fb_predict(dfi, plot = False, days =1): | ||
ApurvShah007 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ''' | ||
| This function takes in: | ||
| dfi - A dataframe with just the column of close price named as Close and date as index | ||
| plot - Whether you want to plot the resulyts or not( Default is False) | ||
| days - Number of days into the future you want to predict (Dafault is 1) | ||
|
|
||
| This function returns: | ||
| mean_err - The mean percentage error in calculations | ||
| pred - A pandas datafrmae with predictions, upper and lower bounds | ||
|
|
||
| ''' | ||
|
Comment on lines
+32
to
+63
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job documenting the method! If you use PyCharm and initialize a multiline comment ( The benefit of doing that is that you can access the documentation of a function using the Typically, you'd want to add This article should help you get started.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, have a look and let me know if it looks good now. |
||
| df = dfi.copy() | ||
|
||
| arr = [i for i in range(len(df))] | ||
| ar = df.index | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we directly use |
||
| df.index= arr | ||
| df['ds'] = ar | ||
| df = df[['ds', 'Close']] | ||
| df.rename(columns = {"Close":'y'}, inplace = True) | ||
|
|
||
| train_length = math.floor(0.1*len(df)) | ||
| train = df[:-train_length] | ||
| test = df[-train_length:] | ||
| m = Prophet(daily_seasonality = True) | ||
|
||
| with suppress_stdout_stderr(): | ||
| m.fit(train) | ||
|
||
| future = m.make_future_dataframe(periods=train_length) | ||
| prediction = m.predict(future) | ||
| test_predict = prediction[-train_length:] | ||
| test['predict'] = test_predict['trend'] | ||
| test['error'] =((test['y'] - test['predict'])/test['y'])*100 | ||
| mean_err = round(test['error'].mean(),3) | ||
|
||
| mn = Prophet(daily_seasonality = True) | ||
| with suppress_stdout_stderr(): | ||
| mn.fit(df) | ||
| future = mn.make_future_dataframe(periods=days) | ||
|
||
| prediction = mn.predict(future) | ||
| pred = pd.DataFrame(prediction[-days:][['ds','trend', 'yhat_upper', 'yhat_lower']]) | ||
| pred.rename(columns = {'ds' : 'Date', 'trend':'Guess', 'yhat_lower':'Lower Bound', 'yhat_upper':'Upper Bound'}, inplace = True) | ||
| pred.set_index(np.arange(1,len(pred)+1), inplace = True) | ||
| if plot: | ||
| m.plot(prediction) | ||
| plt.title("Prediction of the AZPN Stock Price using the Prophet") | ||
| plt.xlabel("Date") | ||
| plt.ylabel("Close Stock Price") | ||
| plt.show() | ||
|
||
| return mean_err, pred | ||
|
|
||
| #Sample Run | ||
| # symbol = "AZPN" | ||
| # days = 5 | ||
| # stock = yf.Ticker(symbol) | ||
| # df = stock.history(period="max") | ||
| # df = df[['Close']] | ||
| # me , pred= fb_predict(df, days = days) | ||
| # print("\n The prediction for {} after {} days is :\n".format(symbol, days)) | ||
| # print(pred) | ||
| # print("\n Mean Percentage Error : ", me) | ||
ApurvShah007 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't be interacting with the system or calling any lower level functions since our packages only serve high level abstract functions. I would think about another way to suppress the logs. Perhaps try overriding the functions from
fbprophet?