Update app.py
Browse files
app.py
CHANGED
|
@@ -2,21 +2,18 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
-
from tensorflow.keras.models import Sequential
|
| 6 |
-
from tensorflow.keras.layers import Dense, LSTM
|
| 7 |
from sklearn.preprocessing import MinMaxScaler
|
| 8 |
from sklearn.metrics import mean_squared_error
|
| 9 |
-
|
|
|
|
| 10 |
import io
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
dataY.append(dataset[i + look_back, 0])
|
| 19 |
-
return np.array(dataX), np.array(dataY)
|
| 20 |
|
| 21 |
def train_and_predict(file):
|
| 22 |
# 載入和預處理數據
|
|
@@ -27,50 +24,42 @@ def train_and_predict(file):
|
|
| 27 |
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 28 |
dataset = scaler.fit_transform(dataset)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
trainX, trainY = create_dataset(train, look_back)
|
| 38 |
-
testX, testY = create_dataset(test, look_back)
|
| 39 |
-
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
|
| 40 |
-
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
model =
|
| 44 |
-
model.
|
| 45 |
-
model.add(Dense(1))
|
| 46 |
-
model.compile(loss='mean_squared_error', optimizer='adam')
|
| 47 |
-
model.fit(trainX, trainY, epochs=50, batch_size=1, verbose=0)
|
| 48 |
|
| 49 |
# 進行預測
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
# 反轉預測
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
# 計算 RMSE
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
# 準備繪圖數據
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
-
return scaler.inverse_transform(dataset),
|
| 72 |
|
| 73 |
-
st.title("
|
| 74 |
|
| 75 |
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 76 |
|
|
@@ -104,11 +93,11 @@ else:
|
|
| 104 |
st.info("Please upload a CSV file to start the prediction.")
|
| 105 |
|
| 106 |
st.markdown("""
|
| 107 |
-
This application uses
|
| 108 |
|
| 109 |
To use:
|
| 110 |
1. Upload a CSV file containing historical stock price data.
|
| 111 |
-
2. The app will train
|
| 112 |
3. You'll see a graph showing the original data and predictions, along with RMSE scores for training and test sets.
|
| 113 |
|
| 114 |
Note: The CSV file should have the stock prices in the second column.
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 5 |
from sklearn.preprocessing import MinMaxScaler
|
| 6 |
from sklearn.metrics import mean_squared_error
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.linear_model import LinearRegression
|
| 9 |
import io
|
| 10 |
|
| 11 |
+
def create_features(data, look_back=1):
|
| 12 |
+
X, y = [], []
|
| 13 |
+
for i in range(len(data) - look_back):
|
| 14 |
+
X.append(data[i:(i + look_back)])
|
| 15 |
+
y.append(data[i + look_back])
|
| 16 |
+
return np.array(X), np.array(y)
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def train_and_predict(file):
|
| 19 |
# 載入和預處理數據
|
|
|
|
| 24 |
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 25 |
dataset = scaler.fit_transform(dataset)
|
| 26 |
|
| 27 |
+
# 創建特徵和目標
|
| 28 |
+
look_back = 3
|
| 29 |
+
X, y = create_features(dataset, look_back)
|
|
|
|
| 30 |
|
| 31 |
+
# 分割訓練集和測試集
|
| 32 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# 創建和訓練模型
|
| 35 |
+
model = LinearRegression()
|
| 36 |
+
model.fit(X_train, y_train)
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# 進行預測
|
| 39 |
+
train_predict = model.predict(X_train)
|
| 40 |
+
test_predict = model.predict(X_test)
|
| 41 |
|
| 42 |
# 反轉預測
|
| 43 |
+
train_predict = scaler.inverse_transform(train_predict.reshape(-1, 1))
|
| 44 |
+
y_train = scaler.inverse_transform(y_train.reshape(-1, 1))
|
| 45 |
+
test_predict = scaler.inverse_transform(test_predict.reshape(-1, 1))
|
| 46 |
+
y_test = scaler.inverse_transform(y_test.reshape(-1, 1))
|
| 47 |
|
| 48 |
# 計算 RMSE
|
| 49 |
+
train_score = np.sqrt(mean_squared_error(y_train, train_predict))
|
| 50 |
+
test_score = np.sqrt(mean_squared_error(y_test, test_predict))
|
| 51 |
|
| 52 |
# 準備繪圖數據
|
| 53 |
+
train_predict_plot = np.empty_like(dataset)
|
| 54 |
+
train_predict_plot[:, :] = np.nan
|
| 55 |
+
train_predict_plot[look_back:len(train_predict)+look_back, :] = train_predict
|
| 56 |
+
test_predict_plot = np.empty_like(dataset)
|
| 57 |
+
test_predict_plot[:, :] = np.nan
|
| 58 |
+
test_predict_plot[len(train_predict)+(look_back*2)+1:len(dataset)-1, :] = test_predict
|
| 59 |
|
| 60 |
+
return scaler.inverse_transform(dataset), train_predict_plot, test_predict_plot, train_score, test_score
|
| 61 |
|
| 62 |
+
st.title("Stock Price Prediction")
|
| 63 |
|
| 64 |
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 65 |
|
|
|
|
| 93 |
st.info("Please upload a CSV file to start the prediction.")
|
| 94 |
|
| 95 |
st.markdown("""
|
| 96 |
+
This application uses a simple linear regression model to predict stock prices based on historical data.
|
| 97 |
|
| 98 |
To use:
|
| 99 |
1. Upload a CSV file containing historical stock price data.
|
| 100 |
+
2. The app will train a model on this data and make predictions.
|
| 101 |
3. You'll see a graph showing the original data and predictions, along with RMSE scores for training and test sets.
|
| 102 |
|
| 103 |
Note: The CSV file should have the stock prices in the second column.
|