拟合同一个scikit-learn模型的多个实例 [英] Fitting multiple instances of the same scikit-learn model
问题描述
我正在尝试对数据集运行多项多项逻辑回归.一旦我运行了一个模型,我就会尝试将我刚刚运行的模型附加到一个列表中,以便稍后访问特定模型.代码看起来像这样
I am trying to run multiple multinomial logistic regressions on my dataset. Once I run one, I am trying to append the model I just ran to a list, so I can access the specific model later. Code looks something like this
models = []
for i in range(0, 10):
model = sklearn.linear_model.LogisticRegression()
model.fit(x,y)
models.append(model)
在每个 for
迭代中x和y不同.问题在于,我在最终阵列模型"的每个元素中获得的系数/模型与for循环的系数/模型相同.我假设这是因为我每次迭代都只是更新参考,而不是每次都不会创建新的Logistic回归模型.有人可以指出我这样做的更好方法吗?
Where x and y are different in each for
iteration. The issue is that the coefficients/model that i get in each element of the final array "models", is the same as the final iteration of that for loop. I am assuming this is because I am just updating the reference each iteration and not creating a new Logistic Regression model each time. Can someone point me to a better way to do this?
推荐答案
You might be interested in the clone
function.
sklearn.base.clone(estimator,*,safe = True)[源代码]
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.
sklearn.base导入克隆中的
from sklearn.base import clone
model = LogisticRegression()
new_model = clone(model)
这篇关于拟合同一个scikit-learn模型的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!