簡體   English   中英

Python 在新數據上應用系數列表(來自回歸模型)

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

我有一個來自 sklearn 邏輯回歸模型的系數列表:

[-0.52  0.31  0.059 0.1 ]

現在,我有一個新的數據框,例如:

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

我想添加一個新列 - 這是在每行上應用 coeffs 列表的結果。 所以這些值將是:

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

最好的方法是什么?

謝謝!

所以我們可以做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])

更新

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

這就是矩陣乘法。 你可以做:

df['new_col'] = df @ a

輸出:

   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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM