How to add to the original DataFrame the predictions from a Decision Trees Model

Hi



What function should I use to add / append to the original DataFrame  in which the model was trained the predictions made by a decision tree?



I will like to see in one column the original trades and in another column the predictions made by the model.

There are a few things you will have to keep in mind while doing the append. The test dataset is only a subset of the actual dataset, hence if you directly add a column to the dataframe, you will get a length mismatch error. Here, we could employ the concept of 'merge'.

Not using 'how' will give you only test data if that is what you'd want.

 

#y_hats has your predicitions

y_hats_df = pd.DataFrame(data = y_hats, columns = ['y_hats'], index = X_test.index.copy())
df_out = pd.merge(df, y_hats_df, how = 'left', left_index = True, right_index = True)

Hope this makes sense and helps!