拟合同一模型的多个实例

2024-04-27 02:54:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的数据集上运行多项逻辑回归。一旦我运行了一个,我将尝试将刚刚运行的模型附加到一个列表中,以便以后可以访问特定的模型。 代码看起来像这样

models = []
for i in range(0, 10):
  model = sklearn.linear_model.LogisticRegression()
  model.fit(x,y)
  models.append(model)

其中x和y在每个for迭代中是不同的。问题是,我在最终数组“models”的每个元素中得到的系数/模型与循环的最终迭代相同。我假设这是因为我只是在每次迭代中更新引用,而不是每次都创建一个新的逻辑回归模型。有人能告诉我一个更好的方法吗


Tags: 数据代码in模型列表formodelmodels
1条回答
网友
1楼 · 发布于 2024-04-27 02:54:03

您可能对^{} function感兴趣

sklearn.base.clone(estimator, *, safe=True)[source]

Constructs a new unfitted estimator with the same parameters. Clone does a deep copy of the model in an estimator without actually copying attached data. It yields a new estimator with the same parameters that has not been fitted on any data.

from sklearn.base import clone

model = LogisticRegression()
new_model = clone(model)

相关问题 更多 >