我工作的一個應用程序,它是一種「預測模型作爲一種服務」,結構如下:如何堅持patsy DesignInfo?
- 火車模型離線
- 定期上傳模型參數的「預測服務器「
- 預測服務器需要輸入一個單一的觀察,並輸出預測
我試圖使用懦夫,但是遇到了以下問題:當一個預測進來,我怎麼轉換它正確的形狀,使它看起來像是一排訓練數據?
的帕齊文檔提供了一個例子,當從訓練數據的DesignInfo可用存儲:http://patsy.readthedocs.io/en/latest/library-developers.html#predictions
# offline model training
import patsy
data = {'animal': ['cat','cat','dog','raccoon'],'cuteness': [3,6,10,4]}
eq_string = "cuteness ~ animal"
dmats = patsy.dmatrices(eq_string,data)
design_info = dmats[1].design_info
train_model(dmats)
# online predictions
input_data = {'animal': ['raccoon']}
# if the DesignInfo were available, I could do this:
new_dmat = build_design_matrices([design_info],
input_data)
make_prediction(new_dmat, trained_model)
然後輸出:
[DesignMatrix with shape (1, 3)
Intercept animal[T.dog] animal[T.raccoon]
1 0 1
Terms:
'Intercept' (column 0)
'animal' (columns 1:3)]
注意,這行是相同的形狀作爲訓練數據;它有一列animal[T.dog]
。在我的應用程序中,我沒有辦法訪問DesignInfo來爲新數據構建DesignMatrix。具體而言,預測服務器如何知道訓練數據中有多少其他類別的動物以及以何種順序?
我想我可能只是鹹菜,但事實證明這是不支持尚未:https://github.com/pydata/patsy/issues/26
我也可以簡單地保持矩陣列作爲一個字符串,並重建從網上矩陣,但是這似乎是一個有點脆弱。
有沒有很好的方法來做到這一點?
您能否在服務器中保留'design_info'? (看起來好像會自動發生。)然後,客戶端只需向服務器發送新的'input_data',服務器就會運行'new_dmat'和'make_prediction'行。或者您是否需要關閉並重新啓動服務器而不進行再培訓?在這種情況下,聽起來你需要保存原始的'dmats'和'train_model()'找到的參數。那是你在找什麼? –