sklearn.bash clone的作用

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)

lr = LogisticRegression()

clf1 = lr.fit(X1, y1)
print("Classifier for data set 1: ")
print ("  - intercept: ", clf1.intercept_)
print ("  - coef_: ", clf1.coef_)
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_)

 结果输出:
sklearn.bash clone的作用_第1张图片

这里只有一个模型,首先lr在数据集1上训练,然后又在数据集2上训练;最后模型参数只有一个拟合数据集2 的生成;

避免这个问题的办法就是新建两个类;本质上来讲lr是个对象,fit只是个函数;

lr1 = LogisticRegression()
lr2 = LogisticRegression()

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

但如何copy呢,克隆模型的方法如下:

from sklearn.base import clone

lr1 = LogisticRegression()
lr2 = clone(lr1)

 

你可能感兴趣的:(ml,python)