-1

I tried saving my Multinomial Naive Bayes classifier as a .pkl file but encountered an error.

I tried

import pickle

with open(r'C:\Users\User\Desktop\clf.pkl','rb') as f:
    pickle.dump(mnb,f) 
#mnb is the MultinomialNB classifier

I am getting the error as

FileNotFoundError: [Errno 2] No such file or directory: 
'C:\\Users\\User\\Desktop\\clf.pkl'

Do I need to create a file first somewhere?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
The Doctor
  • 332
  • 2
  • 5
  • 16

1 Answers1

2

If you are trying to save it, then open it in 'wb' write mode instead of 'rb' read mode as following:

with open(r'C:\Users\User\Desktop\clf.pkl','wb') as f:
    pickle.dump(mnb,f)

In write mode, it will automatically create a file if not present!

It will solve your problem. Hope, it helps.

abhinav
  • 1,108
  • 11
  • 23