如此处所指定的https://stackoverflow.com/a/35662770/5757129,我存储了我的第一个模型的coef和截距。后来,我将它们作为初始化程序传递给我的第二个fit(),如下所示,用于在旧模型之上学习新数据。
from sklearn import neighbors, linear_model
import numpy as np
import pickle
import os
def train_data():
x1 = [[8, 9], [20, 22], [16, 18], [8,4]]
y1 = [0, 1, 2, 3]
#classes = np.arange(10)
#sgd_clf = linear_model.SGDClassifier(learning_rate = 'constant', eta0 = 0.1, shuffle = False, n_iter = 1,warm_start=True)
sgd_clf = linear_model.SGDClassifier(loss="hinge",max_iter=10000)
sgd_clf.fit(x1,y1)
coef = sgd_clf.coef_
intercept = sgd_clf.intercept_
return coef, intercept
def train_new_data(coefs,intercepts):
x2 = [[18, 19],[234,897],[20, 122], [16, 118]]
y2 = [4,5,6,7]
sgd_clf1 = linear_model.SGDClassifier(loss="hinge",max_iter=10000)
new_model = sgd_clf1.fit(x2,y2,coef_init=coefs,intercept_init=intercepts)
return new_model
if __name__ == "__main__":
coefs,intercepts= train_data()
new_model = train_new_data(coefs,intercepts)
print(new_model.predict([[16, 118]]))
print(new_model.predict([[18, 19]]))
print(new_model.predict([[8,9]]))
print(new_model.predict([[20,22]]))
当我运行它时,我得到仅从new_model训练的标签。例如,print(new_model.predict([[8,9]]))
必须将标签打印为0,print(new_model.predict([[20,22]]))
必须将标签打印为1.但它会打印从4到7匹配的标签。
我是否以错误的方式将旧模型中的系数和截距传递给新模型?
编辑:根据@vital_dml回答
重新构建问题答案 0 :(得分:1)
我不确定为什么你需要将系数和截距从第一个模型传递到第二个,但是,你得到这样的错误,因为你的第一个模型是针对4个类y1 = [0, 1, 2, 3]
进行训练的,而第二个模型是2个课程y2 = [4,5]
,这是有争议的。
根据scikit-learn documentation,您的linear_model.SGDClassifier()
会返回:
coef_:array,shape(1,n_features)if n_classes == 2 else(n_classes,n_features) - 赋予特征的权重。
intercept_:array,shape(1,)if n_classes == 2 else(n_classes,) - 决策函数中的常数。
因此,在您的问题中,两个模型中的类和特征的数量必须相同。
无论如何,我鼓励你认为你真的需要这样做吗?也许你可以将这些载体连接起来。