Exemplo n.º 1
0
        public IEnumerable <(Instance, double)> GetAllBetaValues()
        {
            Dictionary <string, int> homoCount = new Dictionary <string, int>();

            TrainingInstances.ForEach(i =>
            {
                if (!homoCount.ContainsKey(i.LabelValue ?? throw new NullReferenceException("Cannot compute beta value for an unlabeled instance. ")))
                {
                    homoCount.Add(i.LabelValue, TrainingInstances.Count(instance => instance.LabelValue == i.LabelValue));
                }
            });

            double[,] distStats = new double[TrainingInstances.Count, TrainingInstances.Count];
            for (int i = 1; i < TrainingInstances.Count; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    distStats[i, j] = distStats[j, i] = EuclideanDistance(TrainingInstances[i], TrainingInstances[j]);
                }
            }

            for (int i = 0; i < TrainingInstances.Count; i++)
            {
                Instance currInstance = TrainingInstances[i];
                Dictionary <Instance, double> distToOtherInstances = new Dictionary <Instance, double>(Enumerable.Range(0, TrainingInstances.Count).Select(j => new KeyValuePair <Instance, double>(TrainingInstances[j], distStats[i, j])));
                distToOtherInstances.Remove(TrainingInstances[i]);
                yield return(currInstance,
                             distToOtherInstances.OrderBy(kvp => kvp.Value).Take(homoCount[currInstance.LabelValue ?? throw new NullReferenceException("Cannot compute beta value for an unlabeled instance. ")] - 1)
        public override void Train()
        {
            if (UseLaplaceCorrection)
            {
                priorProb = new Dictionary <string, double>();
                IEnumerable <string?> distinctLabels = TrainingInstances.Select(i => i.LabelValue).Distinct();
                foreach (string?labelValue in distinctLabels)
                {
                    priorProb.Add(labelValue ?? throw new NullReferenceException("Unlabeled instance in training instances. "), TrainingInstances.Count(i => i.LabelValue == labelValue) / (double)TrainingInstances.Count);
                }
            }
            string splitFeatureName = GetSplitFeature(TrainingInstances, out double threshold);

            rootNode = new Node(TrainingInstances, splitFeatureName, threshold);
            SplitRecursive(rootNode, -1);
        }
Exemplo n.º 3
0
 public double GetBetaValue(Instance testingInstance) =>
 GetNeighbors(testingInstance, TrainingInstances.Count(i => i.LabelValue == testingInstance.LabelValue) - 1)
 .Where(i => i.LabelValue == testingInstance.LabelValue).Sum(i => 1.0 / (1.0 + EuclideanDistance(i, testingInstance)))
 / TrainingInstances.Where(i => i != testingInstance).Sum(i => 1.0 / (1.0 + EuclideanDistance(i, testingInstance)));
Exemplo n.º 4
0
        /// <summary>
        /// For experimental use. Alpha measures the ratio of agreeing neighbors. In this case every instance in the TrainingInstances is considered a neighbour.
        /// </summary>
        /// <param name="testingInstance">The instance to be calculated alpha value on. </param>
        /// <returns>The value of alpha. </returns>
        public double GetAlphaValue(Instance testingInstance)
        {
            int homoCount = TrainingInstances.Count(i => i.LabelValue == testingInstance.LabelValue);

            return(GetNeighbors(testingInstance, homoCount - 1).Count(i => i.LabelValue == testingInstance.LabelValue) / (double)homoCount);
        }
Exemplo n.º 5
0
        public override Dictionary <string, double> GetProbDist(Instance testingInstance)
        {
            Dictionary <string, double> distStats = new Dictionary <string, double>();

            foreach (Instance neighborInstance in GetNeighbors(testingInstance, NeighboringMethod == NeighboringOption.SqrtNeighbors ? (int)Sqrt(TrainingInstances.Count) : TrainingInstances.Count - 1))
            {
                if (neighborInstance.LabelValue is null)
                {
                    throw new NullReferenceException("Unlabeled instance is used as training instance. ");
                }
                if (!distStats.ContainsKey(neighborInstance.LabelValue))
                {
                    distStats.Add(neighborInstance.LabelValue, 0);
                }
                distStats[neighborInstance.LabelValue] += EuclideanDistance(neighborInstance, testingInstance);
            }

            Dictionary <string, double> distStatsInverted = new Dictionary <string, double>();

            foreach (KeyValuePair <string, double> kvp in distStats)
            {
                distStatsInverted.Add(kvp.Key, 1.0 / kvp.Value * (NeighboringMethod == NeighboringOption.AllNeighborsWithReweighting ? TrainingInstances.Count(i => i.LabelValue == kvp.Key) / (double)TrainingInstances.Count : 1.0));
            }
            return(OrderedNormalized(distStatsInverted));
        }