-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a Web Page for predicting the stock price and also implemente…
…d concept of ensemble learning
- Loading branch information
Showing
9 changed files
with
7,452 additions
and
530 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from flask import Flask, render_template, request | ||
import numpy as np | ||
import pickle | ||
import os | ||
import pandas as pd | ||
from sklearn.model_selection import train_test_split | ||
import xgboost as xgb | ||
|
||
app = Flask(__name__) | ||
|
||
models_dir = "Stock-Price-Prediction/prediction.pkl" | ||
data_file = "Stock-Price-Prediction/Updated_SBIN.csv" | ||
|
||
df = pd.read_csv(data_file) | ||
print(df.columns) | ||
|
||
# Load the model | ||
try: | ||
with open(models_dir, "rb") as stock_file: | ||
stock_model = pickle.load(stock_file) | ||
except FileNotFoundError: | ||
stock_model = None | ||
|
||
def save_data(inputs, close_prediction): | ||
new_data = pd.DataFrame([inputs + [close_prediction]], columns=['Open', 'High', 'Low', 'Volume', 'Close']) | ||
with open(data_file, mode='a', newline='') as file: | ||
new_data.to_csv(file, header=False, index=False) | ||
|
||
def retrain_model(): | ||
X = df[['Open', 'High', 'Low', 'Volume']] | ||
y = df['Close'] | ||
|
||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | ||
|
||
stock_model_xgb = xgb.XGBRegressor(objective='reg:squarederror', random_state=42) | ||
stock_model_xgb.fit(X_train, y_train) | ||
|
||
with open(models_dir, "wb") as stock_file_xgb: | ||
pickle.dump(stock_model_xgb, stock_file_xgb) | ||
|
||
@app.route('/') | ||
def home(): | ||
return render_template('home.html') | ||
|
||
@app.route('/predict_close', methods=['GET', 'POST']) | ||
def predict_close(): | ||
if request.method == 'POST': | ||
try: | ||
inputs = [ | ||
float(request.form.get('Open')), | ||
float(request.form.get('High')), | ||
float(request.form.get('Low')), | ||
float(request.form.get('Volume')) | ||
] | ||
|
||
close_prediction = stock_model.predict(np.array([inputs]))[0] if stock_model else None | ||
|
||
save_data(inputs, close_prediction) | ||
retrain_model() | ||
|
||
return str(round(close_prediction, 2)) if close_prediction is not None else "Error: Stock model not loaded." | ||
except Exception as e: | ||
return f"An error occurred: {e}" | ||
# If it's a GET request, render the stock.html page | ||
return render_template('stock.html') | ||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
Flask==2.3.2 | ||
pandas==2.1.1 | ||
numpy==1.26.0 | ||
scikit-learn==1.3.0 | ||
statsmodels==0.14.0 | ||
pickle-mixin==1.0.2 | ||
pickle-mixin==1.0.2 | ||
xgboost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
body { | ||
font-family: 'Arial', sans-serif; | ||
background-color: #f0f8ff; /* Light blue background */ | ||
margin: 0; | ||
padding: 20px; | ||
} | ||
|
||
.container { | ||
max-width: 600px; | ||
margin: auto; | ||
background: white; | ||
padding: 30px; | ||
border-radius: 10px; | ||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); | ||
text-align: center; | ||
} | ||
|
||
h1 { | ||
color: #003366; /* Dark blue for heading */ | ||
font-size: 2.5em; | ||
} | ||
|
||
p { | ||
color: #555; /* Dark gray for paragraph */ | ||
font-size: 1.2em; | ||
} | ||
|
||
nav { | ||
margin-top: 20px; | ||
} | ||
|
||
ul { | ||
list-style-type: none; | ||
padding: 0; | ||
} | ||
|
||
li { | ||
display: inline; | ||
margin: 0 15px; | ||
} | ||
|
||
a { | ||
text-decoration: none; | ||
color: #0066cc; /* Blue link color */ | ||
font-weight: bold; | ||
} | ||
|
||
a:hover { | ||
color: #004999; /* Darker blue on hover */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
body { | ||
font-family: 'Verdana', sans-serif; | ||
background-color: #f5f5f5; /* Light gray background */ | ||
margin: 0; | ||
padding: 20px; | ||
} | ||
|
||
.container { | ||
max-width: 600px; | ||
margin: auto; | ||
background: white; | ||
padding: 30px; | ||
border-radius: 10px; | ||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); | ||
} | ||
|
||
h1 { | ||
color: #ff4500; /* Orange-red for heading */ | ||
font-size: 2.5em; | ||
text-align: center; | ||
} | ||
|
||
label { | ||
margin-top: 10px; | ||
display: block; | ||
font-weight: bold; | ||
color: #333; /* Dark gray for labels */ | ||
} | ||
|
||
input[type="text"] { | ||
width: 100%; | ||
padding: 10px; | ||
margin: 5px 0 15px 0; | ||
border: 1px solid #ccc; | ||
border-radius: 4px; | ||
} | ||
|
||
button { | ||
background-color: #28a745; /* Green button */ | ||
color: white; | ||
padding: 10px; | ||
border: none; | ||
border-radius: 4px; | ||
cursor: pointer; | ||
width: 100%; /* Full-width button */ | ||
} | ||
|
||
button:hover { | ||
background-color: #218838; /* Darker green on hover */ | ||
} | ||
|
||
.result { | ||
margin-top: 20px; | ||
padding: 10px; | ||
color: white; | ||
background-color: green; /* Green background for success */ | ||
border-radius: 4px; | ||
display: none; /* Initially hidden */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Stock Price Predictor</title> | ||
<link rel="stylesheet" href="static/css/home.css"> <!-- Link to your CSS --> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h1>Welcome to Our Stock Price Predictor</h1> | ||
<p>Use this tool to predict stock prices based on historical data.</p> | ||
<nav> | ||
<ul> | ||
<li><a href="{{ url_for('predict_close') }}">Stock Price Prediction</a></li> | ||
</ul> | ||
</nav> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" href="/static/css/stock.css"> <!-- Link to your CSS --> | ||
<title>Stock Price Prediction</title> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h1>Stock Price Prediction</h1> | ||
<form id="stockForm" method="post" onsubmit="send_data(event)"> | ||
<label for="Open">Open</label> | ||
<input type="number" step="0.01" id="Open" name="Open" required> | ||
|
||
<label for="High">High</label> | ||
<input type="number" step="0.01" id="High" name="High" required> | ||
|
||
<label for="Low">Low</label> | ||
<input type="number" step="0.01" id="Low" name="Low" required> | ||
|
||
<label for="Volume">Volume</label> | ||
<input type="number" step="1" id="Volume" name="Volume" required> | ||
|
||
<button type="submit" class="btn">Predict Stock Price</button> | ||
</form> | ||
<div id="prediction" class="prediction-result"></div> | ||
</div> | ||
|
||
<script> | ||
function send_data(event) { | ||
event.preventDefault(); // Prevent the default form submission | ||
var form = document.getElementById('stockForm'); | ||
var formData = new FormData(form); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('POST', '/predict_close', true); // Ensure this matches your Flask route | ||
document.getElementById("prediction").innerHTML = "Predicting Stock Price..."; | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState == XMLHttpRequest.DONE) { | ||
document.getElementById("prediction").innerHTML = "Predicted Stock Price: " + xhr.responseText; | ||
} | ||
} | ||
xhr.send(formData); | ||
} | ||
</script> | ||
</body> | ||
</html> |