![](/img/trans.png)
[英]How to find beta values in Logistic Regression model with sklearn
[英]How to retrain logistic regression model in sklearn with new data
如何在sklearn python中重新訓練我現有的機器學習模型?
我有成千上萬條記錄,用於訓練模型並使用pickle
將其作為.pkl
文件轉儲。 首次訓練模型時,我在創建邏輯回歸對象時使用了warmStart = True
參數。
樣例代碼:
log_regression_model = linear_model.LogisticRegression(warm_start = True)
log_regression_model.fit(X, Y)
# Saved this model as .pkl file on filesystem like pickle.dump(model,open('model.pkl', wb))
我想將其與最新的每日數據保持同步。 為此,我要打開現有的模型文件並獲取過去24小時的新數據並再次對其進行訓練。
樣例代碼:
#open the model from filesystem
log_regression_model = pickle.load(open('model.pkl','rb'))
log_regression_model.fit(X, Y) # New X, Y here is data of last 24 hours only. Few hundreds records only.
但是,當我通過從文件系統中加載模型來重新訓練模型時,似乎會刪除已創建具有數千條記錄的現有模型,並在最近24小時內創建具有數百條記錄的新模型(具有數千條記錄的模型的大小為3MB在文件系統上,而新的重新訓練模型只有67KB)
我試過使用warmStart選項。 如何重新訓練我的LogisticRegression模型?
當在訓練模型上使用fit
,您基本上會舍棄所有先前的信息。
Scikit-learn的某些模型具有partial_fit
方法,可用於增量訓練,如文檔所示 。
我不記得是否可以在sklearn中重新訓練Logistic回歸,但是sklearn具有SGDClassifier
,它的loss=log
運行帶有隨機梯度下降優化的Logistic回歸,並且具有partial_fit
方法。
LogicsticRegression
對象的大小與用於訓練它的樣本數量LogicsticRegression
。
from sklearn.linear_model import LogisticRegression
import pickle
import sys
np.random.seed(0)
X, y = np.random.randn(100000, 1), np.random.randint(2, size=(100000,))
log_regression_model = LogisticRegression(warm_start=True)
log_regression_model.fit(X, y)
print(sys.getsizeof(pickle.dumps(log_regression_model)))
np.random.seed(0)
X, y = np.random.randn(100, 1), np.random.randint(2, size=(100,))
log_regression_model = LogisticRegression(warm_start=True)
log_regression_model.fit(X, y)
print(sys.getsizeof(pickle.dumps(log_regression_model)))
結果是
1230
1233
您可能保存了錯誤的模型對象。 確保您要保存log_regression_model。
pickle.dump(log_regression_model, open('model.pkl', 'wb'))
由於模型大小如此不同,並且LogisticRegression
對象不會隨着不同數量的訓練樣本而改變其大小,因此,似乎使用了不同的代碼來生成您保存的模型和這個新的“重新訓練”的模型。
說了這么多,看起來好像warm_start在這里什么也沒做:
np.random.seed(0)
X, y = np.random.randn(200, 1), np.random.randint(2, size=(200,))
log_regression_model = LogisticRegression(warm_start=True)
log_regression_model.fit(X[:100], y[:100])
print(log_regression_model.intercept_, log_regression_model.coef_)
log_regression_model.fit(X[100:], y[100:])
print(log_regression_model.intercept_, log_regression_model.coef_)
log_regression_model = LogisticRegression(warm_start=False)
log_regression_model.fit(X[100:], y[100:])
print(log_regression_model.intercept_, log_regression_model.coef_)
log_regression_model = LogisticRegression(warm_start=False)
log_regression_model.fit(X, y)
print(log_regression_model.intercept_, log_regression_model.coef_)
給出:
(array([ 0.01846266]), array([[-0.32172516]]))
(array([ 0.17253402]), array([[ 0.33734497]]))
(array([ 0.17253402]), array([[ 0.33734497]]))
(array([ 0.09707612]), array([[ 0.01501025]]))
基於另一個問題 ,如果您使用其他求解器(例如LogisticRegression(warm_start=True, solver='sag')
), warm_start
將產生一定的效果,但是仍然與重新訓練整個數據集的效果不同。添加的新數據。 例如,以上四個輸出變為:
(array([ 0.01915884]), array([[-0.32176053]]))
(array([ 0.17973458]), array([[ 0.33708208]]))
(array([ 0.17968324]), array([[ 0.33707362]]))
(array([ 0.09903978]), array([[ 0.01488605]]))
您可以看到中間的兩行是不同的,但差別不大。 它所做的只是將最后一個模型中的參數用作重新訓練帶有新數據的新模型的起點。 聽起來您想要做的就是保存數據,並在每次添加數據時結合使用舊數據和新數據對它們進行重新訓練。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.