python sklearn中的fit方法 [英] fit method in python sklearn
问题描述
我问自己关于sklearn中的fit方法的各种问题.
I am asking myself various questions about the fit method in sklearn.
问题1:当我这样做时:
Question 1: when I do:
from sklearn.decomposition import TruncatedSVD
model = TruncatedSVD()
svd_1 = model.fit(X1)
svd_2 = model.fit(X2)
在此过程中,变量模型的内容是否发生任何变化?
Is the content of the variable model changing whatsoever during the process?
问题2:当我这样做时:
Question 2: when I do:
from sklearn.decomposition import TruncatedSVD
model = TruncatedSVD()
svd_1 = model.fit(X1)
svd_2 = svd_1.fit(X2)
svd_1发生了什么?换句话说,svd_1已经安装好了,我又重新安装了它,那么它的组件正在发生什么事情?
What is happening to svd_1? In other words, svd_1 has already been fitted and I fit it again, so what is happenning to its component?
推荐答案
问题1:变量模型的内容在此过程中是否发生任何变化?
Question 1: Is the content of the variable model changing whatsoever during the process?
是的. fit
方法修改对象.并且它返回对该对象的引用.因此,保重!在第一个示例中,所有三个变量model
,svd_1
和svd_2
实际上都引用相同的对象.
Yes. The fit
method modifies the object. And it returns a reference to the object. Thus, take care! In the first example all three variables model
, svd_1
, and svd_2
actually refer to the same object.
from sklearn.decomposition import TruncatedSVD
model = TruncatedSVD()
svd_1 = model.fit(X1)
svd_2 = model.fit(X2)
print(model is svd_1 is svd_2) # prints True
问题2: svd_1发生了什么事?
Question 2: What is happening to svd_1?
model
和svd_1
指的是同一对象,因此第一个示例和第二个示例之间绝对没有区别.
model
and svd_1
refer to the same object, so there is absolutely no difference between the first and the second example.
最后的评论:
在两个示例中发生的都是fit(X1)
的结果被fit(X2)
覆盖,如David Maust的答案<.如果要将两个不同的模型拟合到两个不同的数据集,则需要执行以下操作:
Final Remark:
What happens in both examples is that the result of fit(X1)
is overwritten by fit(X2)
, as pointed out in the answer by David Maust. If you want to have two different models fitted to two different sets of data you need to do something like this:
svd_1 = TruncatedSVD().fit(X1)
svd_2 = TruncatedSVD().fit(X2)
这篇关于python sklearn中的fit方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!