Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            IPriorityQueue <string> q = new BinaryHeapPriorityQueue <string>();

            q.Add("bla", 2);
            q.Add("bla3", 2);
            logger.Info("size of q " + q.Count);
            Edu.Stanford.Nlp.Classify.PRCurve pr = new Edu.Stanford.Nlp.Classify.PRCurve("c:/data0204/precsvm", true);
            logger.Info("acc " + pr.Accuracy() + " opt " + pr.OptimalAccuracy() + " cwa " + pr.Cwa() + " optcwa " + pr.OptimalCwa());
            for (int r = 1; r <= pr.NumSamples(); r++)
            {
                logger.Info("optimal precision at recall " + r + " " + pr.Precision(r));
                logger.Info("model precision at recall " + r + " " + pr.LogPrecision(r));
            }
        }
        // static method only
        public static IList <V> GetShortestPath <V, E>(IGraph <V, E> graph, V node1, V node2, bool directionSensitive)
        {
            if (node1.Equals(node2))
            {
                return(Java.Util.Collections.SingletonList(node2));
            }
            ICollection <V>             visited        = Generics.NewHashSet();
            IDictionary <V, V>          previous       = Generics.NewHashMap();
            BinaryHeapPriorityQueue <V> unsettledNodes = new BinaryHeapPriorityQueue <V>();

            unsettledNodes.Add(node1, 0);
            while (unsettledNodes.Count > 0)
            {
                double distance = unsettledNodes.GetPriority();
                V      u        = unsettledNodes.RemoveFirst();
                visited.Add(u);
                if (u.Equals(node2))
                {
                    break;
                }
                unsettledNodes.Remove(u);
                ICollection <V> candidates = ((directionSensitive) ? graph.GetChildren(u) : graph.GetNeighbors(u));
                foreach (V candidate in candidates)
                {
                    double alt = distance - 1;
                    // nodes not already present will have a priority of -inf
                    if (alt > unsettledNodes.GetPriority(candidate) && !visited.Contains(candidate))
                    {
                        unsettledNodes.RelaxPriority(candidate, alt);
                        previous[candidate] = u;
                    }
                }
            }
            if (!previous.Contains(node2))
            {
                return(null);
            }
            List <V> path = new List <V>();

            path.Add(node2);
            V n = node2;

            while (previous.Contains(n))
            {
                path.Add(previous[n]);
                n = previous[n];
            }
            Java.Util.Collections.Reverse(path);
            return(path);
        }
        public virtual void InitMC <F>(IProbabilisticClassifier <L, F> classifier, GeneralDataset <L, F> data)
        {
            //if (!(gData instanceof Dataset)) {
            //  throw new UnsupportedOperationException("Can only handle Datasets, not "+gData.getClass().getName());
            //}
            //
            //Dataset data = (Dataset)gData;
            IPriorityQueue <Pair <int, Pair <double, bool> > > q = new BinaryHeapPriorityQueue <Pair <int, Pair <double, bool> > >();

            total         = 0;
            correct       = 0;
            logLikelihood = 0.0;
            for (int i = 0; i < data.Size(); i++)
            {
                IDatum <L, F> d            = data.GetRVFDatum(i);
                ICounter <L>  scores       = classifier.LogProbabilityOf(d);
                L             guess        = Counters.Argmax(scores);
                L             correctLab   = d.Label();
                double        guessScore   = scores.GetCount(guess);
                double        correctScore = scores.GetCount(correctLab);
                int           guessInd     = data.LabelIndex().IndexOf(guess);
                int           correctInd   = data.LabelIndex().IndexOf(correctLab);
                total++;
                if (guessInd == correctInd)
                {
                    correct++;
                }
                logLikelihood += correctScore;
                q.Add(new Pair <int, Pair <double, bool> >(int.Parse(i), new Pair <double, bool>(guessScore, bool.ValueOf(guessInd == correctInd))), -guessScore);
            }
            accuracy = (double)correct / (double)total;
            IList <Pair <int, Pair <double, bool> > > sorted = q.ToSortedList();

            scores    = new double[sorted.Count];
            isCorrect = new bool[sorted.Count];
            for (int i_1 = 0; i_1 < sorted.Count; i_1++)
            {
                Pair <double, bool> next = sorted[i_1].Second();
                scores[i_1]    = next.First();
                isCorrect[i_1] = next.Second();
            }
        }
Ejemplo n.º 4
0
        public virtual void Init(IList <Pair <double, int> > dataScores)
        {
            IPriorityQueue <Pair <int, Pair <double, int> > > q = new BinaryHeapPriorityQueue <Pair <int, Pair <double, int> > >();

            for (int i = 0; i < dataScores.Count; i++)
            {
                q.Add(new Pair <int, Pair <double, int> >(int.Parse(i), dataScores[i]), -dataScores[i].First());
            }
            IList <Pair <int, Pair <double, int> > > sorted = q.ToSortedList();

            scores  = new double[sorted.Count];
            classes = new int[sorted.Count];
            logger.Info("incoming size " + dataScores.Count + " resulting " + sorted.Count);
            for (int i_1 = 0; i_1 < sorted.Count; i_1++)
            {
                Pair <double, int> next = sorted[i_1].Second();
                scores[i_1]  = next.First();
                classes[i_1] = next.Second();
            }
            Init();
        }
        public static /*<V, E>*/ List <V> GetShortestPath <V, E>(IGraph <V, E> graph, V node1, V node2, bool directionSensitive)
        {
            if (node1.Equals(node2))
            {
                //return Collections.singletonList(node2);
                return(new List <V>()
                {
                    node2
                });
            }

            Set <V> visited        = new Util.HashSet <V>();
            var     previous       = new Dictionary <V, V>();
            var     unsettledNodes = new BinaryHeapPriorityQueue <V>();

            unsettledNodes.Add(node1, 0);

            while (unsettledNodes.Size() > 0)
            {
                var distance = unsettledNodes.GetPriority();
                var u        = unsettledNodes.RemoveFirst();
                visited.Add(u);

                if (u.Equals(node2))
                {
                    break;
                }

                unsettledNodes.Remove(u);

                var candidates = ((directionSensitive) ? graph.GetChildren(u) : new ReadOnlyCollection <V>(graph.GetNeighbors(u)));
                foreach (var candidate in candidates)
                {
                    var alt = distance - 1;
                    // nodes not already present will have a priority of -inf
                    if (alt > unsettledNodes.GetPriority(candidate) && !visited.Contains(candidate))
                    {
                        unsettledNodes.RelaxPriority(candidate, alt);
                        previous[candidate] = u;
                    }
                }
            }

            if (!previous.ContainsKey(node2))
            {
                return(null);
            }
            var path = new List <V>
            {
                node2
            };
            var n = node2;

            while (previous.ContainsKey(n))
            {
                path.Add(previous[n]);
                n = previous[n];
            }
            path.Reverse();
            return(path);
        }