/// <summary> /// Predicts the related items, given the item index and the type (either References or Entities). /// </summary> /// <param name="itemId">The item index in the corresponding feature map column.</param> /// <param name="count">count</param> /// <param name="itemType"> /// Type of item to return related items (i.e. References = user ratings OR Entities = books or /// movies) /// </param> /// <returns>Vector of predictions.</returns> public Vector PredictRelated(int itemId, int count = 5, ItemType itemType = ItemType.References) { var predictions = (ThetaX * ThetaY.T).Each((v, r, c) => v + Mu[r]); var feature = itemType == ItemType.Entities ? predictions[EntityFeatureMap.IndexOf(itemId), VectorType.Col] : predictions[ReferenceFeatureMap.IndexOf(itemId), VectorType.Row]; var result = Vector.Zeros(count); switch (itemType) { case ItemType.Entities: { result = predictions.GetCols() .Select((s, i) => new { Col = s, Idx = i }) .OrderBy(v => RelatedDistanceFunction.Compute(feature, v.Col)) .Take(count) .Select(s => (double)s.Idx).ToVector(); } break; case ItemType.References: { result = predictions.GetRows() .Select((s, i) => new { Row = s, Idx = i }) .OrderBy(v => RelatedDistanceFunction.Compute(feature, v.Row)) .Take(count) .Select(s => (double)s.Idx).ToVector(); } break; } return(result); }
/// <summary> /// Predicts all the recommendations of the Items for the supplied reference, i.e. a user. /// </summary> /// <param name="referenceId">Reference index to use for generating predictions.</param> /// <returns>Vector of predictions.</returns> public Vector Predict(int referenceId) { // [entities x features] * [references * features] var predictions = (ThetaX * ThetaY.T).Each((v, r, c) => v + Mu[r]); int[] indices; predictions[ReferenceFeatureMap.IndexOf(referenceId), VectorType.Col].Sort(false, out indices); return(Y.Slice(indices, true)); }