Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.preprocessing import MinMaxScaler | |
| from keras.models import load_model | |
| import matplotlib.pyplot as plt | |
| from datetime import datetime | |
| import yfinance as yf | |
| st.title("Stock Price Predictor App") | |
| stock = st.text_input("Enter the Stock ID","GOOG") | |
| end = datetime.now() | |
| start = datetime(end.year-20,end.month,end.day) | |
| google_data = yf.download(stock,start,end) | |
| model = load_model("google_stock_prediction.keras") | |
| st.subheader("Stock Data") | |
| st.write(google_data) | |
| splitting_len = int(len(google_data)*0.7) | |
| x_test = pd.DataFrame(google_data.Close[splitting_len:]) | |
| def plot_graph(figsize,values,full_data,extra_data=0, extra_dataset =None): | |
| fig = plt.figure(figsize = figsize) | |
| plt.plot(values,"Orange") | |
| plt.plot(full_data.Close,'b') | |
| if extra_data: | |
| plt.plot(extra_dataset) | |
| return fig | |
| st.subheader("Original Close price and MA for 250 days") | |
| google_data['MA-250'] = google_data.Close.rolling(250).mean() | |
| st.pyplot(plot_graph((15,6),google_data['MA-250'],google_data,0)) | |
| st.subheader("Original Close price and MA for 200 days") | |
| google_data['MA-200'] = google_data.Close.rolling(200).mean() | |
| st.pyplot(plot_graph((15,6),google_data['MA-200'],google_data,0)) | |
| st.subheader("Original Close price and MA for 100 days") | |
| google_data['MA-100'] = google_data.Close.rolling(100).mean() | |
| st.pyplot(plot_graph((15,6),google_data['MA-100'],google_data,0)) | |
| st.subheader("Original Close price and MA for 250 days and 100 Days") | |
| st.pyplot(plot_graph((15,6),google_data['MA-250'],google_data,1,google_data["MA-100"])) | |
| scaler= MinMaxScaler(feature_range = (0,1)) | |
| scaled_data = scaler.fit_transform(x_test[['Close']]) | |
| x_data= [] | |
| y_data = [] | |
| for i in range(100,len(scaled_data)): | |
| x_data.append(scaled_data[i-100:i]) | |
| y_data.append(scaled_data[i]) | |
| x_data ,y_data = np.array(x_data),np.array(y_data) | |
| predictions = model.predict(x_data) | |
| inv_pre = scaler.inverse_transform(predictions) | |
| inv_y_test = scaler.inverse_transform(y_data) | |
| plotting_data = pd.DataFrame( | |
| { | |
| 'original_test_data':inv_y_test.reshape(-1), | |
| 'Predictions':inv_pre.reshape(-1) | |
| }, | |
| index = google_data.index[splitting_len+100:] | |
| ) | |
| st.subheader("Original values vs predicted values") | |
| st.write(plotting_data) | |
| st.subheader("Original close price vs predicted close price") | |
| fig = plt.figure(figsize = (15,6)) | |
| plt.plot(pd.concat([google_data.Close[:splitting_len+100],plotting_data],axis = 0)) | |
| plt.legend(["Data not used",'original test data','predicted test data']) | |
| st.pyplot(fig) |