lyst / lightfm

A Python implementation of LightFM, a hybrid recommendation algorithm.
Apache License 2.0
4.66k stars 679 forks source link

Is there a way to save Dataset()? #698

Open Lulu20220 opened 10 months ago

Lulu20220 commented 10 months ago

I have a trained model saved as a pickle file. My model has both user and item features. When I load the model, I can make predictions for seen users by calling pickled_model.predict(user_ids = [0], item_ids =[0])

But, I want to adjust it to a cold start problem, making predictions for new users. In this case, it requires to have dataset.fit_partial() , where the dataset should be fitted in the old dataset. It there a way to save and retrieve the dataset? Or we need to refit it every time before calling fit_partial()

Saba101 commented 9 months ago

Did you try saving the dataset as pickle? I think it should work:

import pickle
pkl_ext = '.pkl'
dataset_filename = 'lightfm_dataset_abc' + pkl_ext

# Save the dataset to the file using pickle
with open(dataset_filename, 'wb') as dataset_file:
    pickle.dump(dataset,  dataset_file)

And when you want to load dataset:

# Load the dataset from the specified file path using pickle
with open(dataset_filename, 'rb') as dataset_file:
    dataset = pickle.load(dataset_file)

# Fit more data to the loaded dataset
# For example, you can add more user-item interactions:
# dataset.fit_partial(users=new_user_ids, items=new_item_ids)