python中的sklearn,python sklearn中的fit方法

I am asking myself various questions about the fit method in sklearn.

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?

Question 2: when I do:

from sklearn.decomposition import TruncatedSVD

model = TruncatedSVD()

svd_1 = model.fit(X1)

svd_2 = svd_1.fit(X2)

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?

解决方案

Question 1: Is the content of the variable model changing whatsoever during the process?

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

Question 2: What is happening to svd_1?

model and svd_1 refer to the same object, so there is absolutely no difference between the first and the second example.

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值