public virtual float Predict(Feedback feedback) { int userId = UsersMap.ToInternalID(feedback.User.Id); int itemId = ItemsMap.ToInternalID(feedback.Item.Id); List <Tuple <int, float> > features = new List <Tuple <int, float> >(); if (!IgnoreFeaturesOnPrediction) { features = feedback.GetAllAttributes().Select(a => FeatureBuilder.TranslateAttribute(a)).NormalizeSumToOne().ToList(); } bool newUser = (userId > MaxUserID); bool newItem = (itemId > MaxItemID); float userAttrsTerm = 0, itemAttrsTerm = 0; foreach (var feat in features) { // if feat_index is greater than MaxFeatureId it means that the feature is new in test set so its factors has not been learnt if (feat.Item1 < NumTrainFeaturs) { float x_z = feat.Item2; userAttrsTerm += newUser ? 0 : x_z *MatrixExtensions.RowScalarProduct(feature_factors, feat.Item1, user_factors, userId); itemAttrsTerm += newItem ? 0 : x_z *MatrixExtensions.RowScalarProduct(feature_factors, feat.Item1, item_factors, itemId); } } float itemBias = newItem ? 0 : item_bias[itemId]; float userItemTerm = (newUser || newItem) ? 0 : MatrixExtensions.RowScalarProduct(user_factors, userId, item_factors, itemId); return(itemBias + userItemTerm + userAttrsTerm + itemAttrsTerm); }
public override float Predict(int user_id, int item_id) { string userIdOrg = UsersMap.ToOriginalID(user_id); string itemIdOrg = ItemsMap.ToOriginalID(item_id); List <Tuple <int, float> > features = new List <Tuple <int, float> >(); if (!IgnoreFeaturesOnPrediction && Split.Container.FeedbacksDic.ContainsKey(userIdOrg, itemIdOrg)) { var feedback = Split.Container.FeedbacksDic[userIdOrg, itemIdOrg]; features = feedback.GetAllAttributes().Select(a => FeatureBuilder.TranslateAttribute(a)).NormalizeSumToOne(Normalize).ToList(); } bool newUser = (user_id > MaxUserID); bool newItem = (item_id > MaxItemID); float userAttrsTerm = 0, itemAttrsTerm = 0; foreach (var feat in features) { // if feat_index is greater than MaxFeatureId it means that the feature is new in test set so its factors has not been learnt if (feat.Item1 < NumTrainFeaturs) { float x_z = feat.Item2; userAttrsTerm += newUser ? 0 : x_z *MatrixExtensions.RowScalarProduct(feature_factors, feat.Item1, user_factors, user_id); itemAttrsTerm += newItem ? 0 : x_z *MatrixExtensions.RowScalarProduct(feature_factors, feat.Item1, item_factors, item_id); } } float itemBias = newItem ? 0 : item_bias[item_id]; float userItemTerm = (newUser || newItem) ? 0 : MatrixExtensions.RowScalarProduct(user_factors, user_id, item_factors, item_id); return(itemBias + userItemTerm + userAttrsTerm + itemAttrsTerm); }
public override float Predict(Feedback feedback) { int userId = UsersMap.ToInternalID(feedback.User.Id); int itemId = ItemsMap.ToInternalID(feedback.Item.Id); var featurs = feedback.GetAllAttributes().Select(a => FeatureBuilder.TranslateAttribute(a)); bool newUser = (userId > MaxUserID); bool newItem = (itemId > MaxItemID); float userAttrsTerm = 0, itemAttrsTerm = 0; foreach (var feat in featurs) { // if feat_index is greater than MaxFeatureId it means that the feature is new in test set so its factors has not been learnt if (feat.Item1 < NumTrainFeaturs) { float x_z = feat.Item2; int g_z = FeatureGroups[feat.Item1]; float alpha_z = weights[g_z]; userAttrsTerm += newUser ? 0 : alpha_z *x_z *MatrixExtensions.RowScalarProduct(feature_factors, feat.Item1, user_factors, userId); itemAttrsTerm += newItem ? 0 : alpha_z *x_z *MatrixExtensions.RowScalarProduct(feature_factors, feat.Item1, item_factors, itemId); } } int u = 0; int i = 1; float alpha_u = weights[u]; float alpha_i = weights[i]; float itemBias = newItem ? 0 : item_bias[itemId]; float userItemTerm = (newUser || newItem) ? 0 : alpha_u *alpha_i *MatrixExtensions.RowScalarProduct(user_factors, userId, item_factors, itemId); return(itemBias + userItemTerm + alpha_u * userAttrsTerm + alpha_i * itemAttrsTerm); }