[英]Python apply list of coefficient (from regression model) on new data
I have a list of coeeficient from sklearn logistic regression model:我有一个来自 sklearn 逻辑回归模型的系数列表:
[-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.我想添加一个新列 - 这是在每行上应用 coeffs 列表的结果。 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
所以我们可以做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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.