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?
Thanks!
So we can do numpy
reshape
l = [-0.52, 0.31, 0.059, 0.1 ]
s = [1, 5 ,2 ,7 ,6 ,2 ,1 ,9]
np.sum(np.array(s).reshape(-1,4)*l, axis=1)
Out[140]: array([ 1.848, -1.541])
Updated
df['New'] = df.dot(l)
df
Out[145]:
A B C D New
0 1 5 2 7 1.848
1 6 2 1 9 -1.541
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.