I want to score a model on a pandas DataFrame and then create a column with that model's predictions in the same DataFrame. The new column should be named so that it references the appropriate model so this can be done multiple times with multiple models. I can do this in R using deparse
and substitute
like so:
df <- data.frame(a=1:5+rnorm(1:5), b=6:10+rnorm(1:5), y=11:15+rnorm(1:5))
ols <- lm(df$y ~ df$a + df$b)
func <- function(df, model){
new_col <- paste0(deparse(substitute(model)), '_predictions')
df[, new_col] <- predict(model, df)
return(df)
}
func(df, ols)
a b y ols_predictions
1 1.569142 7.735250 11.90998 12.99388
2 0.828704 4.468299 12.16632 12.01042
3 2.270323 8.135620 14.25781 13.51283
4 1.847564 9.602450 13.76106 13.46148
5 5.776140 10.723743 16.08072 16.19727
What would be the equivalent of this in Python?