lightfm icon indicating copy to clipboard operation
lightfm copied to clipboard

ValueError: Sample weight and interaction matrix entries must be in the same order

Open Sachet12345 opened this issue 3 years ago • 2 comments

I am really new to using lightFM and can't seem to figure out how to debug this error. Attaching the code snippet :

    interactions, weights = dataset.build_interactions(user_item_interactions['user_item_count'])
    train, test = random_train_test_split(interactions, test_percentage = 0.2)
    
    from lightfm import LightFM
    
    model = LightFM(
        no_components=350,
        learning_rate=0.05,
        loss='warp')
    
    model.fit(
        train,
        item_features=item_features,
        sample_weight = weights,
        epochs=5, num_threads=4, verbose=True)

I do not get this error if I fit the whole interaction matrix instead of the train, test split. Thanks.

Sachet12345 avatar Feb 20 '21 16:02 Sachet12345

Hi @Sachet12345 ,

I think you forgot to mention the error you receive. I assume the cause of your error is that you mix a subset of interactions (train) with the full set of weights. You need to also call random_train_test_split for your weights! See this comment for more help: https://github.com/lyst/lightfm/issues/412#issuecomment-459360226

SimonCW avatar Feb 25 '21 17:02 SimonCW

Hi @Sachet12345,

I think the issue is that you're splitting the interaction matrix but not the weight matrix and when you're doing the fit it throws an error because of mismatch between both matrices. You can split both using the following method:

train, test = random_train_test_split(interactions, test_percentage=0.2, random_state=np.random.RandomState(3))
train_weights, test_weights = random_train_test_split(weights, test_percentage=0.2, random_state=np.random.RandomState(3))

    model.fit(
        train,
        item_features=item_features,
        sample_weight = train_weights,
        epochs=5, num_threads=4, verbose=True)

Make sure to use random state so that both the splits are consistent.

wahabaftab avatar Jul 04 '23 08:07 wahabaftab