我正在构建一个推荐系统,以便根据用户特性和项目特性向员工推荐培训,LightFM根据文档,这是一个很好的算法。
我的用户数据帧:
User-Id name age los ou gender skills
0 1 Luis 21 IFS architecture M python
1 2 Peter 22 ADV pmo M pm
2 3 Jurgen 23 IFS architecture M sql
3 4 Bart 24 IFS architecture M python
4 5 Cristina 25 ADV pmo F pm
5 6 Lambert 33 IFS development M sql
6 7 Rahul 44 IFS development M python
我的训练数据帧
Training-Id training name main skill
0 1 basic python python
1 2 advanced python python
2 3 basic scrum pm
3 4 advanced scrum pm
4 5 basic sql sql
5 6 advanced sql sql
我的培训采用了数据帧(10表示用户接受了该培训(所以我的重量只有10
User-Id Training-Id TrainingTaken
0 1 1 10
1 1 2 10
2 2 3 10
3 2 4 10
4 3 5 10
5 3 6 10
6 4 1 10
7 4 2 10
为了创建tha矩阵,我发现了一个很好的帮助工具:https://github.com/Med-ELOMARI/LightFM-Dataset-Helper
因此:
items_column = "Training-Id"
user_column = "User-Id"
ratings_column = "TrainingTaken"
items_feature_columns = [
"training name",
"main skill"
]
user_features_columns = ["name","age","los","ou", "gender", "skills"]
dataset_helper_instance = DatasetHelper(
users_dataframe=usersdf,
items_dataframe=trainingsdf,
interactions_dataframe=trainingstakendf,
item_id_column=items_column,
items_feature_columns=items_feature_columns,
user_id_column=user_column,
user_features_columns=user_features_columns,
interaction_column=ratings_column,
clean_unknown_interactions=True,
)
# run the routine
# you can alslo run the steps separately one by one | routine function is simplifying the flow
dataset_helper_instance.routine()
上述助手返回交互矩阵、权重矩阵等
dataset_helper_instance.weights.todense()
Output menu
matrix([[10., 10., 0., 0., 0., 0.],
[ 0., 0., 10., 10., 0., 0.],
[ 0., 0., 0., 0., 10., 10.],
[10., 10., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.]], dtype=float32)
dataset_helper_instance.interactions.todense()
matrix([[1., 1., 0., 0., 0., 0.],
[0., 0., 1., 1., 0., 0.],
[0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]], dtype=float32)
然后我训练测试拆分并拟合模型
from lightfm import LightFM
from lightfm.cross_validation import random_train_test_split
(train, test) = random_train_test_split(interactions=dataset_helper_instance.interactions, test_percentage=0.2)
model = LightFM(loss='warp')
model.fit(
interactions=dataset_helper_instance.interactions,
sample_weight=dataset_helper_instance.weights,
item_features=dataset_helper_instance.item_features_list,
user_features=dataset_helper_instance.user_features_list,
verbose=True,
epochs=50,
num_threads=20,
)
然后我检查AUC和精度:
from lightfm.evaluation import precision_at_k
from lightfm.evaluation import auc_score
train_precision = precision_at_k(model, train,item_features=dataset_helper_instance.item_features_list, user_features=dataset_helper_instance.user_features_list , k=10).mean()
test_precision = precision_at_k(model, test, item_features=dataset_helper_instance.item_features_list, user_features=dataset_helper_instance.user_features_list,k=10).mean()
train_auc = auc_score(model, train,item_features=dataset_helper_instance.item_features_list, user_features=dataset_helper_instance.user_features_list).mean()
test_auc = auc_score(model, test,item_features=dataset_helper_instance.item_features_list, user_features=dataset_helper_instance.user_features_list).mean()
print('Precision: train %.2f, test %.2f. '% (train_precision, test_precision))
print('AUC: train %.2f, test %.2f.' % (train_auc, test_auc))
Precision: train 0.15, test 0.10.
AUC: train 0.90, test 1.00.
然后我为现有用户做预测
scores = model.predict(user_ids=6, item_ids=[1,2,3,5,6])
print(scores)
[ 0.01860116 -0.20987387 0.06134995 0.08332028 0.13678455]
太好了,我可以得到一些关于用户ID 6的训练预测。
现在我想预测新用户,(冷启动(
我尝试了以下方法:
dataset = Dataset()
new_user_feature = [8,{'name:John', 'Age:33', 'los:IFS','ou:development', 'skills:sql'} ]
new_user_feature = [8,new_user_feature]
new_user_feature = dataset.build_user_features([new_user_feature])
#predict new users User-Id name age los ou gender skills
model.predict(0, item_ids=[1,2,3,5,6], user_features=new_user_feature)
然而,我得到了这个错误:
ValueError: user id 8 not in user id mappings.
我在这里错过了什么?
我无法测试它,但我认为问题出在您编写时
new_user_feature = [8,{'name:John', 'Age:33', 'los:IFS','ou:development', 'skills:sql'} ]
new_user_feature = [8,new_user_feature]
根据文档,dataset.build_user_features(..)
想要形式为(user id, [list of feature names])
或(user id, {feature name: feature weight})
的可迭代项。
在您的情况下,我认为您应该将上面的两行替换为:
new_user_feature = [8,{'name':'John', 'Age':33, 'los':'IFS','ou':'development', 'skills':'sql'} ]
# Is the gender missing?
如果它不起作用,也许输入格式是这样的:
new_user_feature = [8,['John', 33, 'IFS', 'development', 'sql'] ]
让我知道它是否解决了问题