Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error AttributeError: 'implicit.evaluation._memoryviewslice' object has no attribute 'dtype' when calling mean_average_precision_at_k function #726

Open
MRossa157 opened this issue Jan 7, 2025 · 2 comments

Comments

@MRossa157
Copy link

Hello! I encountered an issue when using the mean_average_precision_at_k function from the implicit library.

Problem Description:

When calling the mean_average_precision_at_k function, the following error occurs:

AttributeError: 'implicit.evaluation._memoryviewslice' object has no attribute 'dtype'

Context:

  • Operating System: Windows 10
  • Python: 3.10.7
  • implicit library version: 0.7.2
  • Installed dependencies:
    [tool.poetry.dependencies]
    python = "^3.10.7"
    pandas = "^2.2.2"
    implicit = "^0.7.2"

Steps to Reproduce:

  1. Installed the implicit library version 0.7.2.
  2. Called the mean_average_precision_at_k function with the following parameters:
    metric_map = mean_average_precision_at_k(
        model,
        csr_train,
        csr_test,
        K=6,
        show_progress=True,
    )
  3. Encountered the above-mentioned error.

Expected Behavior:

The function should return the MAP@K metric value without errors.

Additional Information:

  • Tried reinstalling the library and its dependencies, but the error persists.
  • The code includes the following imports:
    import numpy as np
    import pandas as pd
    from implicit.cpu.als import AlternatingLeastSquares as ALScpu
    from implicit.evaluation import mean_average_precision_at_k
    from scipy.sparse import coo_matrix

I would appreciate any assistance in resolving this issue.

@fkurushin
Copy link

fkurushin commented Jan 10, 2025

Hi @MRossa157 ! I have trained model with python implicit package and faced the same problem:

The minimum example to reproduce the error

import os
import random
import pandas as pd
from scipy.sparse import csr_matrix
from implicit.evaluation import train_test_split, ndcg_at_k, mean_average_precision_at_k
from implicit.gpu.als import AlternatingLeastSquares

os.environ['OPENBLAS_NUM_THREADS']="1"
os.environ['CUDA_VISIBLE_DEVICES']="0"

# init random data
n_actions = 100000
max_uid = 100000
max_action_id = 10000

df = pd.DataFrame(data={
    "user_id" : [random.randint(1, max_uid) for i in range(0, n_actions)],
    "action" : [random.randint(1, max_action_id) for i in range(0, n_actions)],
    "impression" : [1 for i in range(0, n_actions)]
})

# convert to sparse format
user_rows = [uid for uid in df.user_id.tolist()]
query_cols = [st for st in df.action.tolist()]
qvecs = csr_matrix((df.impression, (user_rows, query_cols)))

# train test split and model training
train_user_items, test_user_items = train_test_split(qvecs, train_percentage=0.9, random_state=19)

model = AlternatingLeastSquares(factors=130, regularization=0.05, alpha=1.0, calculate_training_loss=True)
model.fit(train_user_items)

# calculate ndcg
ndcg = ndcg_at_k(model, train_user_items, test_user_items, K=14, show_progress=True, num_threads=1)

packages version:
implicit-0.7.2 (built from source)
python-3.11.2
cuda-12.3

os:
Debian GNU/Linux 12

@sorlandet
Copy link

Updating to scipy 1.14.1 should resolve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants