/// <summary> /// Runs the majority vote method on the data. /// </summary> /// <param name="data">The data</param> /// <param name="calculateAccuracy">Compute the accuracy (true).</param> /// <param name="useVoteDistribution">The true label is sampled from the vote distribution (true) or it is /// taken as the mode of the vote counts (false). /// In the latter case, ties are broken by sampling from the most voted classes.</param> /// <returns>The updated results</returns> public Results RunMajorityVote(IList <Datum> data, bool calculateAccuracy, bool useVoteDistribution) { var dataMapping = new DataMapping(data); Mapping = dataMapping; GoldLabels = Mapping.GetGoldLabelsPerTaskId(); var inferredLabels = useVoteDistribution ? dataMapping.GetVoteDistribPerTaskIndex() : dataMapping.GetMajorityVotesPerTaskIndex().Select(mv => mv == null ? (Discrete)null : Discrete.PointMass(mv.Value, dataMapping.LabelCount)).ToArray(); TrueLabel = inferredLabels.Select((lab, i) => new { key = dataMapping.TaskIndexToId[i], val = lab }).ToDictionary(a => a.key, a => a.val); if (calculateAccuracy) { UpdateAccuracy(); } return(this); }
/// <summary> /// Run Dawid-Skene on the data. /// </summary> /// <param name="data">The data.</param> /// <param name="calculateAccuracy">Whether to calculate accuracy</param> /// <returns>A results instance</returns> public Results RunDawidSkene(IList <Datum> data, bool calculateAccuracy) { // If you want to run Dawid-Skene code, download his code, integrate it into // the project, and change false to true below. #if false var dataMapping = new DataMapping(data); Mapping = dataMapping; 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[dataMapping.LabelCount]; var dwLabels = ds.classes.Keys.ToArray(); for (int i = 0; i < dataMapping.LabelCount; i++) { labelIndexMap[i] = Array.IndexOf(dwLabels, (i + dataMapping.LabelMin).ToString()); } GoldLabels = Mapping.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 = dataMapping.TaskIndexToId[i], val = lab }).ToDictionary(a => a.key, a => a.val); if (calculateAccuracy) { UpdateAccuracy(); } return(this); #else throw new ApplicationException("To support Dawid-Skene, you must link to the C# version of their code"); #endif }