As specified here, https://mcmap.net/q/332454/-save-progress-between-multiple-instances-of-partial_fit-in-python-sgdclassifier, i stored the coef and intercept of my first model. Later, i am passing them as initializers to my second fit() as shown below for learning new data on top of old model.
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]]))
When i run this, i get the lables that are trained only from new_model. For instance, print(new_model.predict([[8,9]]))
has to print label as 0 and print(new_model.predict([[20,22]]))
has to print label as 1. But it prints lables matching from 4 to 7.
Am i passing the coef and intercepts from old model to the new one in wrong way ?
EDIT: Reframed the question as per @vital_dml answer