1

I have created a model that predicts the name of a flower based on its features using LinearSVC in sklearn and saved the model using pickle.

for example my target variables are lily and rose - so given the features the model has the capability to return either lily or rose as the flower name.

At a later stage i want to rename lily to lilly as i felt that is a better name.

I know of two methods to achieve this.

One is to retrain the model by renaming the target variables of lily to lilly in my traning data, which means my model in future will return lilly and `rose

The other is to keep the model as it is and then after prediction use a mapping dictionary {'lily' : 'lilly','rose': 'rose'} to get the renamed value.

Is there any other way to tweek the existing model which is saved after pickling so that the model returns lily as lilly from now on?

thanks for helping

Venkatachalam
  • 12,957
  • 8
  • 35
  • 62
Jithin P James
  • 508
  • 1
  • 5
  • 16

1 Answers1

2

You just have to update the classes variable of the classifier after you load your pickle file. Quick example with LinearSVC. This can work for any Sklearn compatible classifier.


>>> from sklearn.svm import LinearSVC
>>> svc = LinearSVC(random_state=42).fit(df.data, df.target)
>>> svc.predict(df.data.iloc[0:2, :])
array(['setosa', 'setosa'], dtype=object)
>>> svc.classes_ = np.array(['class1', 'class2', 'class3'])
>>> svc.predict(df.data.iloc[0:2, :])
array(['class1', 'class1'], dtype='<U6')
Venkatachalam
  • 12,957
  • 8
  • 35
  • 62