0

I have an n-gram model which uses a custom analyzer defined by a lambda function.

n=3
vect = CountVectorizer(analyzer=lambda x: (x[-i-1:] for i in range(0,min(n,len(x)))))
vect.fit(df.firstname)

I tried saving this model using pickle but I get the following error:

PicklingError                             Traceback (most recent call last)
<ipython-input-123-1bf65fd467e6> in <module>()
----> 1 pickle.dump(vect, open(n_gram_pickle_file, 'wb'))

PicklingError: Can't pickle <function <lambda> at 0x7f6d3f7ae6a8>: attribute lookup <lambda> on 
__main__ failed

Can someone help me how to do this? Is there any other way to save the model other than pickling?

Praneeth Vasarla
  • 113
  • 1
  • 1
  • 9
  • 2
    Does this answer your question? [Can Python pickle lambda functions?](https://stackoverflow.com/questions/25348532/can-python-pickle-lambda-functions) – Danylo Baibak Oct 19 '20 at 06:46

1 Answers1

0

Python cannot pickle lambda functions, this is already explained in this post

A simple solution is to make a proper function doing the same.

Bluexm
  • 157
  • 6