0% found this document useful (0 votes)
26 views132 pages

Project ML Code

Uploaded by

robert.yf.lu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views132 pages

Project ML Code

Uploaded by

robert.yf.lu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 132

8/22/24, 11:05 PM Project-ML-CODE

In [ ]: # Import essential libraries


import pandas as pd
import numpy as np

# Import TA-Lib for technical analysis


import talib as ta

# Import libraries for plotting and visualization


import matplotlib.pyplot as plt
import seaborn as sns

# Import libraries for machine learning preprocessing


from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, MinMaxScaler

# Import machine learning models and utilities


from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score, f1_score, roc_auc_score
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.metrics import classification_report, precision_score, recall_score, r

# Import MiniSom for Self-Organizing Maps (SOM)


from minisom import MiniSom

# Import TensorFlow
import tensorflow as tf

# Latex and Markdown display


from IPython.display import display, Math, Markdown

WARNING:tensorflow:From c:\Users\bob19\anaconda3\Lib\site-packages\keras\src\losses.
py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use t
f.compat.v1.losses.sparse_softmax_cross_entropy instead.

Step 1.1: Load the data from an Excel file


Load historical daily data of Guizhou Maotai from an Excel file.

In [ ]: # Load the historical daily data of Guizhou Maotai from an Excel file
file_path = 'data/600519.SH.xlsx'
data = pd.read_excel(file_path)

Step 1.2: Initial inspection of the data


file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 1/132
8/22/24, 11:05 PM Project-ML-CODE

Inspect and preprocess the data, ensuring the date column is in the correct format and the
necessary columns are present.

In [ ]: # make sure the date column is in datetime format


data['date'] = pd.to_datetime(data['date'])

# Get the data from 2012 to 2019


start_date = '2012-01-01'
end_date = '2019-12-31'
data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]

# sort the data by date in ascending order


data = data.sort_values('date')

# Ensure all column names are in lowercase


data.columns = data.columns.str.lower()

# Ensure all necessary columns are of correct data type


required_columns = ['open', 'high', 'low', 'close', 'volume']
for col in required_columns:
if col in data.columns:
data[col] = data[col].astype(float)

# Display the first few rows of the data to understand its structure
data.head()

Out[ ]: date open high close low volume

2508 2012-01-04 125.890804 126.725693 121.795244 121.617748 3387828.0

2509 2012-01-05 120.960354 121.749226 120.401570 119.882229 3012264.0

2510 2012-01-06 120.368700 123.622797 122.695872 119.415480 2519062.0

2511 2012-01-09 122.275141 123.649093 123.596501 119.494367 2616016.0

2512 2012-01-10 122.932534 127.968166 127.849835 122.347454 3923560.0

In [ ]: data.describe()

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 2/132


8/22/24, 11:05 PM Project-ML-CODE

Out[ ]: date open high close low volum

count 1945 1945.000000 1945.000000 1945.000000 1945.000000 1.945000e+0

2016-01-06
mean 343.831636 348.447328 344.355735 339.791978 3.950207e+0
06:56:04.935732736

2012-01-04
min 81.967920 83.057607 81.905849 81.388593 0.000000e+0
00:00:00

2014-01-09
25% 133.207590 135.213108 133.670677 131.527994 2.383883e+0
00:00:00

2016-01-06
50% 197.230775 202.281978 198.025872 193.899586 3.345317e+0
00:00:00

2018-01-02
75% 550.677366 561.690913 552.724050 546.088388 4.734770e+0
00:00:00

2019-12-31
max 1146.538136 1156.420158 1149.099451 1143.799856 2.891405e+0
00:00:00

std NaN 277.630109 280.924069 277.984950 274.538610 2.478505e+0

Step 1.3: Check for data types and missing


values
Ensure the data types are correct and check for any missing values in the dataset.

In [ ]: # Check the data types of each column


print(data.dtypes)

date datetime64[ns]
open float64
high float64
close float64
low float64
volume float64
dtype: object

In [ ]: # Check for missing values in the dataset


missing_values = data.isnull().sum()
print("Missing values in each column:")
print(missing_values)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 3/132


8/22/24, 11:05 PM Project-ML-CODE

Missing values in each column:


date 0
open 0
high 0
close 0
low 0
volume 0
dtype: int64

Step 2.1: Feature engineering - Technical


indicators
Calculate various technical indicators such as Bollinger Bands, Moving Averages, and others
using TA-Lib.

In [ ]: # Define a list of time periods to be used for applicable indicators


time_periods_short = [5, 10, 14]
time_periods_medium = [20, 30, 50, 60]
time_periods_long = [100, 120, 200]

# Bollinger Bands
for period in time_periods_short + time_periods_medium:
data[f'bb_upper_{period}'], data[f'bb_middle_{period}'], data[f'bb_lower_{perio

# Double Exponential Moving Average (DEMA)


for period in time_periods_short + time_periods_medium:
data[f'dema_{period}'] = ta.DEMA(data['close'], timeperiod=period)

# Exponential Moving Average (EMA)


for period in time_periods_short + time_periods_medium + time_periods_long:
data[f'ema_{period}'] = ta.EMA(data['close'], timeperiod=period)

# Hilbert Transform - Instantaneous Trendline (HT_TRENDLINE)


data['ht_trendline'] = ta.HT_TRENDLINE(data['close'])

# Kaufman Adaptive Moving Average (KAMA)


for period in time_periods_short + time_periods_medium:
data[f'kama_{period}'] = ta.KAMA(data['close'], timeperiod=period)

# Moving Average (SMA)


for period in time_periods_short + time_periods_medium + time_periods_long:
data[f'sma_{period}'] = ta.SMA(data['close'], timeperiod=period)

# MESA Adaptive Moving Average (MAMA)


data['mama'], data['fama'] = ta.MAMA(data['close'])

# Moving Average with Variable Period (MAVP)


data['mavp'] = ta.MAVP(data['close'], periods=ta.SMA(data['close'], timeperiod=30))

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 4/132


8/22/24, 11:05 PM Project-ML-CODE

Step 2.2: Calculate the slope and slope


change for SMA
Compute the slope and the rate of change of the slope for selected Simple Moving Averages
(SMA).

In [ ]: # Example SMA columns (use actual SMA columns in your dataset)


sma_columns = ['sma_5', 'sma_10', 'sma_20', 'sma_60', 'sma_120', 'sma_200']

# Calculate the slope for each SMA over the past 5 days
for sma in sma_columns:
data[f'{sma}_slope'] = data[sma].diff(periods=5) / 5 # Change in SMA value ove

# Calculate the slope change (rate of change of slope) for each SMA
for sma in sma_columns:
data[f'{sma}_slope_change'] = data[f'{sma}_slope'].diff() # Day-over-day slope

Step 3.1: MidPoint and MidPrice indicators


Generate MidPoint and MidPrice indicators over specified periods.

In [ ]: # MidPoint over Period


for period in time_periods_short + time_periods_medium:
data[f'midpoint_{period}'] = ta.MIDPOINT(data['close'], timeperiod=period)

# MidPrice over Period


for period in time_periods_short + time_periods_medium:
data[f'midprice_{period}'] = ta.MIDPRICE(data['high'], data['low'], timeperiod=

Step 3.2: ADX, ADXR, APO, Aroon, and


Aroon Oscillator
Calculate various momentum and directional movement indicators.

In [ ]: # ADX and ADXR (Average Directional Movement Index and Rating)


for period in time_periods_short + time_periods_medium:
data[f'adx_{period}'] = ta.ADX(data['high'], data['low'], data['close'], timepe
data[f'adxr_{period}'] = ta.ADXR(data['high'], data['low'], data['close'], time

# APO (Absolute Price Oscillator)


for fastperiod in [5, 10, 12]:
for slowperiod in [26, 50]:
data[f'apo_{fastperiod}_{slowperiod}'] = ta.APO(data['close'], fastperiod=f

# Aroon and Aroon Oscillator


for period in time_periods_short + time_periods_medium:

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 5/132


8/22/24, 11:05 PM Project-ML-CODE

data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'


data[f'aroonosc_{period}'] = ta.AROONOSC(data['high'], data['low'], timeperiod=

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 6/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:4: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'adxr_{period}'] = ta.ADXR(data['high'], data['low'], data['close'], timeper
iod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'adx_{period}'] = ta.ADX(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:4: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'adxr_{period}'] = ta.ADXR(data['high'], data['low'], data['close'], timeper
iod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'adx_{period}'] = ta.ADX(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:4: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'adxr_{period}'] = ta.ADXR(data['high'], data['low'], data['close'], timeper
iod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'apo_{fastperiod}_{slowperiod}'] = ta.APO(data['close'], fastperiod=fastperi
od, slowperiod=slowperiod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'apo_{fastperiod}_{slowperiod}'] = ta.APO(data['close'], fastperiod=fastperi
od, slowperiod=slowperiod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'apo_{fastperiod}_{slowperiod}'] = ta.APO(data['close'], fastperiod=fastperi
od, slowperiod=slowperiod)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 7/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'apo_{fastperiod}_{slowperiod}'] = ta.APO(data['close'], fastperiod=fastperi
od, slowperiod=slowperiod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'apo_{fastperiod}_{slowperiod}'] = ta.APO(data['close'], fastperiod=fastperi
od, slowperiod=slowperiod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'apo_{fastperiod}_{slowperiod}'] = ta.APO(data['close'], fastperiod=fastperi
od, slowperiod=slowperiod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:14: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroonosc_{period}'] = ta.AROONOSC(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 8/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:14: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroonosc_{period}'] = ta.AROONOSC(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:14: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroonosc_{period}'] = ta.AROONOSC(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:14: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroonosc_{period}'] = ta.AROONOSC(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 9/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:14: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroonosc_{period}'] = ta.AROONOSC(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:14: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroonosc_{period}'] = ta.AROONOSC(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:13: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroon_up_{period}'], data[f'aroon_down_{period}'] = ta.AROON(data['high'],
data['low'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\439499995.py:14: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'aroonosc_{period}'] = ta.AROONOSC(data['high'], data['low'], timeperiod=per
iod)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 10/132


8/22/24, 11:05 PM Project-ML-CODE

Step 3.3: Other momentum indicators


Calculate additional momentum indicators including BOP, CCI, CMO, and others.

In [ ]: # BOP (Balance of Power)


data['bop'] = ta.BOP(data['open'], data['high'], data['low'], data['close'])

# CCI (Commodity Channel Index)


for period in time_periods_short + time_periods_medium:
data[f'cci_{period}'] = ta.CCI(data['high'], data['low'], data['close'], timepe

# CMO (Chande Momentum Oscillator)


for period in time_periods_short + time_periods_medium:
data[f'cmo_{period}'] = ta.CMO(data['close'], timeperiod=period)

# Momentum Indicators (MOM, ROC, ROCP, ROCR, ROCR100)


for period in time_periods_short + time_periods_medium:
data[f'mom_{period}'] = ta.MOM(data['close'], timeperiod=period)
data[f'roc_{period}'] = ta.ROC(data['close'], timeperiod=period)
data[f'rocp_{period}'] = ta.ROCP(data['close'], timeperiod=period)
data[f'rocr_{period}'] = ta.ROCR(data['close'], timeperiod=period)
data[f'rocr100_{period}'] = ta.ROCR100(data['close'], timeperiod=period)

# RSI (Relative Strength Index)


for period in time_periods_short + time_periods_medium:
data[f'rsi_{period}'] = ta.RSI(data['close'], timeperiod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 11/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:2: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['bop'] = ta.BOP(data['open'], data['high'], data['low'], data['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'cci_{period}'] = ta.CCI(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'cci_{period}'] = ta.CCI(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'cci_{period}'] = ta.CCI(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'cci_{period}'] = ta.CCI(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'cci_{period}'] = ta.CCI(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'cci_{period}'] = ta.CCI(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'cci_{period}'] = ta.CCI(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:10: PerformanceWarni

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 12/132


8/22/24, 11:05 PM Project-ML-CODE

ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'cmo_{period}'] = ta.CMO(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'cmo_{period}'] = ta.CMO(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'cmo_{period}'] = ta.CMO(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'cmo_{period}'] = ta.CMO(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'cmo_{period}'] = ta.CMO(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'cmo_{period}'] = ta.CMO(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'cmo_{period}'] = ta.CMO(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:14: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'mom_{period}'] = ta.MOM(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'roc_{period}'] = ta.ROC(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 13/132


8/22/24, 11:05 PM Project-ML-CODE

using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram


e.copy()`
data[f'rocp_{period}'] = ta.ROCP(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:17: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr_{period}'] = ta.ROCR(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:18: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr100_{period}'] = ta.ROCR100(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:14: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'mom_{period}'] = ta.MOM(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'roc_{period}'] = ta.ROC(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocp_{period}'] = ta.ROCP(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:17: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr_{period}'] = ta.ROCR(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:18: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr100_{period}'] = ta.ROCR100(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:14: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'mom_{period}'] = ta.MOM(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 14/132


8/22/24, 11:05 PM Project-ML-CODE

data[f'roc_{period}'] = ta.ROC(data['close'], timeperiod=period)


C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocp_{period}'] = ta.ROCP(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:17: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr_{period}'] = ta.ROCR(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:18: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr100_{period}'] = ta.ROCR100(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:14: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'mom_{period}'] = ta.MOM(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'roc_{period}'] = ta.ROC(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocp_{period}'] = ta.ROCP(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:17: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr_{period}'] = ta.ROCR(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:18: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr100_{period}'] = ta.ROCR100(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:14: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'mom_{period}'] = ta.MOM(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:15: PerformanceWarni

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 15/132


8/22/24, 11:05 PM Project-ML-CODE

ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'roc_{period}'] = ta.ROC(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocp_{period}'] = ta.ROCP(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:17: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr_{period}'] = ta.ROCR(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:18: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr100_{period}'] = ta.ROCR100(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:14: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'mom_{period}'] = ta.MOM(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'roc_{period}'] = ta.ROC(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocp_{period}'] = ta.ROCP(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:17: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr_{period}'] = ta.ROCR(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:18: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr100_{period}'] = ta.ROCR100(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:14: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 16/132


8/22/24, 11:05 PM Project-ML-CODE

using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram


e.copy()`
data[f'mom_{period}'] = ta.MOM(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'roc_{period}'] = ta.ROC(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocp_{period}'] = ta.ROCP(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:17: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr_{period}'] = ta.ROCR(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:18: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rocr100_{period}'] = ta.ROCR100(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rsi_{period}'] = ta.RSI(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rsi_{period}'] = ta.RSI(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rsi_{period}'] = ta.RSI(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rsi_{period}'] = ta.RSI(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 17/132


8/22/24, 11:05 PM Project-ML-CODE

data[f'rsi_{period}'] = ta.RSI(data['close'], timeperiod=period)


C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rsi_{period}'] = ta.RSI(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4266680648.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'rsi_{period}'] = ta.RSI(data['close'], timeperiod=period)

Step 3.4: Stochastic Oscillators and other


indicators
Generate stochastic oscillators, TRIX, ULTOSC, WILLR, and other indicators.

In [ ]: # Stochastic Oscillator (STOCH, STOCHF, STOCHRSI)


data['stoch_slowk'], data['stoch_slowd'] = ta.STOCH(data['high'], data['low'], data
data['stochf_fastk'], data['stochf_fastd'] = ta.STOCHF(data['high'], data['low'], d
for period in time_periods_short + time_periods_medium:
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['

# TRIX (1-day Rate-Of-Change of a Triple Smooth EMA)


for period in time_periods_short + time_periods_medium:
data[f'trix_{period}'] = ta.TRIX(data['close'], timeperiod=period)

# ULTOSC (Ultimate Oscillator)


data['ultosc'] = ta.ULTOSC(data['high'], data['low'], data['close'], timeperiod1=7,

# WILLR (Williams' %R)


for period in time_periods_short + time_periods_medium:
data[f'willr_{period}'] = ta.WILLR(data['high'], data['low'], data['close'], ti

# AD and ADOSC (Chaikin A/D Line and Oscillator)


data['ad'] = ta.AD(data['high'], data['low'], data['close'], data['volume'])
for fastperiod in [3, 6]:
for slowperiod in [10, 20]:
data[f'adosc_{fastperiod}_{slowperiod}'] = ta.ADOSC(data['high'], data['low

# OBV (On Balance Volume)


data['obv'] = ta.OBV(data['close'], data['volume'])

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 18/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:2: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['stoch_slowk'], data['stoch_slowd'] = ta.STOCH(data['high'], data['low'], dat
a['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:2: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['stoch_slowk'], data['stoch_slowd'] = ta.STOCH(data['high'], data['low'], dat
a['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['stochf_fastk'], data['stochf_fastd'] = ta.STOCHF(data['high'], data['low'],
data['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['stochf_fastk'], data['stochf_fastd'] = ta.STOCHF(data['high'], data['low'],
data['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 19/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 20/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'stochrsi_k_{period}'], data[f'stochrsi_d_{period}'] = ta.STOCHRSI(data['clo
se'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'trix_{period}'] = ta.TRIX(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'trix_{period}'] = ta.TRIX(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'trix_{period}'] = ta.TRIX(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'trix_{period}'] = ta.TRIX(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'trix_{period}'] = ta.TRIX(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'trix_{period}'] = ta.TRIX(data['close'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:9: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'trix_{period}'] = ta.TRIX(data['close'], timeperiod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 21/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:12: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data['ultosc'] = ta.ULTOSC(data['high'], data['low'], data['close'], timeperiod1=
7, timeperiod2=14, timeperiod3=28)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'willr_{period}'] = ta.WILLR(data['high'], data['low'], data['close'], timep
eriod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'willr_{period}'] = ta.WILLR(data['high'], data['low'], data['close'], timep
eriod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'willr_{period}'] = ta.WILLR(data['high'], data['low'], data['close'], timep
eriod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'willr_{period}'] = ta.WILLR(data['high'], data['low'], data['close'], timep
eriod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'willr_{period}'] = ta.WILLR(data['high'], data['low'], data['close'], timep
eriod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'willr_{period}'] = ta.WILLR(data['high'], data['low'], data['close'], timep
eriod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'willr_{period}'] = ta.WILLR(data['high'], data['low'], data['close'], timep
eriod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 22/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:19: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data['ad'] = ta.AD(data['high'], data['low'], data['close'], data['volume'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'adosc_{fastperiod}_{slowperiod}'] = ta.ADOSC(data['high'], data['low'], dat
a['close'], data['volume'], fastperiod=fastperiod, slowperiod=slowperiod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'adosc_{fastperiod}_{slowperiod}'] = ta.ADOSC(data['high'], data['low'], dat
a['close'], data['volume'], fastperiod=fastperiod, slowperiod=slowperiod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'adosc_{fastperiod}_{slowperiod}'] = ta.ADOSC(data['high'], data['low'], dat
a['close'], data['volume'], fastperiod=fastperiod, slowperiod=slowperiod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:22: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'adosc_{fastperiod}_{slowperiod}'] = ta.ADOSC(data['high'], data['low'], dat
a['close'], data['volume'], fastperiod=fastperiod, slowperiod=slowperiod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3909819040.py:25: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data['obv'] = ta.OBV(data['close'], data['volume'])

In [ ]: # Hilbert Transform Indicators


data['ht_dcperiod'] = ta.HT_DCPERIOD(data['close'])
data['ht_dcphase'] = ta.HT_DCPHASE(data['close'])
data['ht_inphase'], data['ht_quadrature'] = ta.HT_PHASOR(data['close'])
data['ht_sine'], data['ht_leadsine'] = ta.HT_SINE(data['close'])
data['ht_trendmode'] = ta.HT_TRENDMODE(data['close'])

# MINUS_DI and PLUS_DI (Directional Indicators)


for period in time_periods_short + time_periods_medium:
data[f'minus_di_{period}'] = ta.MINUS_DI(data['high'], data['low'], data['close
data[f'plus_di_{period}'] = ta.PLUS_DI(data['high'], data['low'], data['close']

# MINUS_DM and PLUS_DM (Directional Movement)


for period in time_periods_short + time_periods_medium:

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 23/132


8/22/24, 11:05 PM Project-ML-CODE

data[f'minus_dm_{period}'] = ta.MINUS_DM(data['high'], data['low'], timeperiod=


data[f'plus_dm_{period}'] = ta.PLUS_DM(data['high'], data['low'], timeperiod=pe

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 24/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:2: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['ht_dcperiod'] = ta.HT_DCPERIOD(data['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['ht_dcphase'] = ta.HT_DCPHASE(data['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:4: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['ht_inphase'], data['ht_quadrature'] = ta.HT_PHASOR(data['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:4: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['ht_inphase'], data['ht_quadrature'] = ta.HT_PHASOR(data['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['ht_sine'], data['ht_leadsine'] = ta.HT_SINE(data['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['ht_sine'], data['ht_leadsine'] = ta.HT_SINE(data['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['ht_trendmode'] = ta.HT_TRENDMODE(data['close'])
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_di_{period}'] = ta.MINUS_DI(data['high'], data['low'], data['close'],
timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:11: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_di_{period}'] = ta.PLUS_DI(data['high'], data['low'], data['close'], t
imeperiod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 25/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_di_{period}'] = ta.MINUS_DI(data['high'], data['low'], data['close'],
timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:11: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_di_{period}'] = ta.PLUS_DI(data['high'], data['low'], data['close'], t
imeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_di_{period}'] = ta.MINUS_DI(data['high'], data['low'], data['close'],
timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:11: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_di_{period}'] = ta.PLUS_DI(data['high'], data['low'], data['close'], t
imeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_di_{period}'] = ta.MINUS_DI(data['high'], data['low'], data['close'],
timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:11: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_di_{period}'] = ta.PLUS_DI(data['high'], data['low'], data['close'], t
imeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_di_{period}'] = ta.MINUS_DI(data['high'], data['low'], data['close'],
timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:11: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_di_{period}'] = ta.PLUS_DI(data['high'], data['low'], data['close'], t
imeperiod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 26/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_di_{period}'] = ta.MINUS_DI(data['high'], data['low'], data['close'],
timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:11: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_di_{period}'] = ta.PLUS_DI(data['high'], data['low'], data['close'], t
imeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_di_{period}'] = ta.MINUS_DI(data['high'], data['low'], data['close'],
timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:11: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_di_{period}'] = ta.PLUS_DI(data['high'], data['low'], data['close'], t
imeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_dm_{period}'] = ta.MINUS_DM(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_dm_{period}'] = ta.PLUS_DM(data['high'], data['low'], timeperiod=perio
d)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_dm_{period}'] = ta.MINUS_DM(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_dm_{period}'] = ta.PLUS_DM(data['high'], data['low'], timeperiod=perio
d)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 27/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_dm_{period}'] = ta.MINUS_DM(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_dm_{period}'] = ta.PLUS_DM(data['high'], data['low'], timeperiod=perio
d)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_dm_{period}'] = ta.MINUS_DM(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_dm_{period}'] = ta.PLUS_DM(data['high'], data['low'], timeperiod=perio
d)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_dm_{period}'] = ta.MINUS_DM(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_dm_{period}'] = ta.PLUS_DM(data['high'], data['low'], timeperiod=perio
d)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_dm_{period}'] = ta.MINUS_DM(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_dm_{period}'] = ta.PLUS_DM(data['high'], data['low'], timeperiod=perio
d)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 28/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:15: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'minus_dm_{period}'] = ta.MINUS_DM(data['high'], data['low'], timeperiod=per
iod)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\2078805182.py:16: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'plus_dm_{period}'] = ta.PLUS_DM(data['high'], data['low'], timeperiod=perio
d)

Step 4.1: Volatility indicators


Compute volatility indicators such as ATR, NATR, and TRANGE.

In [ ]: # Average True Range (ATR)


for period in time_periods_short + time_periods_medium:
data[f'atr_{period}'] = ta.ATR(data['high'], data['low'], data['close'], timepe

# Normalized Average True Range (NATR)


for period in time_periods_short + time_periods_medium:
data[f'natr_{period}'] = ta.NATR(data['high'], data['low'], data['close'], time

# True Range (TRANGE)


data['trange'] = ta.TRANGE(data['high'], data['low'], data['close'])

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 29/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'atr_{period}'] = ta.ATR(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'atr_{period}'] = ta.ATR(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'atr_{period}'] = ta.ATR(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'atr_{period}'] = ta.ATR(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'atr_{period}'] = ta.ATR(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'atr_{period}'] = ta.ATR(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:3: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'atr_{period}'] = ta.ATR(data['high'], data['low'], data['close'], timeperio
d=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:7: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'natr_{period}'] = ta.NATR(data['high'], data['low'], data['close'], timeper
iod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 30/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:7: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'natr_{period}'] = ta.NATR(data['high'], data['low'], data['close'], timeper
iod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:7: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'natr_{period}'] = ta.NATR(data['high'], data['low'], data['close'], timeper
iod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:7: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'natr_{period}'] = ta.NATR(data['high'], data['low'], data['close'], timeper
iod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:7: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'natr_{period}'] = ta.NATR(data['high'], data['low'], data['close'], timeper
iod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:7: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'natr_{period}'] = ta.NATR(data['high'], data['low'], data['close'], timeper
iod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:7: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'natr_{period}'] = ta.NATR(data['high'], data['low'], data['close'], timeper
iod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\3555839564.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data['trange'] = ta.TRANGE(data['high'], data['low'], data['close'])

Step 4.2: Ratio calculations


Calculate High-to-Low (H2L) and Open-to-Close (O2C) ratios, as well as daily returns.

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 31/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: # Calculate High-to-Low (H2L) ratio


data['h2l'] = data['high'] / data['low']

# Calculate Open-to-Close (O2C) ratio


data['o2c'] = data['open'] / data['close']

# Calculate the daily return as the percentage change in the closing price
data['return'] = data['close'].pct_change()

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4279441582.py:2: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['h2l'] = data['high'] / data['low']
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4279441582.py:5: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['o2c'] = data['open'] / data['close']
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4279441582.py:8: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['return'] = data['close'].pct_change()

Step 4.3: Volume indicators


Calculate Money Flow Index (MFI) and Volume Rate of Change (VROC) indicators.

In [ ]: # Define a list of time periods for volume-related indicators


time_periods_volume = [5, 10, 14, 20, 30, 50, 100]

# Money Flow Index (MFI)


for period in time_periods_volume:
data[f'mfi_{period}'] = ta.MFI(data['high'], data['low'], data['close'], data['

# Volume Rate of Change (VROC)


for period in time_periods_volume:
data[f'vroc_{period}'] = ta.ROC(data['volume'], timeperiod=period)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 32/132


8/22/24, 11:05 PM Project-ML-CODE

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'mfi_{period}'] = ta.MFI(data['high'], data['low'], data['close'], data['vol
ume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'mfi_{period}'] = ta.MFI(data['high'], data['low'], data['close'], data['vol
ume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'mfi_{period}'] = ta.MFI(data['high'], data['low'], data['close'], data['vol
ume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'mfi_{period}'] = ta.MFI(data['high'], data['low'], data['close'], data['vol
ume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'mfi_{period}'] = ta.MFI(data['high'], data['low'], data['close'], data['vol
ume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'mfi_{period}'] = ta.MFI(data['high'], data['low'], data['close'], data['vol
ume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:6: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data[f'mfi_{period}'] = ta.MFI(data['high'], data['low'], data['close'], data['vol
ume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'vroc_{period}'] = ta.ROC(data['volume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:10: PerformanceWarni

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 33/132


8/22/24, 11:05 PM Project-ML-CODE

ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'vroc_{period}'] = ta.ROC(data['volume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'vroc_{period}'] = ta.ROC(data['volume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'vroc_{period}'] = ta.ROC(data['volume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'vroc_{period}'] = ta.ROC(data['volume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'vroc_{period}'] = ta.ROC(data['volume'], timeperiod=period)
C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\4136467864.py:10: PerformanceWarni
ng: DataFrame is highly fragmented. This is usually the result of calling `frame.in
sert` many times, which has poor performance. Consider joining all columns at once
using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = fram
e.copy()`
data[f'vroc_{period}'] = ta.ROC(data['volume'], timeperiod=period)

Step 5.1: Drop NaN values


Ensure there are no NaN values left after feature generation.

In [ ]: # Final check to ensure no NaN values are left after feature generation
data = data.dropna()

# Display the first few rows of the final data with all features
data.head()

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 34/132


8/22/24, 11:05 PM Project-ML-CODE

Out[ ]: date open high close low volume bb_upper_5 bb_m

2012-
2713 157.575019 158.216029 156.313030 156.146100 1626181.0 167.051730 160
11-08

2012-
2714 156.306352 157.448152 156.787110 156.019233 1167006.0 162.486557 158
11-09

2012-
2715 156.913977 157.434798 154.550251 152.834213 2046433.0 159.423325 156
11-12

2012-
2716 154.243101 154.243101 152.280007 150.704190 2044777.0 159.967128 155
11-13

2012-
2717 152.493677 154.243101 154.009399 151.238365 1223848.0 158.046785 154
11-14

5 rows × 295 columns

Step 5.2: Cut data to start from 2013


Filter the data to focus on the period from 2013 to 2019.

In [ ]: # Cut the data to start from 2013


start_date = '2013-01-01'
end_date = '2019-12-31'
data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]

data.head()

Out[ ]: date open high close low volume bb_upper_5 bb_m

2013-
2751 141.556439 141.556439 138.177780 136.882405 5837034.0 142.200099 139
01-04

2013-
2752 137.469998 137.469998 136.408325 135.213108 4127139.0 142.415461 138
01-07

2013-
2753 135.667157 140.788562 140.781885 135.667157 6528698.0 143.124203 139
01-08

2013-
2754 141.222579 145.329051 144.527788 141.222579 5374932.0 145.366993 139
01-09

2013-
2755 143.946873 144.895034 143.225736 142.224158 3052786.0 146.676703 140
01-10

5 rows × 295 columns

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 35/132


8/22/24, 11:05 PM Project-ML-CODE

Step 6.1: Calculate the future return


Compute the future return as the percentage change from t0 to t1 (t+1/t0).

In [ ]: # Calculate the future return as the percentage change from t0 to t1 (t+1/t0)


data['FUTURE_RETURN'] = (data['close'].shift(-1) - data['close']) / data['close']

# Drop the last row as it will have NaN value for FUTURE_RETURN
data = data.dropna(subset=['FUTURE_RETURN'])

# Display the first few rows to check the FUTURE_RETURN column


data[['date', 'close', 'FUTURE_RETURN']].head()

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\933941594.py:2: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['FUTURE_RETURN'] = (data['close'].shift(-1) - data['close']) / data['close']
Out[ ]: date close FUTURE_RETURN

2751 2013-01-04 138.177780 -0.012806

2752 2013-01-07 136.408325 0.032062

2753 2013-01-08 140.781885 0.026608

2754 2013-01-09 144.527788 -0.009009

2755 2013-01-10 143.225736 -0.015105

Step 6.2: Determine the threshold for


classification
Set the threshold for classification based on the distribution of future returns.

In [ ]: # Plot the distribution of returns


plt.figure(figsize=(10, 6))
sns.histplot(data['FUTURE_RETURN'], bins=100, kde=True)
plt.title('Distribution of Future Returns')
plt.xlabel('FUTURE_RETURN')
plt.ylabel('Frequency')
plt.xlim(-0.05, 0.05)
plt.show()

# Statistical description of the returns


print(data['FUTURE_RETURN'].describe())

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 36/132


8/22/24, 11:05 PM Project-ML-CODE

threshold = data['FUTURE_RETURN'].mean() + data['FUTURE_RETURN'].std()*0.5


print(f"Threshold for positive future return classification: {threshold:.4f}")

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

count 1701.000000
mean 0.001422
std 0.020056
min -0.100000
25% -0.009021
50% 0.000215
75% 0.011151
max 0.100000
Name: FUTURE_RETURN, dtype: float64
Threshold for positive future return classification: 0.0114

In [ ]: # Explain the threshold calculation


display(Math(r'\text{Threshold} = \mu + 0.5 \times \sigma'))

Threshold = μ + 0.5 × σ

Step 6.3: Create the target variable


Create the binary target variable y based on the threshold for positive future return.

In [ ]: # Explain the threshold calculation


binary_target_tree_latex = r"""
\begin{array}{c}

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 37/132


8/22/24, 11:05 PM Project-ML-CODE

\text{Start with FUTURE\_RETURN} \\


\downarrow \\
\text{Is } \text{FUTURE\_RETURN} > \text{threshold?} \\
\begin{array}{ll}
\text{Yes} & \quad \text{No} \\
\downarrow & \quad \downarrow \\
\text{y = 1 (Positive Return)} & \quad \text{y = 0 (No Positive Return)} \\
\end{array}
\end{array}
"""

display(Math(binary_target_tree_latex))

Start with FUTURE\_RETURN



Is FUTURE\_RETURN> threshold?
Yes No
↓ ↓
y = 1 (Positive Return) y = 0 (No Positive Return)

In [ ]: # Create the binary target variable y: 1 if return > threshold, 0 otherwise


data['y'] = np.where(data['FUTURE_RETURN'] > threshold, 1, 0)

# Display the first few rows to verify the target variable


data[['date', 'close', 'FUTURE_RETURN', 'y']].head()

C:\Users\bob19\AppData\Local\Temp\ipykernel_21596\326017906.py:2: PerformanceWarnin
g: DataFrame is highly fragmented. This is usually the result of calling `frame.ins
ert` many times, which has poor performance. Consider joining all columns at once u
sing pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.
copy()`
data['y'] = np.where(data['FUTURE_RETURN'] > threshold, 1, 0)
Out[ ]: date close FUTURE_RETURN y

2751 2013-01-04 138.177780 -0.012806 0

2752 2013-01-07 136.408325 0.032062 1

2753 2013-01-08 140.781885 0.026608 1

2754 2013-01-09 144.527788 -0.009009 0

2755 2013-01-10 143.225736 -0.015105 0

In [ ]: # Check for class imbalance


data['y'].value_counts(normalize=True)

Out[ ]: y
0 0.753674
1 0.246326
Name: proportion, dtype: float64

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 38/132


8/22/24, 11:05 PM Project-ML-CODE

Step 7.1: Preprocessing - Scaling and


transforming features
Use pipelines to preprocess and scale the features before feeding them into the model.

In [ ]: # Explain StandardScaler
display(Math(r'\text{StandardScaler: } z = \frac{x - \mu}{\sigma}'))

# Explain MinMaxScaler
display(Math(r'\text{MinMaxScaler: } x_{scaled} = \frac{x - x_{min}}{x_{max} - x_{m

x−μ
StandardScaler: z =
σ
x − xmin
MinMaxScaler: xscaled =
xmax − xmin

In [ ]: # 1. Continuous/Numerical features (price-based indicators like moving averages)


numerical_features = [col for col in data.columns if 'sma' in col or 'ema' in col o

# 2. Volatility-based features (ATR, Bollinger Bands)


volatility_features = [col for col in data.columns if 'atr' in col or 'bb' in col o

# 3. Volume-based features (OBV, MFI, VROC)


volume_features = [col for col in data.columns if 'obv' in col or 'mfi' in col or '

# 4. Momentum-based features (RSI, MOM, ROC)


momentum_features = [col for col in data.columns if 'rsi' in col or 'mom' in col or

# 5. Ratios (H2L, O2C)


ratio_features = ['h2l', 'o2c']

# 6. Price and volume features


price_volume_features = ['open', 'close', 'high', 'low', 'volume']

# 7. SMA slope and slope change features


sma_columns = ['sma_5', 'sma_10', 'sma_20', 'sma_60', 'sma_120', 'sma_200']
sma_slope_features = [f'{sma}_slope' for sma in sma_columns]
sma_slope_change_features = [f'{sma}_slope_change' for sma in sma_columns]

# Combine all feature lists


all_features = numerical_features + volatility_features + volume_features + momentu

# Pipeline for numerical features


numerical_pipeline = Pipeline(steps=[
('scaler', StandardScaler()) # Standard scaling for numerical features
])

# Pipeline for volatility features


volatility_pipeline = Pipeline(steps=[
('scaler', MinMaxScaler()) # Min-Max scaling for volatility-based features
])

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 39/132


8/22/24, 11:05 PM Project-ML-CODE

# Pipeline for volume-based features


volume_pipeline = Pipeline(steps=[
('scaler', MinMaxScaler()) # Min-Max scaling for volume-based features
])

# Pipeline for momentum-based features


momentum_pipeline = Pipeline(steps=[
('scaler', MinMaxScaler()) # Min-Max scaling for momentum features
])

# Pipeline for ratio features


ratio_pipeline = Pipeline(steps=[
('scaler', MinMaxScaler()) # Min-Max scaling for ratio features
])

# Pipeline for price and volume features


price_volume_pipeline = Pipeline(steps=[
('scaler', StandardScaler()) # Standard scaling for price and volume features
])

# Pipeline for SMA slope and slope change features


sma_slope_pipeline = Pipeline(steps=[
('scaler', StandardScaler()) # Standard scaling for SMA slope and slope change
])

# Combine all pipelines into a single ColumnTransformer


preprocessor = ColumnTransformer(
transformers=[
('num', numerical_pipeline, numerical_features),
('vol', volatility_pipeline, volatility_features),
('volm', volume_pipeline, volume_features),
('mom', momentum_pipeline, momentum_features),
('rat', ratio_pipeline, ratio_features),
('price_vol', price_volume_pipeline, price_volume_features), # Apply scali
('sma_slope', sma_slope_pipeline, sma_slope_features), # SMA slope feature
('sma_slope_change', sma_slope_pipeline, sma_slope_change_features) # SMA
]
)

# Apply the preprocessor to the data


data_processed = preprocessor.fit_transform(data)

# Convert the processed data back into a DataFrame


processed_columns = numerical_features + volatility_features + volume_features + mo
data_processed_df = pd.DataFrame(data_processed, columns=processed_columns)

# Add the target column 'y' back to the processed DataFrame


data_processed_df['y'] = data['y'].values

# drop nan values


data_processed_df = data_processed_df.dropna()

# Display the first few rows of the processed data


data_processed_df.head()

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 40/132


8/22/24, 11:05 PM Project-ML-CODE

Out[ ]: dema_5 dema_10 dema_14 dema_20 dema_30 dema_50 dema_60 ema_5

0 -0.815957 -0.814504 -0.815330 -0.818313 -0.820458 -0.813450 -0.807504 -0.813880 -0

1 -0.820938 -0.817867 -0.817796 -0.819957 -0.821721 -0.814863 -0.809009 -0.817156 -0

2 -0.815031 -0.815267 -0.815949 -0.818560 -0.820921 -0.815003 -0.809445 -0.814218 -0

3 -0.804461 -0.808981 -0.811168 -0.814946 -0.818530 -0.814112 -0.809004 -0.807873 -0

4 -0.801350 -0.805839 -0.808430 -0.812629 -0.816900 -0.813604 -0.808865 -0.805168 -0

5 rows × 173 columns

In [ ]: data_processed_df.describe()

Out[ ]: dema_5 dema_10 dema_14 dema_20 dema_30 de

count 1.701000e+03 1.701000e+03 1.701000e+03 1.701000e+03 1.701000e+03 1.70100

mean -6.683530e-17 1.336706e-16 2.005059e-16 -2.673412e-16 1.336706e-16 2.6734

std 1.000294e+00 1.000294e+00 1.000294e+00 1.000294e+00 1.000294e+00 1.00029

min -1.015105e+00 -1.015205e+00 -1.015182e+00 -1.013721e+00 -1.010513e+00 -1.00597

25% -8.376433e-01 -8.353190e-01 -8.344057e-01 -8.333103e-01 -8.327865e-01 -8.3885

50% -3.944266e-01 -3.966726e-01 -3.928047e-01 -3.887822e-01 -3.936933e-01 -4.1118

75% 8.155541e-01 8.166955e-01 8.125680e-01 8.145304e-01 8.188911e-01 8.0405

max 2.723433e+00 2.723200e+00 2.717185e+00 2.708961e+00 2.706439e+00 2.71539

8 rows × 173 columns

In [ ]: # Check for missing columns (features) in the processed data


missing_features = set(all_features) - set(data_processed_df.columns)
if missing_features:
print(f"Missing features that were not scaled: {missing_features}")
else:
print("All features have been scaled and included in the processed data.")

All features have been scaled and included in the processed data.

Exploratory Data Analysis (EDA)


In [ ]: # Ensure 'date' column is in datetime format
data['date'] = pd.to_datetime(data['date'])

# Plotting price and volume trends over time


def plot_price_volume_trends(data):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 41/132


8/22/24, 11:05 PM Project-ML-CODE

plt.figure(figsize=(14, 8))

# Subplot 1: Closing Price


plt.subplot(2, 1, 1)
plt.plot(data['date'], data['close'], label='Closing Price', color='blue')
plt.title('Closing Price Over Time')
plt.xlabel('Date')
plt.ylabel('Price')
plt.grid(True)

# Subplot 2: Volume
plt.subplot(2, 1, 2)
plt.plot(data['date'], data['volume'], label='Volume', color='orange')
plt.title('Volume Over Time')
plt.xlabel('Date')
plt.ylabel('Volume')
plt.grid(True)

plt.tight_layout()
plt.show()

plot_price_volume_trends(data)

In [ ]: # Define a function to plot the distribution


def plot_pairwise_relationships_numerical(data, features):
sns.pairplot(data[features], diag_kind='kde', plot_kws={'alpha':0.5})
plt.suptitle('Pairplot of Numerical Features', y=1.02)
plt.show()

# Select a subset of numerical features for pairplot


pairplot_numerical_features = numerical_features[:5]

plot_pairwise_relationships_numerical(data, pairplot_numerical_features)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 42/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 43/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: def plot_pairwise_relationships_volatility(data, features):


sns.pairplot(data[features], diag_kind='kde', plot_kws={'alpha':0.5})
plt.suptitle('Pairplot of Volatility Features', y=1.02)
plt.show()

# Select a subset of volatility features for pairplot


pairplot_volatility_features = volatility_features[:5]

plot_pairwise_relationships_volatility(data, pairplot_volatility_features)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 44/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 45/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: def plot_pairwise_relationships_volume(data, features):


sns.pairplot(data[features], diag_kind='kde', plot_kws={'alpha':0.5})
plt.suptitle('Pairplot of Volume Features', y=1.02)
plt.show()

# Select a subset of volume features for pairplot


pairplot_volume_features = volume_features[:5]

plot_pairwise_relationships_volume(data, pairplot_volume_features)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 46/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 47/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: def plot_pairwise_relationships_momentum(data, features):


sns.pairplot(data[features], diag_kind='kde', plot_kws={'alpha':0.5})
plt.suptitle('Pairplot of Momentum Features', y=1.02)
plt.show()

# Select a subset of momentum features for pairplot


pairplot_momentum_features = momentum_features[:5]

plot_pairwise_relationships_momentum(data, pairplot_momentum_features)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 48/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 49/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: def plot_pairwise_relationships_ratio(data, features):


sns.pairplot(data[features], diag_kind='kde', plot_kws={'alpha':0.5})
plt.suptitle('Pairplot of Ratio Features', y=1.02)
plt.show()

# Select a subset of ratio features for pairplot


pairplot_ratio_features = ratio_features

plot_pairwise_relationships_ratio(data, pairplot_ratio_features)

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 50/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: # Step 1: Split the data into success and failure groups


data_success = data[data['y'] == 1] # success
data_failure = data[data['y'] == 0] # failure

# Step 2: Plot the distribution of the selected features


def plot_simple_distribution(data1, data2, features):
num_features = len(features)
num_rows = (num_features // 2) + 1 if num_features % 2 != 0 else num_features /

plt.figure(figsize=(14, num_rows * 6))

for i, feature in enumerate(features):


plt.subplot(num_rows, 2, i + 1)
sns.histplot(data1[feature], stat="density", color='red', kde=True, label='
sns.histplot(data2[feature], stat="density", color='blue', kde=True, label=

plt.title(f'Distribution of {feature}')
plt.legend()

plt.tight_layout()
plt.show()

plot_simple_distribution(data_success, data_failure, numerical_features)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 51/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 52/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 53/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 54/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 55/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 56/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 57/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 58/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 59/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 60/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 61/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 62/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: def plot_volatility_features_distribution(data1, data2, features):


num_features = len(features)
num_rows = (num_features // 2) + 1 if num_features % 2 != 0 else num_features /

plt.figure(figsize=(14, num_rows * 6))

for i, feature in enumerate(features):


plt.subplot(num_rows, 2, i + 1)
sns.histplot(data1[feature], stat="density", color='red', kde=True, label='
sns.histplot(data2[feature], stat="density", color='blue', kde=True, label=

plt.title(f'Distribution of {feature}')
plt.legend()

plt.tight_layout()
plt.show()

plot_volatility_features_distribution(data_success, data_failure, volatility_featur

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 63/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 64/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 65/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 66/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 67/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 68/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 69/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 70/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 71/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 72/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: def plot_volume_features_distribution(data1, data2, features):


num_features = len(features)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 73/132


8/22/24, 11:05 PM Project-ML-CODE

num_rows = (num_features // 2) + 1 if num_features % 2 != 0 else num_features /

plt.figure(figsize=(14, num_rows * 6))

for i, feature in enumerate(features):


plt.subplot(num_rows, 2, i + 1)
sns.histplot(data1[feature], stat="density", color='red', kde=True, label='
sns.histplot(data2[feature], stat="density", color='blue', kde=True, label=

plt.title(f'Distribution of {feature}')
plt.legend()

plt.tight_layout()
plt.show()

plot_volume_features_distribution(data_success, data_failure, volume_features)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 74/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 75/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 76/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 77/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 78/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 79/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: def plot_momentum_features_distribution(data1, data2, features):


num_features = len(features)
num_rows = (num_features // 2) + 1 if num_features % 2 != 0 else num_features /

plt.figure(figsize=(14, num_rows * 6))

for i, feature in enumerate(features):


plt.subplot(num_rows, 2, i + 1)
sns.histplot(data1[feature], stat="density", color='red', kde=True, label='
sns.histplot(data2[feature], stat="density", color='blue', kde=True, label=

plt.title(f'Distribution of {feature}')
plt.legend()

plt.tight_layout()
plt.show()

plot_momentum_features_distribution(data_success, data_failure, momentum_features)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 80/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 81/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 82/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 83/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 84/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 85/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 86/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 87/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 88/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 89/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 90/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 91/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 92/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 93/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 94/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 95/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 96/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 97/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: def plot_ratio_features_distribution(data1, data2, features):


num_features = len(features)
num_rows = (num_features // 2) + 1 if num_features % 2 != 0 else num_features /

plt.figure(figsize=(14, num_rows * 6))

for i, feature in enumerate(features):


plt.subplot(num_rows, 2, i + 1)
sns.histplot(data1[feature], stat="density", color='red', kde=True, label='
sns.histplot(data2[feature], stat="density", color='blue', kde=True, label=

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 98/132


8/22/24, 11:05 PM Project-ML-CODE

plt.title(f'Distribution of {feature}')
plt.legend()

plt.tight_layout()
plt.show()

plot_ratio_features_distribution(data_success, data_failure, ratio_features)

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

In [ ]: def plot_ratio_features_distribution(data1, data2, features):


num_features = len(features)
num_rows = (num_features // 2) + 1 if num_features % 2 != 0 else num_features /

plt.figure(figsize=(14, num_rows * 6))

for i, feature in enumerate(features):


plt.subplot(num_rows, 2, i + 1)
sns.histplot(data1[feature], stat="density", color='red', kde=True, label='
sns.histplot(data2[feature], stat="density", color='blue', kde=True, label=

plt.title(f'Distribution of {feature}')
plt.legend()

plt.tight_layout()
plt.show()

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 99/132


8/22/24, 11:05 PM Project-ML-CODE

plot_ratio_features_distribution(data_success, data_failure, sma_slope_features)

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 100/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: def plot_ratio_features_distribution(data1, data2, features):


num_features = len(features)
num_rows = (num_features // 2) + 1 if num_features % 2 != 0 else num_features /

plt.figure(figsize=(14, num_rows * 6))

for i, feature in enumerate(features):


plt.subplot(num_rows, 2, i + 1)
sns.histplot(data1[feature], stat="density", color='red', kde=True, label='

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 101/132


8/22/24, 11:05 PM Project-ML-CODE

sns.histplot(data2[feature], stat="density", color='blue', kde=True, label=

plt.title(f'Distribution of {feature}')
plt.legend()

plt.tight_layout()
plt.show()

plot_ratio_features_distribution(data_success, data_failure, sma_slope_change_featu

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 102/132


8/22/24, 11:05 PM Project-ML-CODE

c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
c:\Users\bob19\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning:
use_inf_as_na option is deprecated and will be removed in a future version. Convert
inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 103/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: # Correlation heatmap for numerical features


def plot_group_correlation_heatmap(data, features, title):
correlation_matrix = data[features].corr()
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=False, cmap='coolwarm', fmt='.2f', linewi
plt.title(f'{title} Correlation Heatmap')
plt.show()

# Call the function for each feature group

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 104/132


8/22/24, 11:05 PM Project-ML-CODE

plot_group_correlation_heatmap(data, numerical_features, 'Numerical Features')


plot_group_correlation_heatmap(data, volatility_features, 'Volatility Features')
plot_group_correlation_heatmap(data, volume_features, 'Volume Features')
plot_group_correlation_heatmap(data, momentum_features, 'Momentum Features')
plot_group_correlation_heatmap(data, ratio_features, 'Ratio Features')
plot_group_correlation_heatmap(data, sma_slope_features, 'SMA Slope Features')
plot_group_correlation_heatmap(data, sma_slope_change_features, 'SMA Slope Change F

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 105/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 106/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 107/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 108/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 109/132


8/22/24, 11:05 PM Project-ML-CODE

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 110/132


8/22/24, 11:05 PM Project-ML-CODE

In [ ]: # Calculate the correlation of all features with the target variable `y`
correlation_with_y = data.corr()['y'].sort_values(ascending=False)

# Select top 30 features most correlated with the target variable


top_features = correlation_with_y.index[1:31]

# Correlation heatmap for top features


def plot_top_correlation_heatmap(data, top_features):
correlation_matrix = data[top_features].corr()
plt.figure(figsize=(12, 10))
sns.heatmap(correlation_matrix, annot=False, cmap='coolwarm', fmt='.2f', linewi
plt.title('Top Correlated Features with y')
plt.show()

plot_top_correlation_heatmap(data, top_features)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 111/132


8/22/24, 11:05 PM Project-ML-CODE

Feature Selection
1. Random Forest Feature Selection
In [ ]: # Explain feature importance
display(Math(r'\text{Feature Importance: } I(f_j) = \frac{1}{T} \sum_{t=1}^{T} I_t(

display(Markdown("""
**Explanation:**

- **Feature Importance**: Denoted as \( I(f_j) \), it represents the importance of


"""))

1 T
Feature Importance: I(fj ) = ∑ I (f )
T t=1 t j

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 112/132


8/22/24, 11:05 PM Project-ML-CODE

Explanation:

Feature Importance: Denoted as ( I(f_j) ), it represents the importance of feature ( f_j ) in


the model.

In [ ]: X = data_processed_df.drop(columns=['y'])
y = data_processed_df['y']

model = RandomForestClassifier(n_estimators=100, random_state=88)


model.fit(X, y)

importance_scores = model.feature_importances_
feature_importance_df = pd.DataFrame({'Feature': X.columns, 'Importance': importanc
feature_importance_df = feature_importance_df.sort_values(by='Importance', ascendin

# Select top 35% most important features


top_80_percent_features = feature_importance_df['Feature'].head(int(len(importance_
data_top_80_percent_df = X[top_80_percent_features]

print(f"Number of features after Random Forest selection: {len(top_80_percent_featu

Number of features after Random Forest selection: 60

2. SOM Feature Selection


In [ ]: # Explain SOM weight update
display(Math(r'\Delta w_{ij} = \eta(t) \times h_{ij}(t) \times (x(t) - w_{ij}(t))')

display(Markdown("""
**Explanation:**

- **SOM Weight Update**: This formula describes how the weights \( w_{ij} \) of the

Δwij = η(t) × hij (t) × (x(t) − wij (t))

Explanation:

SOM Weight Update: This formula describes how the weights ( w_{ij} ) of the nodes in a
Self-Organizing Map (SOM) are updated during training.

In [ ]: # Step 6.2: Retain 50-60% of features after SOM reduction


som = MiniSom(x=15, y=15, input_len=data_top_80_percent_df.shape[1], sigma=1.0, lea
som.random_weights_init(data_top_80_percent_df.values)
som.train_random(data_top_80_percent_df.values, 100)

# Compute feature importance based on SOM weights variance and select top 80%
som_feature_importance = np.var(som.get_weights(), axis=0).sum(axis=1)
sorted_som_indices = np.argsort(som_feature_importance)[::-1]
top_50_60_percent_indices = sorted_som_indices[:int(len(sorted_som_indices) * 0.8)]
top_som_features = data_top_80_percent_df.columns[top_50_60_percent_indices]

data_top_som_features_df = data_top_80_percent_df[top_som_features]

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 113/132


8/22/24, 11:05 PM Project-ML-CODE

print(f"Number of features after SOM reduction: {len(top_som_features)}")

Number of features after SOM reduction: 12

3. Leave-One-Feature-Out Feature Evaluation


In [ ]: feature_importance_scores = []
for feature in data_top_som_features_df.columns:
X_temp = data_top_som_features_df.drop(columns=[feature])
scores = cross_val_score(model, X_temp, y, cv=5, scoring='accuracy')
feature_importance_scores.append((feature, np.mean(scores)))

# Sort features by their importance scores


sorted_scores = sorted(feature_importance_scores, key=lambda x: x[1], reverse=True)

# Ensure at least 20 features are selected and avoid duplicates


final_features = []
for feature, score in sorted_scores:
if feature not in final_features:
final_features.append(feature)
if len(final_features) >= 20: # Stop when 20 unique features are selected
break

print(f"Number of features after Leave-One-Feature-Out evaluation: {len(final_featu

# display the selected features


print("Selected features after leave-one-feature-out evaluation:")
for feature in final_features:
print(feature)

Number of features after Leave-One-Feature-Out evaluation: 8


Selected features after leave-one-feature-out evaluation:
vroc_50
sma_5_slope
volume
mfi_14
return
natr_5
vroc_30
h2l

4. Mannually add back important yet uncaptured features


In [ ]: additional_features = [
'sma_5_slope',
'sma_5_slope_change',
'sma_10_slope',
'sma_10_slope_change',
'rsi_14',
'sma_20_slope',
'sma_20_slope_change'
]

# make sure the additional features are in the data

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 114/132


8/22/24, 11:05 PM Project-ML-CODE

for feature in additional_features:


if feature not in final_features and feature in data_processed_df.columns:
final_features.append(feature)
if len(final_features) >= 15: # stop when 15 features are selected
break

print(f"Number of features after manual addition: {len(final_features)}")

# display the final selected features


print("Final selected features:")
for feature in final_features:
print(feature)

Number of features after manual addition: 14


Final selected features:
vroc_50
sma_5_slope
volume
mfi_14
return
natr_5
vroc_30
h2l
sma_5_slope_change
sma_10_slope
sma_10_slope_change
rsi_14
sma_20_slope
sma_20_slope_change

Part Five: Model Building and Optimization

Step 7.2: Split the data into training,


validation, and test sets
Sequentially split the data to maintain time order, avoiding data leakage.

In [ ]: # Define the features and target


X = data_processed_df[final_features] # Selected features
y = data_processed_df['y'] # Target variable

# Split into train (60%) and temp (40%)


X_train, X_temp, y_train, y_temp = train_test_split(X, y, train_size=0.6, shuffle=F

# Split temp into validation (20%) and test (20%)


X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, train_size=0.5, shu

train_shape = X_train.shape
val_shape = X_val.shape
test_shape = X_test.shape

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 115/132


8/22/24, 11:05 PM Project-ML-CODE

# Create a dictionary with the shapes of the datasets


shape_dict = {
"Dataset": ["Training Set", "Validation Set", "Test Set"],
"Shape": [train_shape, val_shape, test_shape]
}

# Create a DataFrame from the dictionary


shape_df = pd.DataFrame(shape_dict)

shape_df

Out[ ]: Dataset Shape

0 Training Set (1020, 22)

1 Validation Set (340, 22)

2 Test Set (341, 22)

In [ ]: # Display the first few rows of each set to ensure data integrity
print("First few rows of the training set:")
print(X_train.head())
print(y_train.head())

print('----------------------------------')

print("First few rows of the validation set:")


print(X_val.head())
print(y_val.head())

print('----------------------------------')

print("First few rows of the test set:")


print(X_test.head())
print(y_test.head())

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 116/132


8/22/24, 11:05 PM Project-ML-CODE

First few rows of the training set:


vroc_50 vroc_50 sma_5_slope sma_5_slope volume mfi_14 return \
0 0.238114 0.238114 -0.364965 -0.364965 0.696042 0.446488 -0.566936
1 0.287847 0.287847 -0.252934 -0.252934 0.018891 0.462097 -0.709286
2 0.549192 0.549192 -0.133794 -0.133794 0.969954 0.575913 1.528269
3 0.165723 0.165723 -0.062294 -0.062294 0.513041 0.564499 1.256258
4 0.159367 0.159367 -0.053494 -0.053494 -0.406574 0.499435 -0.519949

natr_5 vroc_30 vroc_30 ... sma_5_slope_change sma_10_slope \


0 0.299184 0.151988 0.151988 ... 0.286730 -0.278774
1 0.269562 0.121151 0.121151 ... 0.315569 -0.325432
2 0.266935 0.124195 0.124195 ... 0.335590 -0.376946
3 0.254584 0.180026 0.180026 ... 0.201405 -0.401666
4 0.224688 0.137091 0.137091 ... 0.024797 -0.336236

sma_10_slope sma_10_slope_change sma_10_slope_change rsi_14 \


0 -0.278774 -0.255187 -0.255187 0.397164
1 -0.325432 -0.218168 -0.218168 0.363216
2 -0.376946 -0.240890 -0.240890 0.465412
3 -0.401666 -0.115535 -0.115535 0.538914
4 -0.336236 0.306232 0.306232 0.510117

sma_20_slope sma_20_slope sma_20_slope_change sma_20_slope_change


0 -0.329610 -0.329610 0.598738 0.598738
1 -0.254989 -0.254989 0.497388 0.497388
2 -0.153306 -0.153306 0.677619 0.677619
3 -0.076604 -0.076604 0.511252 0.511252
4 -0.051910 -0.051910 0.164891 0.164891

[5 rows x 22 columns]
0 0
1 1
2 1
3 0
4 0
Name: y, dtype: int32
----------------------------------
First few rows of the validation set:
vroc_50 vroc_50 sma_5_slope sma_5_slope volume mfi_14 \
1020 0.066409 0.066409 0.256744 0.256744 -0.366256 0.880916
1021 0.177007 0.177007 0.507654 0.507654 0.303612 0.898204
1022 0.049973 0.049973 0.647739 0.647739 0.187647 0.834521
1023 0.060037 0.060037 0.762405 0.762405 -0.249079 0.749484
1024 0.033754 0.033754 0.759129 0.759129 -0.297278 0.667080

return natr_5 vroc_30 vroc_30 ... sma_5_slope_change \


1020 0.974213 0.089731 0.202074 0.202074 ... 0.072243
1021 0.910180 0.103433 0.238673 0.238673 ... 0.706748
1022 -0.011183 0.114553 0.241149 0.241149 ... 0.394587
1023 -0.663577 0.116055 0.146385 0.146385 ... 0.322990
1024 -0.450654 0.118912 0.136383 0.136383 ... -0.009218

sma_10_slope sma_10_slope sma_10_slope_change sma_10_slope_change \


1020 0.506244 0.506244 0.446649 0.446649
1021 0.593454 0.593454 0.408131 0.408131
1022 0.659972 0.659972 0.311326 0.311326

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 117/132


8/22/24, 11:05 PM Project-ML-CODE

1023 0.674107 0.674107 0.066246 0.066246


1024 0.637897 0.637897 -0.169289 -0.169289

rsi_14 sma_20_slope sma_20_slope sma_20_slope_change \


1020 0.882277 0.305083 0.305083 0.106389
1021 0.928890 0.381660 0.381660 0.510416
1022 0.931446 0.412521 0.412521 0.205960
1023 0.809778 0.419277 0.419277 0.045434
1024 0.740583 0.401451 0.401451 -0.118283

sma_20_slope_change
1020 0.106389
1021 0.510416
1022 0.205960
1023 0.045434
1024 -0.118283

[5 rows x 22 columns]
1020 1
1021 0
1022 0
1023 0
1024 0
Name: y, dtype: int32
----------------------------------
First few rows of the test set:
vroc_50 vroc_50 sma_5_slope sma_5_slope volume mfi_14 \
1360 0.066694 0.066694 -2.707663 -2.707663 -0.067671 0.154347
1361 0.052262 0.052262 -2.822225 -2.822225 -0.439935 0.221686
1362 0.067878 0.067878 -2.405615 -2.405615 -0.249834 0.305615
1363 0.021536 0.021536 -1.617521 -1.617521 -0.672310 0.378990
1364 0.069992 0.069992 -0.930498 -0.930498 -0.246481 0.273434

return natr_5 vroc_30 vroc_30 ... sma_5_slope_change \


1360 1.165500 0.233288 0.079422 0.079422 ... -0.694504
1361 -0.801700 0.225490 0.045841 0.045841 ... -0.322677
1362 1.084222 0.232434 0.062609 0.062609 ... 1.173477
1363 -0.409441 0.198187 0.067305 0.067305 ... 2.219834
1364 -0.648342 0.207961 0.087926 0.087926 ... 1.935149

sma_10_slope sma_10_slope sma_10_slope_change sma_10_slope_change \


1360 -2.108801 -2.108801 -1.513420 -1.513420
1361 -2.447012 -2.447012 -1.582201 -1.582201
1362 -2.436736 -2.436736 0.048196 0.048196
1363 -2.356027 -2.356027 0.377714 0.377714
1364 -2.188009 -2.188009 0.786191 0.786191

rsi_14 sma_20_slope sma_20_slope sma_20_slope_change \


1360 0.309545 -0.860509 -0.860509 -2.332326
1361 0.275045 -1.192977 -1.192977 -2.213717
1362 0.362596 -1.385926 -1.385926 -1.284555
1363 0.344629 -1.507684 -1.507684 -0.810445
1364 0.314571 -1.595994 -1.595994 -0.587683

sma_20_slope_change
1360 -2.332326

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 118/132


8/22/24, 11:05 PM Project-ML-CODE

1361 -2.213717
1362 -1.284555
1363 -0.810445
1364 -0.587683

[5 rows x 22 columns]
1360 0
1361 1
1362 0
1363 0
1364 0
Name: y, dtype: int32

Step 8.1: Hyperparameter tuning with


GridSearchCV
Perform hyperparameter tuning on Logistic Regression, Random Forest, and SVM models
using GridSearchCV.

In [ ]: # Explain the logistic regression function


display(Math(r'\text{Logistic Regression: } P(y=1|x) = \frac{1}{1 + e^{-(\beta_0 +

display(Markdown("""
**Explanation:**

- **Logistic Regression Function**: This formula represents the probability that th


"""))

1
Logistic Regression: P (y = 1|x) = −(β0 +β1 x1 +⋯+βn xn )
1+e
Explanation:

Logistic Regression Function: This formula represents the probability that the dependent
variable ( y ) equals 1, given the input features ( x ).

In [ ]: # Explain the SVM decision function


display(Math(r'\text{SVM Decision Function: } f(x) = w \cdot x + b'))

# Simple explanation in markdown


display(Markdown("""
**Explanation:**

- **SVM Decision Function**: This formula represents the decision boundary used by
"""))

SVM Decision Function: f(x) = w ⋅ x + b

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 119/132


8/22/24, 11:05 PM Project-ML-CODE

Explanation:

SVM Decision Function: This formula represents the decision boundary used by a Support
Vector Machine (SVM) to classify data points.

In [ ]: # Define parameter grids for each model


log_reg_params = {
'C': [1, 10, 20, 30],
'penalty': ['l2'],
'solver': ['liblinear']
}

rf_params = {
'n_estimators': [100, 200, 500],
'max_depth': [None, 10, 20],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}

svm_params = {
'C': [1, 10, 100],
'kernel': ['linear', 'rbf'],
'gamma': ['scale']
}

# Step 8.1: Perform hyperparameter tuning with GridSearchCV


log_reg_grid = GridSearchCV(LogisticRegression(random_state=88, class_weight='balan
rf_grid = GridSearchCV(RandomForestClassifier(random_state=88, class_weight='balanc
svm_grid = GridSearchCV(SVC(probability=True, random_state=88, class_weight='balanc

# Train with the best parameters found


log_reg_grid.fit(X_train, y_train)
rf_grid.fit(X_train, y_train)
svm_grid.fit(X_train, y_train)

# Get the best estimators


log_reg_best = log_reg_grid.best_estimator_
rf_best = rf_grid.best_estimator_
svm_best = svm_grid.best_estimator_

# Print best parameters


print("Best Logistic Regression Params:", log_reg_grid.best_params_)
print("Best Random Forest Params:", rf_grid.best_params_)
print("Best SVM Params:", svm_grid.best_params_)

Best Logistic Regression Params: {'C': 30, 'penalty': 'l2', 'solver': 'liblinear'}
Best Random Forest Params: {'max_depth': None, 'min_samples_leaf': 1, 'min_samples_s
plit': 2, 'n_estimators': 500}
Best SVM Params: {'C': 100, 'gamma': 'scale', 'kernel': 'linear'}

Step 8.2: Evaluate the tuned base learners


on the validation set
file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 120/132
8/22/24, 11:05 PM Project-ML-CODE

Evaluate the performance of tuned models on the validation set using accuracy, F1 score,
and AUC-ROC.

In [ ]: # Logistic Regression
y_pred_log_reg = log_reg_best.predict(X_val)
y_pred_log_reg_proba = log_reg_best.predict_proba(X_val)[:, 1]
log_reg_accuracy = accuracy_score(y_val, y_pred_log_reg)
log_reg_f1 = f1_score(y_val, y_pred_log_reg)
log_reg_auc = roc_auc_score(y_val, y_pred_log_reg_proba)

# Random Forest
y_pred_rf = rf_best.predict(X_val)
y_pred_rf_proba = rf_best.predict_proba(X_val)[:, 1]
rf_accuracy = accuracy_score(y_val, y_pred_rf)
rf_f1 = f1_score(y_val, y_pred_rf)
rf_auc = roc_auc_score(y_val, y_pred_rf_proba)

# SVM
y_pred_svm = svm_best.predict(X_val)
y_pred_svm_proba = svm_best.predict_proba(X_val)[:, 1]
svm_accuracy = accuracy_score(y_val, y_pred_svm)
svm_f1 = f1_score(y_val, y_pred_svm)
svm_auc = roc_auc_score(y_val, y_pred_svm_proba)

metrics_dict = {
'Model': ['Logistic Regression', 'Random Forest', 'SVM'],
'Validation Set Accuracy': [log_reg_accuracy, rf_accuracy, svm_accuracy],
'Validation Set F1 Score': [log_reg_f1, rf_f1, svm_f1],
'Validation Set AUC-ROC': [log_reg_auc, rf_auc, svm_auc]
}

metrics_df = pd.DataFrame(metrics_dict)

# Display the results


metrics_df.set_index('Model').T

Out[ ]: Model Logistic Regression Random Forest SVM

Validation Set Accuracy 0.641176 0.726471 0.679412

Validation Set F1 Score 0.377551 0.000000 0.347305

Validation Set AUC-ROC 0.567059 0.526303 0.577916

In [ ]: # Explain the formula for Accuracy


display(Math(r'\text{Accuracy: } \frac{TP + TN}{TP + TN + FP + FN}'))

display(Markdown("""
**Explanation:**

- **Accuracy**: This metric represents the proportion of correctly classified insta


"""))

# Explain the formula for F1 Score


display(Math(r'\text{F1 Score: } 2 \times \frac{\text{Precision} \times \text{Recal

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 121/132


8/22/24, 11:05 PM Project-ML-CODE

display(Markdown("""
**Explanation:**

- **F1 Score**: The F1 Score is a metric that balances precision and recall, provid
"""))

# AUC
display(Math(r'\text{AUC: Area Under the ROC Curve}'))

display(Markdown("""
**Explanation:**

- **AUC (Area Under the ROC Curve)**: AUC measures the area under the Receiver Oper
"""))

TP + TN
Accuracy:
TP + TN + FP + FN
Explanation:

Accuracy: This metric represents the proportion of correctly classified instances (both true
positives and true negatives) out of the total number of instances.

Precision × Recall
F1 Score: 2 ×
Precision + Recall
Explanation:

F1 Score: The F1 Score is a metric that balances precision and recall, providing a single
measure that accounts for both false positives and false negatives.

AUC: Area Under the ROC Curve


Explanation:

AUC (Area Under the ROC Curve): AUC measures the area under the Receiver Operating
Characteristic (ROC) curve, which plots the true positive rate (sensitivity) against the false
positive rate (1-specificity).

Part Seven: Building the Blending Model

Step 9.1: Generate predictions from tuned


base learners on the validation set
Use tuned models to generate predictions and probabilities on the validation set.

In [ ]: # Predict probabilities from tuned base learners


log_reg_val_pred = log_reg_best.predict_proba(X_val)[:, 1]
rf_val_pred = rf_best.predict_proba(X_val)[:, 1]

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 122/132


8/22/24, 11:05 PM Project-ML-CODE

svm_val_pred = svm_best.predict_proba(X_val)[:, 1]

# Create a new dataset using these predictions


blending_X_val = np.column_stack((log_reg_val_pred, rf_val_pred, svm_val_pred))
blending_y_val = y_val # The true labels remain the same

print("Shape of blending validation dataset:", blending_X_val.shape)

Shape of blending validation dataset: (340, 3)

Step 9.2: Train the blending model


Use Logistic Regression as a meta-learner to combine predictions from base models.

In [ ]: # Logistic Regression as the meta-learner


blending_model = LogisticRegression(random_state=88, class_weight='balanced')

# Train the meta-learner on the blending validation dataset


blending_model.fit(blending_X_val, blending_y_val)

Out[ ]: ▾ LogisticRegression i ?

LogisticRegression(class_weight='balanced', random_state=88)

Step 9.3: Use the Trained Blending Model to Predict


on the Test Set and Evaluate Performance
In [ ]: # Predict probabilities from tuned base learners on the test set
log_reg_test_pred = log_reg_best.predict_proba(X_test)[:, 1]
rf_test_pred = rf_best.predict_proba(X_test)[:, 1]
svm_test_pred = svm_best.predict_proba(X_test)[:, 1]

# Create a new dataset for the test set using these predictions
blending_X_test = np.column_stack((log_reg_test_pred, rf_test_pred, svm_test_pred))
blending_y_test = y_test # The true labels remain the same

# Step 9.3: Predict and evaluate the blending model on the test set

# Predict using the blending model


blending_test_pred = blending_model.predict(blending_X_test)
blending_test_pred_proba = blending_model.predict_proba(blending_X_test)[:, 1]

# Calculate performance metrics


blending_accuracy = accuracy_score(blending_y_test, blending_test_pred)
blending_f1 = f1_score(blending_y_test, blending_test_pred)
blending_auc = roc_auc_score(blending_y_test, blending_test_pred_proba)

metrics_dict = {
'Blending Model - Test Set Accuracy': [blending_accuracy],
'Blending Model - Test Set F1 Score': [blending_f1],
'Blending Model - Test Set AUC-ROC': [blending_auc]

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 123/132


8/22/24, 11:05 PM Project-ML-CODE

metrics_df = pd.DataFrame(metrics_dict)

# Display the results


metrics_df

Out[ ]: Blending Model - Test Set Blending Model - Test Set Blending Model - Test Set
Accuracy F1 Score AUC-ROC

0 0.521994 0.365759 0.534573

Step 10.1: Evaluate the blending model on


the test set
In [ ]: # Generate predictions and probabilities from the blending model on the test set
blending_test_pred = blending_model.predict(blending_X_test)
blending_test_pred_proba = blending_model.predict_proba(blending_X_test)[:, 1]

# Calculate evaluation metrics


blending_accuracy = accuracy_score(blending_y_test, blending_test_pred)
blending_precision = precision_score(blending_y_test, blending_test_pred)
blending_recall = recall_score(blending_y_test, blending_test_pred)
blending_f1 = f1_score(blending_y_test, blending_test_pred)
blending_auc = roc_auc_score(blending_y_test, blending_test_pred_proba)

# Generate the classification report as a dictionary


report_dict = classification_report(blending_y_test, blending_test_pred, output_dic

# Convert the dictionary to a DataFrame


report_df = pd.DataFrame(report_dict).transpose()

# Display the DataFrame


report_df

Out[ ]: precision recall f1-score support

0 0.752874 0.521912 0.616471 251.000000

1 0.281437 0.522222 0.365759 90.000000

accuracy 0.521994 0.521994 0.521994 0.521994

macro avg 0.517155 0.522067 0.491115 341.000000

weighted avg 0.628448 0.521994 0.550300 341.000000

In [ ]: # Calculate evaluation metrics


blending_accuracy = accuracy_score(blending_y_test, blending_test_pred)
blending_f1 = f1_score(blending_y_test, blending_test_pred)
blending_auc = roc_auc_score(blending_y_test, blending_test_pred_proba)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 124/132


8/22/24, 11:05 PM Project-ML-CODE

# Confusion Matrix
conf_matrix = confusion_matrix(blending_y_test, blending_test_pred)
ConfusionMatrixDisplay(conf_matrix).plot()
plt.title("Confusion Matrix of Blending Model")
plt.show()

In [ ]: # Plot the ROC curve


fpr, tpr, _ = roc_curve(blending_y_test, blending_test_pred_proba)
plt.figure(figsize=(10, 7))
plt.plot(fpr, tpr, color='b', label=f'AUC = {blending_auc:.2f}')
plt.plot([0, 1], [0, 1], color='r', linestyle='--')
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.legend(loc='lower right')
plt.show()

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 125/132


8/22/24, 11:05 PM Project-ML-CODE

Step 11.1: Create backtest DataFrame


Generate a backtest DataFrame containing necessary columns for strategy evaluation.

In [ ]: # Predict probabilities from tuned base learners on the test set


log_reg_test_pred = log_reg_best.predict_proba(X_test)[:, 1]
rf_test_pred = rf_best.predict_proba(X_test)[:, 1]
svm_test_pred = svm_best.predict_proba(X_test)[:, 1]

# Create a new dataset for the test set using these predictions
blending_X_test = np.column_stack((log_reg_test_pred, rf_test_pred, svm_test_pred))
blending_y_test = y_test # The true labels remain the same

# Predict using the blending model


blending_test_pred = blending_model.predict(blending_X_test)
blending_test_pred_proba = blending_model.predict_proba(blending_X_test)[:, 1]

# Calculate performance metrics


blending_accuracy = accuracy_score(blending_y_test, blending_test_pred)
blending_f1 = f1_score(blending_y_test, blending_test_pred)
blending_auc = roc_auc_score(blending_y_test, blending_test_pred_proba)

# Print the performance of the blending model


print("Blending Model - Accuracy: {:.4f}, F1 Score: {:.4f}, AUC: {:.4f}".format(ble

Blending Model - Accuracy: 0.5220, F1 Score: 0.3658, AUC: 0.5346

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 126/132


8/22/24, 11:05 PM Project-ML-CODE

Step 11.2: Calculate daily strategy returns


Compute daily returns based on the shifted signal and the future returns.

In [ ]: # Step 1: Extract 'date' and 'FUTURE_RETURN' columns from the original data and add
data_processed_df['date'] = data['date'].values
data_processed_df['FUTURE_RETURN'] = data['FUTURE_RETURN'].values

# Step 2: Create prediction signal (use the best model to generate predictions)
backtest_df = data_processed_df.iloc[-len(X_test):].copy()
backtest_df['signal'] = blending_test_pred

# Step 3: Shift the signal to avoid data leakage


backtest_df['shifted_signal'] = backtest_df['signal'].shift(1)

# Step 4: Make sure 'backtest_df' is sorted by date in ascending order


backtest_df = backtest_df.sort_values('date')

# Step 5: Create the final 'backtest_df' with all columns needed for backtesting
backtest_df = backtest_df[['date', 'close', 'signal', 'shifted_signal', 'y', 'FUTUR

# Step 6: Check if 'backtest_df' is created successfully


backtest_df.head(20)

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 127/132


8/22/24, 11:05 PM Project-ML-CODE

Out[ ]: date close signal shifted_signal y FUTURE_RETURN

1360 2018-08-07 0.902385 1 NaN 0 -0.014659

1361 2018-08-08 0.870074 1 1.0 1 0.023158

1362 2018-08-09 0.920371 1 1.0 0 -0.006793

1363 2018-08-10 0.905275 1 1.0 0 -0.011584

1364 2018-08-13 0.879710 1 1.0 0 0.001340

1365 2018-08-14 0.882632 1 1.0 0 -0.025480

1366 2018-08-15 0.826973 1 1.0 0 -0.021666

1367 2018-08-16 0.780852 1 1.0 0 -0.020943

1368 2018-08-17 0.737236 1 1.0 1 0.015641

1369 2018-08-20 0.769129 1 1.0 1 0.041409

1370 2018-08-21 0.854883 1 1.0 0 -0.005644

1371 2018-08-22 0.842710 1 1.0 0 0.003759

1372 2018-08-23 0.850772 1 1.0 0 -0.014786

1373 2018-08-24 0.818943 1 1.0 1 0.040891

1374 2018-08-27 0.905661 1 1.0 0 -0.009166

1375 2018-08-28 0.885427 0 1.0 0 0.007122

1376 2018-08-29 0.901004 0 0.0 0 -0.007946

1377 2018-08-30 0.883500 0 0.0 0 -0.031173

1378 2018-08-31 0.815378 0 0.0 0 0.010649

1379 2018-09-03 0.837925 0 0.0 1 0.013254

In [ ]: # Define trading strategy logic


# Buy signal: Buy the stock when shifted_signal is 1.
# Sell signal: Sell the stock when shifted_signal is 0.

# Trading strategy decision tree


decision_tree_latex = r"""
\begin{array}{c}
\text{Start} \\
\downarrow \\
\text{Is } \text{shifted\_signal} = 1 \text{?} \\
\begin{array}{ll}
\text{Yes} & \quad \text{No} \\
\downarrow & \quad \downarrow \\
\text{Buy Stock} & \quad \text{Sell Stock} \\
\end{array}
\end{array}

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 128/132


8/22/24, 11:05 PM Project-ML-CODE

"""

display(Math(decision_tree_latex))

Start

Is shifted\_signal = 1?
Yes No
↓ ↓
Buy Stock Sell Stock

In [ ]: # Calculate daily strategy return


# When shifted_signal is 1, calculate the return for the holding period; otherwise,
backtest_df['strategy_return'] = backtest_df['shifted_signal'] * backtest_df['FUTUR

# Check the strategy return in backtest_df


backtest_df[['date', 'close', 'shifted_signal', 'FUTURE_RETURN', 'strategy_return']

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 129/132


8/22/24, 11:05 PM Project-ML-CODE

Out[ ]: date close shifted_signal FUTURE_RETURN strategy_return

1360 2018-08-07 0.902385 NaN -0.014659 NaN

1361 2018-08-08 0.870074 1.0 0.023158 0.023158

1362 2018-08-09 0.920371 1.0 -0.006793 -0.006793

1363 2018-08-10 0.905275 1.0 -0.011584 -0.011584

1364 2018-08-13 0.879710 1.0 0.001340 0.001340

1365 2018-08-14 0.882632 1.0 -0.025480 -0.025480

1366 2018-08-15 0.826973 1.0 -0.021666 -0.021666

1367 2018-08-16 0.780852 1.0 -0.020943 -0.020943

1368 2018-08-17 0.737236 1.0 0.015641 0.015641

1369 2018-08-20 0.769129 1.0 0.041409 0.041409

1370 2018-08-21 0.854883 1.0 -0.005644 -0.005644

1371 2018-08-22 0.842710 1.0 0.003759 0.003759

1372 2018-08-23 0.850772 1.0 -0.014786 -0.014786

1373 2018-08-24 0.818943 1.0 0.040891 0.040891

1374 2018-08-27 0.905661 1.0 -0.009166 -0.009166

1375 2018-08-28 0.885427 1.0 0.007122 0.007122

1376 2018-08-29 0.901004 0.0 -0.007946 -0.000000

1377 2018-08-30 0.883500 0.0 -0.031173 -0.000000

1378 2018-08-31 0.815378 0.0 0.010649 0.000000

1379 2018-09-03 0.837925 0.0 0.013254 0.000000

Step 11.3: Calculate cumulative returns


Calculate the cumulative return for the strategy and the buy-and-hold strategy for
comparison.

In [ ]: # Calculate the cumulative return of the strategy


backtest_df['cumulative_strategy_return'] = (1 + backtest_df['strategy_return']).cu

# Calculate the cumulative return of the buy-and-hold strategy


backtest_df['cumulative_market_return'] = (1 + backtest_df['FUTURE_RETURN']).cumpro

# Visualize the cumulative returns


backtest_df[['date', 'cumulative_strategy_return', 'cumulative_market_return']].set
plt.show()

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 130/132


8/22/24, 11:05 PM Project-ML-CODE

Step 11.4: Calculate performance metrics


Compute key performance metrics such as cumulative return, max drawdown, annualized
return, and Sharpe ratio.

In [ ]: # Maximum Drawdown formula


display(Math(r'\text{Max Drawdown: } \text{MDD} = \max_{t} (\text{rolling\_max}(t)

# Annualized Return formula


display(Math(r'\text{Annualized Return: } R_{\text{annual}} = \left(1 + R_{\text{cu

# Annualized Volatility formula


display(Math(r'\text{Annualized Volatility: } \sigma_{\text{annual}} = \sigma_{\tex

# Sharpe Ratio formula


display(Math(r'\text{Sharpe Ratio: } S = \frac{R_{\text{annual}} - R_f}{\sigma_{\te

Max Drawdown: MDD = max(rolling\_max(t) − cumulative\_strategy\_return(t))


t

252
Annualized Return: Rannual = (1 + Rcumulative ) n −1

Annualized Volatility: σannual = σdaily × √252


Rannual − Rf
Sharpe Ratio: S = assuming Rf = 0
σannual

In [ ]: # Calculate cumulative return


cumulative_return = backtest_df['cumulative_strategy_return'].iloc[-1]

# Calculate maximum drawdown


rolling_max = backtest_df['cumulative_strategy_return'].cummax()
drawdown = rolling_max - backtest_df['cumulative_strategy_return']
max_drawdown = drawdown.max()

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 131/132


8/22/24, 11:05 PM Project-ML-CODE

# Calculate annualized return


annualized_return = ((1 + cumulative_return) ** (252 / len(backtest_df))) - 1

# Calculate annualized volatility


annualized_volatility = backtest_df['strategy_return'].std() * np.sqrt(252)

# Calculate Sharpe ratio (assuming risk-free rate is 0)


sharpe_ratio = annualized_return / annualized_volatility

performance_metrics = {
"Metric": ["Cumulative Return", "Max Drawdown", "Annualized Return", "Annualize
"Value": [cumulative_return, max_drawdown, annualized_return, annualized_volati
}

performance_df = pd.DataFrame(performance_metrics)

# Display the results


performance_df

Out[ ]: Metric Value

0 Cumulative Return 0.098988

1 Max Drawdown 0.255308

2 Annualized Return 0.072245

3 Annualized Volatility 0.250624

4 Sharpe Ratio 0.288259

file:///C:/Users/bob19/Desktop/Final Exam/Project-ML-CODE.html 132/132

You might also like