What is the numpy or pandas equivalent of the R function sweep()?
To elaborate: in R let's say we have a coefficient vector, say beta (numeric type) and an array, say data (20x5 numeric type). I want to superimpose the vector on each row of the array and multiply the corresponding elements. And then return the resultant (20x5) array I could achieve this using sweep().
Equivalent sample R code:
beta <- c(10, 20, 30, 40)
data <- array(1:20,c(5,4))
sweep(data,MARGIN=2,beta,`*`)
#---------------
> data
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
> beta
[1] 10 20 30 40
> sweep(data,MARGIN=2,beta,`*`)
[,1] [,2] [,3] [,4]
[1,] 10 120 330 640
[2,] 20 140 360 680
[3,] 30 160 390 720
[4,] 40 180 420 760
[5,] 50 200 450 800
I have heard exciting things about numpy and pandas in Python and it seems to have a lot of R like commands. What would be the fastest way to achieve the same using these libraries? The actual data has millions of rows and around 50 columns. The beta vector is of course conformable with data.