googlestocks / app.py
samarthv's picture
Update app.py
a57a319
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Remove the Streamlit default layout
st.set_page_config(layout="wide")
# Load the stock data from the CSV files
data1 = pd.read_csv('Google_test_data.csv')
data2 = pd.read_csv('tesla.csv')
# Combine the datasets into a dictionary
datasets = {'Google': data1, 'Tesla': data2}
# Get the user's dataset selection
selected_dataset = st.selectbox('Select Stock', list(datasets.keys()))
# Retrieve the selected dataset
selected_data = datasets[selected_dataset]
# Prepare the data for prediction
X = np.arange(len(selected_data)).reshape(-1, 1)
y = selected_data['Close']
# Train the linear regression model
model = LinearRegression()
model.fit(X, y)
# Define a flag for displaying the graphs
display_graphs = False
# Create a submit button
if st.button('Submit'):
display_graphs = True
# Display the graphs if the submit button is clicked
if display_graphs:
# Create subplots
fig, axes = plt.subplots(3, 1, figsize=(10, 15))
# Plot 1: Actual and Predicted Stock Prices
axes[0].plot(selected_data['Date'], y, label='Actual')
axes[0].plot(selected_data['Date'], model.predict(X), label='Predicted')
axes[0].set_xlabel('Date')
axes[0].set_ylabel('Stock Price')
axes[0].set_title(f'{selected_dataset} Stock Price Prediction')
axes[0].legend()
# Plot 2: Volume of Trades
axes[1].plot(selected_data['Date'], selected_data['Volume'])
axes[1].set_xlabel('Date')
axes[1].set_ylabel('Volume')
axes[1].set_title(f'{selected_dataset} Volume of Trades')
# Plot 3: Daily Percentage Change in Stock Prices
daily_returns = selected_data['Close'].pct_change() * 100
axes[2].plot(selected_data['Date'], daily_returns)
axes[2].set_xlabel('Date')
axes[2].set_ylabel('Percentage Change')
axes[2].set_title(f'{selected_dataset} Daily Percentage Change in Stock Prices')
# Display the graphs in Streamlit
st.pyplot(fig)