Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying DF row by coefficients

I want to store the coefficients of a statsmodels.api model for future use (so I don't have to run the model every time). When I get a new dataframe for which I want to make a prediction on, I want to be able to multiply each row of the dataframe by the coefficients( i.e. model.params). I would then sum the results of each row*coefficients to get the prediction for that row. However, it does not seem to be working for me when I try:

preds = []
for row in df.iterrows():
    preds.append((model.params*row).sum())

Edit: example

df:

Height    Weight    Color
  6         5         3
  6         2         4
  9         1         9
  10        3         3

coefficients:

Height: -1.6403

Weight: 2.0435

Color: 300.4532

like image 991
big11mac Avatar asked Feb 24 '26 13:02

big11mac


1 Answers

I would consider doing something like:

df.dot(model.params)

This computes the dot product on each of the rows of the DataFrame.

like image 107
Gabi Lee Avatar answered Feb 27 '26 04:02

Gabi Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!