Exemplo n.º 1
0
        /// <summary>
        /// Prints the objects that have probability distributions with entropy higher than the given threshold.
        /// </summary>
        /// <param name="entropy_threshold">entropy_threshold</param>
        /// <returns></returns>
        public string PrintObjectClassProbabilities(double entropy_threshold)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < I; i++)
            {
                double entropy = DawidSkene.Entropy(T, i);
                if (entropy < entropy_threshold)
                {
                    continue;
                }

                string object_name = objects_names[i];
                sb.Append(object_name + "\t");
                for (int j = 0; j < J; j++)
                {
                    string class_name = classes_names[j];
                    sb.Append("Pr[" + class_name + "]=" + T[i, j] + "\t");
                }
                sb.AppendLine();
            }

            return(sb.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Run Dawid-Skene on the data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="fullData">The full data.</param>
        /// <param name="calculateAccuracy">Whether to calculate accuracy</param>
        /// <returns>A results instance</returns>
        public Results RunDawidSkene(IList<Datum> data, IList<Datum> fullData, bool calculateAccuracy)
        {
            // If you want to run Dawid-Skene code, download his code, integrate it into
            // the project, and change false to true below.
            Console.WriteLine("--- Dawid Skene ---");
            PredictedLabel = new Dictionary<string, int?>();
            Mapping = new DataMapping(data);
            var fullDataMapping = new DataMapping(fullData);
            var labelings = data.Select(d => new Labeling(d.WorkerId, d.TaskId, d.WorkerLabel.ToString(), d.GoldLabel.ToString())).ToList();
            DawidSkene ds = new DawidSkene(labelings, null, null);
            // The labels may be in a different order from our data labeling - we need to create a map.
            int[] labelIndexMap = new int[Mapping.LabelCount];
            var dwLabels = ds.classes.Keys.ToArray();
            for (int i = 0; i < Mapping.LabelCount; i++)
            {
                labelIndexMap[i] = Array.IndexOf(dwLabels, (i + Mapping.LabelMin).ToString());
            }

            GoldLabels = fullDataMapping.GetGoldLabelsPerTaskId().
                ToDictionary(kvp => kvp.Key, kvp => kvp.Value == null ? (int?)null : (int?)labelIndexMap[kvp.Value.Value]);

            ds.Estimate(10);

            var inferredLabels = ds.GetObjectClassProbabilities().Select(r => new Discrete(r)).ToArray();
            TrueLabel = inferredLabels.Select((lab, i) => new
            {
                key = Mapping.TaskIndexToId[i],
                val = lab
            }).ToDictionary(a => a.key, a => a.val);

            if (calculateAccuracy)
            {
                UpdateAccuracy();
            }

            return this;
        }