我正在使用Spotlight的ImplicitFactorizationModel构建隐式反馈模型。数据框有4列-'user_ids','timestamps','item_ids','ratings'。
我收到“ user_ids”不存在的错误。
进口
import pandas as pd
import numpy as np
from spotlight.cross_validation import random_train_test_split
from spotlight.evaluation import mrr_score
from spotlight.factorization.implicit import ImplicitFactorizationModel
from spotlight.datasets import _transport
from spotlight.interactions import Interactions
数据文件
https://takeafile.com/?f=pevufofixe
代码
deskdrop_df = pd.read_csv('./DeskDrop.csv')
deskdrop_df.head(3)
deskdrop_df.info()
输出:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 69868 entries, 0 to 69867
Data columns (total 4 columns):
user_ids 69868 non-null int64
timestamps 69868 non-null int64
item_ids 69868 non-null int64
ratings 69868 non-null float64
dtypes: float64(1), int64(3)
memory usage: 2.1 MB
更改数据类型
deskdrop_df['ratings'] = deskdrop_df['ratings'].astype('float32')
int_df = {'item_ids':deskdrop_df['item_ids'],
'user_ids':deskdrop_df['user_ids'],
'ratings':deskdrop_df['ratings'],
'timestamps':deskdrop_df['timestamps']}
train, test = random_train_test_split(Interactions(**int_df),test_percentage=0.1)
print("train ",type(train))
print("test ",type(test))
model = ImplicitFactorizationModel(loss='bpr',
n_iter=10,
batch_size=1024,
learning_rate=1e-2,
l2=1e-6)
model.fit(np.array(train))
mrr = mrr_score(model, test)
mrr
错误
69868
train <class 'spotlight.interactions.Interactions'>
test <class 'spotlight.interactions.Interactions'>
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-27-292b7a32b71e> in <module>
12 l2=1e-6)
13
---> 14 model.fit(np.array(train))
15
16 mrr = mrr_score(model, test)
~/anaconda3/envs/spotlight/lib/python3.6/site-packages/spotlight/factorization/implicit.py in fit(self, interactions, verbose)
200 """
201
--> 202 user_ids = interactions.user_ids.astype(np.int64)
203 item_ids = interactions.item_ids.astype(np.int64)
204
AttributeError: 'numpy.ndarray' object has no attribute 'user_ids'