/// <summary>
        /// Extracts the prediction, orders the action depdendent feature objects accordingly and appends the
        /// action dependent feature objcts that did produce empty examples at the end.
        /// </summary>
        /// <typeparam name="TActionDependentFeature">The action dependent feature type.</typeparam>
        /// <param name="vw">The Vowpal Wabbit instance.></param>
        /// <param name="examples">The list of examples.</param>
        /// <param name="validActionDependentFeatures">The list of non-empty action dependent feature objects.</param>
        /// <param name="emptyActionDependentFeatures">The list of empty action dependent feature objects.</param>
        /// <returns>Returns the ranked list of action dependent features.</returns>
        public static ActionDependentFeature <TActionDependentFeature>[] GetPrediction <TActionDependentFeature>(
            VowpalWabbit vw,
            IReadOnlyList <VowpalWabbitExample> examples,
            IReadOnlyList <ActionDependentFeature <TActionDependentFeature> > validActionDependentFeatures,
            IReadOnlyList <ActionDependentFeature <TActionDependentFeature> > emptyActionDependentFeatures)
        {
            // Since the prediction result is stored in the first example
            // and we'll have to get an actual VowpalWabbitExampt
            var firstExample = examples.FirstOrDefault();

            if (firstExample == null)
            {
                return(null);
            }

            var values = firstExample.GetPrediction(vw, VowpalWabbitPredictionType.Multilabel);

            if (values.Length != validActionDependentFeatures.Count)
            {
                throw new InvalidOperationException("Number of predictions returned unequal number of examples fed");
            }

            var result = new ActionDependentFeature <TActionDependentFeature> [validActionDependentFeatures.Count + emptyActionDependentFeatures.Count];

            int i = 0;

            foreach (var index in values)
            {
                result[i++] = validActionDependentFeatures[index];
            }

            // append invalid ones at the end
            foreach (var f in emptyActionDependentFeatures)
            {
                result[i++] = f;
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Extracts the prediction, orders the action depdendent feature objects accordingly and appends the
        /// action dependent feature objcts that did produce empty examples at the end.
        /// </summary>
        /// <typeparam name="TActionDependentFeature">The action dependent feature type.</typeparam>
        /// <param name="vw">The Vowpal Wabbit instance.></param>
        /// <param name="examples">The list of examples.</param>
        /// <param name="validActionDependentFeatures">The list of non-empty action dependent feature objects.</param>
        /// <param name="emptyActionDependentFeatures">The list of empty action dependent feature objects.</param>
        /// <returns>Returns the ranked list of action dependent features.</returns>
        public static ActionDependentFeature <TActionDependentFeature>[] GetPrediction <TActionDependentFeature>(
            VowpalWabbit vw,
            IReadOnlyList <VowpalWabbitExample> examples,
            IReadOnlyList <ActionDependentFeature <TActionDependentFeature> > validActionDependentFeatures,
            IReadOnlyList <ActionDependentFeature <TActionDependentFeature> > emptyActionDependentFeatures)
        {
            // Since the prediction result is stored in the first example
            // and we'll have to get an actual VowpalWabbitExampt
            var firstExample = examples.FirstOrDefault();

            if (firstExample == null)
            {
                return(null);
            }

            ActionDependentFeature <TActionDependentFeature>[] result;
            int i = 0;

            var values       = firstExample.GetPrediction(vw, VowpalWabbitPredictionType.Dynamic);
            var actionScores = values as ActionScore[];

            if (actionScores != null)
            {
                if (actionScores.Length != validActionDependentFeatures.Count)
                {
                    throw new InvalidOperationException("Number of predictions returned unequal number of examples fed");
                }

                result = new ActionDependentFeature <TActionDependentFeature> [validActionDependentFeatures.Count + emptyActionDependentFeatures.Count];

                foreach (var index in actionScores)
                {
                    result[i]             = validActionDependentFeatures[(int)index.Action];
                    result[i].Probability = index.Score;
                    i++;
                }
            }
            else
            {
                var multilabel = values as int[];
                if (multilabel != null)
                {
                    if (multilabel.Length != validActionDependentFeatures.Count)
                    {
                        throw new InvalidOperationException("Number of predictions returned unequal number of examples fed");
                    }

                    result = new ActionDependentFeature <TActionDependentFeature> [validActionDependentFeatures.Count + emptyActionDependentFeatures.Count];

                    foreach (var index in multilabel)
                    {
                        result[i++] = validActionDependentFeatures[index];
                    }

                    result[0].Probability = 1f;
                }
                else
                {
                    throw new NotSupportedException("Unsupported return type: " + values.GetType());
                }
            }

            // append invalid ones at the end
            foreach (var f in emptyActionDependentFeatures)
            {
                result[i++] = f;
            }

            return(result);
        }