ibrahim yıldız
Upload 2 files
a575745 verified
import numpy as np
import pandas as pd
import streamlit as st
import plotly.express as px
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
import yfinance as yf
from sklearn.linear_model import LinearRegression
# Streamlit app
st.title('CUSTOM Stock Price Prediction 💰')
st.write('This model predicts upon trends. It will not perform well in volatile history. setting the time frame "max" is recommended. Your predicted days value can not exceed the time frame days. Have fun!')
# Input widgets
stock = st.text_input('Stock tag', value='NVDA')
daysago = st.text_input('Time frame in days (write "max" for max time)', value='365')
forecast_out = st.number_input('Predicted days', value=180,min_value=1)
forecast_col = 'Close'
def prepare_data(df,forecast_col,forecast_out):
label = df[forecast_col].shift(-forecast_out) #creating new column called label with the last 5 rows are nan
X = np.array(df[[forecast_col]]) #creating the feature array
X = preprocessing.scale(X) #processing the feature array
X_lately = X[-forecast_out:] #creating the column i want to use later in the predicting method
X = X[:-forecast_out] # X that will contain the training and testing
label.dropna(inplace=True) #dropping na values
y = np.array(label) # assigning Y
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.2, random_state=42) #cross validation
response = [X_train,X_test , Y_train, Y_test , X_lately]
return response
# Button to trigger model generation and prediction
if st.button('Generate'):
# Fetch stock data
if daysago != 'max':
daysago = str(daysago) + 'd'
ticker = yf.Ticker(stock)
data = ticker.history(period=daysago)
X_train, X_test, Y_train, Y_test , X_lately =prepare_data(data,forecast_col,forecast_out); #calling the method were the cross validation and data preperation is in
# Model Generation
learner = LinearRegression()
learner.fit(X_train, Y_train)
score = learner.score(X_test, Y_test)
forecast = learner.predict(X_lately)
#st.write('Used Model:', selected_algorithm)
st.write('Accuracy Score:', score)
#GRAPH
# Create a DataFrame with future dates and predicted values
future_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=forecast_out, freq='D')
predicted_data = pd.DataFrame({'Date': future_dates, 'Predicted Close': forecast})
# Concatenate original data and predicted data
combined_data = pd.concat([data.rename(columns={'Close': 'Actual Close'}), predicted_data.set_index('Date')], axis=1)
# Plot original and predicted stock prices
fig = px.line(combined_data, x=combined_data.index, y=['Actual Close', 'Predicted Close'], title=f'Predicted {stock} Stock Prices')
fig.update_layout(xaxis_title='Date',yaxis_title='Price',legend_title_text='')
# Set line colors
fig.data[1].line.color = 'orange'
st.plotly_chart(fig)
st.write('Findings: I tried using pycaret.regression to model, interestingly the r2 score of KNeighborsRegressor() was always the highest, with 96%, but it clearly gave a wrong output. Methods other than Ridge, Lasso and Linear Regression seem to always fail so I decied to stick with Linear Regression despite its 88% acc. score. It is not always correct, if the stock price has lots of ups and downs, it wont be able to give a good estimate as the accuracy score often goes below 50%. The discrepancy can be understood by the disconnection between the lines actual close and predicted close. It needs deep learning to go ruther.')