Created
December 20, 2022 09:15
-
-
Save e96031413/883a7cd06941071c83ed634fe2118e10 to your computer and use it in GitHub Desktop.
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
import pandas as pd | |
import numpy as np | |
from sklearn.model_selection import train_test_split | |
from sklearn.linear_model import LinearRegression | |
from sklearn.metrics import mean_absolute_error, mean_squared_error | |
from FinMind.data import DataLoader | |
dl = DataLoader() | |
stock_data = dl.taiwan_stock_daily( | |
stock_id='2330', start_date='2010-01-01', end_date='2022-12-20' | |
) | |
tsmc = stock_data | |
tsmc.dropna(inplace=True) | |
# Remove outliers | |
tsmc = tsmc[tsmc["close"] > 0] | |
tsmc = tsmc.apply(pd.to_numeric, errors="coerce") | |
tsmc = tsmc.dropna(axis='columns') | |
# Split the data into a training set and a test set | |
train_data, test_data, train_target, test_target = train_test_split(tsmc.drop("close", axis=1), tsmc["close"], test_size=0.1) | |
# Create a linear regression model | |
model = LinearRegression() | |
# Train the model on the training data | |
model.fit(train_data, train_target) | |
# Make predictions on the test data | |
predictions = model.predict(test_data) | |
# Calculate the MAE and RMSE | |
mae = mean_absolute_error(test_target, predictions) | |
rmse = np.sqrt(mean_squared_error(test_target, predictions)) | |
print(f"MAE: {mae:.2f}") | |
print(f"RMSE: {rmse:.2f}") | |
# Make a prediction for the next day | |
next_day_prediction = model.predict(np.asarray(tsmc.iloc[-1].drop('close', axis=0)).reshape(1, -1)) | |
print(f"Prediction for next day: {next_day_prediction[0]:.2f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment