Financial Forecasting with Machine Learning using Python (Numpy, Pandas, Matplotlib and Scikit-learn)

Lyron Foster
3 min readMar 28, 2023

In this tutorial, we will explore how machine learning can be used for financial forecasting using Python. We will begin by loading financial data from an API and preprocessing it for machine learning, which includes normalization and splitting the data into training and validation sets.

Then, we will define a machine learning model using an LSTM-based neural network architecture and train it on the preprocessed data. After evaluating the model’s performance on the validation set, we will use it to make predictions on new data.

Sounds cool, right?

Alright let’s go!

Step 1. Import the required libraries:

First, you need to import the required libraries, including numpy, pandas, matplotlib, and scikit-learn.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense, LSTM

Step 2. Load the data:

Next, you need to load the financial data that you will use for forecasting. You can use a financial data API such as Alpha Vantage to load the stock market data for the company of interest.

import requests

api_key = 'YOUR_API_KEY'
symbol = 'AAPL'
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol={symbol}&outputsize=full&apikey={api_key}'
response = requests.get(url)
data = response.json()
df = pd.DataFrame(data['Time Series (Daily)']).transpose()
df.index = pd.to_datetime(df.index)
df = df.sort_index()

Step 3. Preprocess the data:

Once the data is loaded, you need to preprocess it so that it can be used for training the machine learning model. This involves tasks such as feature engineering, normalization, and splitting the data into training and validation sets.

# Extract the closing prices
y = df['4. close'].values.astype(float)

# Normalize the closing prices
scaler = MinMaxScaler(feature_range=(0, 1))
y = scaler.fit_transform(y.reshape(-1, 1))
# Create the feature matrix
X = []
for i in range(60, len(df)):
X.append(y[i-60:i, 0])
X = np.array(X)
# Split the data into training and validation sets
X_train, X_val, y_train, y_val = train_test_split(X, y[60:], test_size=0.2, shuffle=False)

Step 4. Define the model:

Once the data is preprocessed, you need to define the architecture of the machine learning model. For this example, we will use a recurrent neural network (RNN) with LSTM cells.

# Define the model
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))
model.add(LSTM(units=50))
model.add(Dense(units=1))
model.compile(optimizer='adam', loss='mean_squared_error')

Step 5. Train the model:

Once the model is defined, you need to train it using the preprocessed dat

# Train the model
model.fit(X_train, y_train, epochs=100, batch_size=32, validation_data=(X_val, y_val))

Step 6. Evaluate the model:

After training the model, you need to evaluate its performance on the validation set.

# Evaluate the model on the validation set
y_pred = model.predict(X_val)
rmse = np.sqrt(mean_squared_error(y_val, y_pred))
print('Root Mean Squared Error:', rmse)

Step 7. Visualize the results:

Once the model is trained and evaluated, you can visualize the results to see how well the model is able to forecast the financial data.

# Visualize the results
y_pred = scaler.inverse_transform(y_pred)
y_val = scaler.inverse_transform(y_val)
plt.plot(y_val, label='Actual')
plt.plot(y_pred, label='Predicted')
plt.legend()
plt.show()

Step 8. Make predictions:

Once you are satisfied with the model’s performance on the validation set, you can use it to make predictions on new data.

# Make predictions
last_60_days = y[-60:]
last_60_days_scaled = scaler.transform(last_60_days.reshape(-1, 1))
X_test = []
X_test.append(last_60_days_scaled)
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
y_pred = model.predict(X_test)
y_pred = scaler.inverse_transform(y_pred)
print('Predicted price:', y_pred[0][0])

This example should give you an idea of how to use machine learning for financial forecasting using Python. With some domain knowledge and creativity, you can use machine learning for a variety of financial forecasting tasks, including predicting stock prices, market trends, and other financial indicators.

If you found this article interesting, then you might find the book: Algorithmic Trading by Lyron Foster a good read.

--

--

Lyron Foster

Founder & CEO at Tech Guys 2 Go, LLC | Author | Multinational Entrepreneur | A.I. Engineer | Programmer | Server Engineer | DevOps Engineer | InfoSec Ninja