如何在sklearn python中重新训练我现有的机器学习模型?
我有成千上万的记录用于训练我的模型并使用.pkl
转储为pickle
文件。
在第一次训练模型时,我在创建逻辑回归对象时使用了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模型?
答案 0 :(得分:2)
当您在训练有素的模型上使用fit
时,您基本上会丢弃所有以前的信息。
Scikit-learn有一些模型具有partial_fit
方法,可用于增量训练,in documentation。
我不记得是否可以在sklearn中重新训练Logistic回归,但sklearn有SGDClassifier
loss=log
使用随机梯度下降优化运行Logistic回归,并且partial_fit
方法
答案 1 :(得分:0)
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]]))
基于this other question,warm_start
如果您使用其他解算器(例如LogisticRegression(warm_start=True, solver='sag')
)会产生一些效果,但它仍然与整个重新训练相同添加了新数据的数据集。例如,以上四个输出变为:
(array([ 0.01915884]), array([[-0.32176053]]))
(array([ 0.17973458]), array([[ 0.33708208]]))
(array([ 0.17968324]), array([[ 0.33707362]]))
(array([ 0.09903978]), array([[ 0.01488605]]))
你可以看到中间两条线不同,但差别不大。它所做的就是使用最后一个模型中的参数作为使用新数据重新训练新模型的起点。听起来你想要做的就是保存数据,并在每次添加数据时将旧数据和新数据重新组合起来。