Section 7 Unit 6
Versions used: Python 3.12.2 / Pandas 2.2.2 / Numpy 1.26.4 / Sklearn 1.4.2
Please look at the attached code and help me to clear out warning errors.
STRATEGY PERFORMANCE: The strategy returns "highly underperformed' the Gold Returns
Once I have the clean code, will tweak the code to see if returns can be improved.
Hi Anil, let me give you an example of resolving SettingWithCopyWarning in the notebook shared.
Let's consider how X_test is created.
Define test data
X_test = X[split:]
Here you took the slice of original dataframe and saves as X_test which is futher being modified to add columns. This is the cause of the warnings.
Instead, create a copy of original dataframe X and modify it to avoid such SettingWithCopyWarning.
So, create X_test as below
Define test data
X_test = X[split:].copy()
By using .copy(), you ensure that X_test is an independent copy of the sliced DataFrame X[split:], rather than a view or slice of the original DataFrame. This should help prevent the warning from being triggered when you add new columns to X_test.
Now, use this format to add a new column –> .loc[row_indexer,col_indexer] = value
For example,
Create a new column in X_test
X_test.loc[:, 'yU_predict'] = yU_predict
X_test.loc[:, 'yD_predict'] = yD_predict
Make sure you do this while creating any dataframe in your code file to avoid getting 'SettingWithCopyWarning' while trying to create new columns.
I hope this helps!