增量拟合sklearn RandomForestClassifier
Incrementally fitting sklearn RandomForestClassifier
我正在使用一个在每次迭代时生成数据的环境。我想保留之前迭代的模型并将新数据添加到现有模型。
我想了解模型拟合的工作原理。它会将新数据与现有模型相匹配,还是会使用新数据创建新模型。
调用新数据拟合:
clf = RandomForestClassifier(n_estimators=100)
for i in customRange:
get_data()
clf.fit(new_train_data) #directly fitting new train data
clf.predict(new_test_data)
或者
保存train数据的历史,对所有历史数据调用fit是唯一的解决办法
clf = RandomForestClassifier(n_estimators=100)
global_train_data = new dict()
for i in customRange:
get_data()
global_train_data.append(new_train_data) #Appending new train data
clf.fit(global_train_data) #Fitting on global train data
clf.predict(new_test_data)
我的目标是有效地训练模型,所以我不想浪费 CPU 时间重新学习模型。
我想确认正确的方法,还想知道该方法在 所有分类器 中是否一致
您的第二种方法是 "correct",正如您已经猜到的那样,在每次追加数据时,它都会从头开始适应新的分类器;但可以说这不是您要找的。
你真正要找的是参数warm_start
;来自 docs:
warm_start : bool, optional (default=False)
When set to True
, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole
new forest. See the Glossary.
因此,您应该使用第一种方法,并进行以下修改:
clf = RandomForestClassifier(n_estimators=100, warm_start=True)
这在分类器之间不一定是一致的(有些分类器带有 partial_fit
方法)——例如 中的 SGDClasssifier
;你应该总是检查相关文档。
我正在使用一个在每次迭代时生成数据的环境。我想保留之前迭代的模型并将新数据添加到现有模型。
我想了解模型拟合的工作原理。它会将新数据与现有模型相匹配,还是会使用新数据创建新模型。
调用新数据拟合:
clf = RandomForestClassifier(n_estimators=100)
for i in customRange:
get_data()
clf.fit(new_train_data) #directly fitting new train data
clf.predict(new_test_data)
或者 保存train数据的历史,对所有历史数据调用fit是唯一的解决办法
clf = RandomForestClassifier(n_estimators=100)
global_train_data = new dict()
for i in customRange:
get_data()
global_train_data.append(new_train_data) #Appending new train data
clf.fit(global_train_data) #Fitting on global train data
clf.predict(new_test_data)
我的目标是有效地训练模型,所以我不想浪费 CPU 时间重新学习模型。
我想确认正确的方法,还想知道该方法在 所有分类器 中是否一致
您的第二种方法是 "correct",正如您已经猜到的那样,在每次追加数据时,它都会从头开始适应新的分类器;但可以说这不是您要找的。
你真正要找的是参数warm_start
;来自 docs:
warm_start : bool, optional (default=False)
When set to
True
, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. See the Glossary.
因此,您应该使用第一种方法,并进行以下修改:
clf = RandomForestClassifier(n_estimators=100, warm_start=True)
这在分类器之间不一定是一致的(有些分类器带有 partial_fit
方法)——例如 SGDClasssifier
;你应该总是检查相关文档。