如何复制估算器以便在多个数据集上使用它



下面是一个创建两个数据集的示例:

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
# data set 1
X1, y1 = make_classification(n_classes=2, n_features=5, random_state=1)
# data set 2
X2, y2 = make_classification(n_classes=2, n_features=5, random_state=2)

我想使用具有相同参数值的 LogisticRegression 估计器来拟合每个数据集上的分类器:

lr = LogisticRegression()
clf1 = lr.fit(X1, y1)
clf2 = lr.fit(X2, y2)
print "Classifier for data set 1: "
print "  - intercept: ", clf1.intercept_
print "  - coef_: ", clf1.coef_
print "Classifier for data set 2: "
print "  - intercept: ", clf2.intercept_
print "  - coef_: ", clf2.coef_

问题是两个分类器是相同的:

Classifier for data set 1: 
  - intercept:  [ 0.05191729]
  - coef_:  [[ 0.06704494  0.00137751 -0.12453698 -0.05999127  0.05798146]]
Classifier for data set 2: 
  - intercept:  [ 0.05191729]
  - coef_:  [[ 0.06704494  0.00137751 -0.12453698 -0.05999127  0.05798146]]

对于这个简单的例子,我可以使用如下内容:

lr1 = LogisticRegression()
lr2 = LogisticRegression()
clf1 = lr1.fit(X1, y1)
clf2 = lr2.fit(X2, y2)

以避免问题。但是,问题仍然存在:通常如何复制/复制具有特定参数值的估算器?

from sklearn.base import clone
lr1 = LogisticRegression()
lr2 = clone(lr1)

相关内容

  • 没有找到相关文章

最新更新