googlestocks / app.py
samarthv's picture
Update app.py
a3f6dd5
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Set a title for the app
st.title("Stock Price Prediction")
# 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 and table
display_graphs = False
# Create a submit button
if st.button('Submit'):
display_graphs = True
# Display the image as the background
st.markdown(
"""
<style>
.stApp {
background-image: url('https://img.etimg.com/thumb/msid-96018223,width-1200,height-900,imgsize-117108,resizemode-8,quality-100/markets/stocks/news/traders-guide-these-2-stocks-have-up-to-6-upside-scope.jpg');
background-size: cover;
}
</style>
""",
unsafe_allow_html=True
)
# Display the graphs and table if the submit button is clicked
if display_graphs:
# Create subplots with increased spacing between subplots
fig, axes = plt.subplots(3, 1, figsize=(10, 20), sharex=True, gridspec_kw={'hspace': 0.5})
# 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_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_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')
# Remove the background image
st.markdown(
"""
<style>
.stApp {
background-image: none;
}
</style>
""",
unsafe_allow_html=True
)
# Display the graphs in Streamlit
st.pyplot(fig)
# Display the table of price details and predicted prices
st.subheader("Price Details and Predicted Prices")
price_table = pd.DataFrame({'Date': selected_data['Date'], 'Actual Price': y, 'Predicted Price': model.predict(X)})
st.dataframe(price_table)