Trading with Machine Learning: Regression

Section 7 Unit 5

Versions used: Python 3.12.2 / Pandas 2.2.2 / Numpy 1.26.4 / Sklearn 1.4.2



[26]    # Create new columns in X_test

    X_test['yU_predict'] = yU_predict

    X_test['yD_predict'] = yD_predict



C:\Users\anilh\AppData\Local\Temp\ipykernel_3128\3608534760.py:2: SettingWithCopyWarning: 

A value is trying to be set on a copy of a slice from a DataFrame.

Try using .loc[row_indexer,col_indexer] = value instead … COULD NOT UNDERSTAND HOW TO USE IT !!!



See the caveats in the documentation: Indexing and selecting data — pandas 2.2.3 documentation

  X_test['yU_predict'] = yU_predict

C:\Users\anilh\AppData\Local\Temp\ipykernel_3128\3608534760.py:3: SettingWithCopyWarning: 

A value is trying to be set on a copy of a slice from a DataFrame.

Try using .loc[row_indexer,col_indexer] = value instead



See the caveats in the documentation: Indexing and selecting data — pandas 2.2.3 documentation

  X_test['yD_predict'] = yD_predict

Hi Anil, 



It seems like you're encountering a SettingWithCopyWarning while trying to create new columns yU_predict and yD_predict in your X_test DataFrame. This warning typically occurs when you try to modify a slice of a DataFrame without explicitly indicating whether you are modifying a copy or the original DataFrame.



It should be noted that the SettingWithCopyWarning is a warning and not an error.  



To avoid this warning, you can try using the .loc accessor to explicitly assign values to the new columns. 



Here's how you can do it:

# Create new columns in X_test without triggering the warning
X_test.loc[:, 'yU_predict'] = yU_predict
X_test.loc[:, 'yD_predict'] = yD_predict

Using .loc with the colon ':' selects all rows, and by providing the column names 'yU_predict' and 'yD_predict', you explicitly assign values to these columns in the original DataFrame X_test.

This should resolve the warning you're encountering.

Let us know if you need further assistance!