Search code examples
pythonpandasdataframescikit-learnlogistic-regression

Python apply list of coefficient (from regression model) on new data


I have a list of coeeficient from sklearn logistic regression model:

[-0.52  0.31  0.059 0.1 ]

Now , I have a new dataframe, for example:

    df =  A  B  C  D
          1  5  2  7
          6  2  1  9

And I want to add a new column - that is the result of applying the list of coeffs on each row. So the values will be:

1*-0.52 + 5*0.31 + 2*0.059 + 7*0.1 = 1.848
6*-0.52 + 2*0.31 + 1*0.059 + 9*0.1 = -1.541

What is the best way to do that?


Solution

  • This is matrix multiplication. You can do:

    df['new_col'] = df @ a
    

    Output:

       A  B  C  D  new_col
    0  1  5  2  7    1.848
    1  6  2  1  9   -1.541