コード例 #1
0
        public ClassifierResult <LblT> Classify(ExT example)
        {
            Utils.ThrowException((m_examples == null || m_similarity == null) ? new InvalidOperationException() : null);
            Utils.ThrowException(example == null ? new ArgumentNullException("example") : null);
            ArrayList <KeyDat <double, LabeledExample <LblT, ExT> > > tmp = new ArrayList <KeyDat <double, LabeledExample <LblT, ExT> > >(m_examples.Count);

            foreach (LabeledExample <LblT, ExT> labeled_example in m_examples)
            {
                double sim = m_similarity.GetSimilarity(example, labeled_example.Example);
                tmp.Add(new KeyDat <double, LabeledExample <LblT, ExT> >(sim, labeled_example));
            }
            tmp.Sort(new DescSort <KeyDat <double, LabeledExample <LblT, ExT> > >());
            Dictionary <LblT, double> voting = new Dictionary <LblT, double>(m_lbl_cmp);
            int    n = Math.Min(m_k, tmp.Count);
            double value;

            if (m_soft_voting) // "soft" voting
            {
                for (int i = 0; i < n; i++)
                {
                    KeyDat <double, LabeledExample <LblT, ExT> > item = tmp[i];
                    if (!voting.TryGetValue(item.Dat.Label, out value))
                    {
                        voting.Add(item.Dat.Label, item.Key);
                    }
                    else
                    {
                        voting[item.Dat.Label] = value + item.Key;
                    }
                }
            }
            else // normal voting
            {
                for (int i = 0; i < n; i++)
                {
                    KeyDat <double, LabeledExample <LblT, ExT> > item = tmp[i];
                    if (!voting.TryGetValue(item.Dat.Label, out value))
                    {
                        voting.Add(item.Dat.Label, 1);
                    }
                    else
                    {
                        voting[item.Dat.Label] = value + 1.0;
                    }
                }
            }
            ClassifierResult <LblT> classifier_result = new ClassifierResult <LblT>();

            foreach (KeyValuePair <LblT, double> item in voting)
            {
                classifier_result.Items.Add(new KeyDat <double, LblT>(item.Value, item.Key));
            }
            classifier_result.Items.Sort(new DescSort <KeyDat <double, LblT> >());
            return(classifier_result);
        }
コード例 #2
0
ファイル: ModelUtils.cs プロジェクト: mgrcar/OPA
        public static ClassifierResult <LblT> ClassifyGroup <LblT, ExT>(IEnumerable <ExT> examples, IModel <LblT, ExT> model, GroupClassifyMethod method, IEqualityComparer <LblT> lbl_cmp)
        {
            Dictionary <LblT, double> tmp = new Dictionary <LblT, double>(lbl_cmp);

            foreach (ExT example in examples)
            {
                ClassifierResult <LblT> result = model.Classify(example); // throws InvalidOperationException, ArgumentNullException
                foreach (KeyDat <double, LblT> lbl_info in result)
                {
                    if (method == GroupClassifyMethod.Vote)
                    {
                        if (!tmp.ContainsKey(lbl_info.Dat))
                        {
                            tmp.Add(lbl_info.Dat, 1);
                        }
                        else
                        {
                            tmp[lbl_info.Dat]++;
                        }
                        break;
                    }
                    else
                    {
                        if (!tmp.ContainsKey(lbl_info.Dat))
                        {
                            tmp.Add(lbl_info.Dat, lbl_info.Key);
                        }
                        else
                        {
                            switch (method)
                            {
                            case GroupClassifyMethod.Max:
                                tmp[lbl_info.Dat] = Math.Max(lbl_info.Key, tmp[lbl_info.Dat]);
                                break;

                            case GroupClassifyMethod.Sum:
                                tmp[lbl_info.Dat] += lbl_info.Key;
                                break;
                            }
                        }
                    }
                }
            }
            ClassifierResult <LblT> aggr_result = new ClassifierResult <LblT>();

            foreach (KeyValuePair <LblT, double> item in tmp)
            {
                aggr_result.Items.Add(new KeyDat <double, LblT>(item.Value, item.Key));
            }
            aggr_result.Items.Sort(new DescSort <KeyDat <double, LblT> >());
            return(aggr_result);
        }
コード例 #3
0
        public ClassifierResult <LblT> Classify(SparseVector <double> .ReadOnly example)
        {
            Utils.ThrowException(m_centroids == null ? new InvalidOperationException() : null);
            Utils.ThrowException(example == null ? new ArgumentNullException("example") : null);
            ClassifierResult <LblT> result = new ClassifierResult <LblT>();

            foreach (Pair <LblT, SparseVector <double> .ReadOnly> labeled_centroid in m_centroids)
            {
                double sim = m_similarity.GetSimilarity(labeled_centroid.Second, example);
                result.Items.Add(new KeyDat <double, LblT>(sim, labeled_centroid.First));
            }
            result.Items.Sort(new DescSort <KeyDat <double, LblT> >());
            return(result);
        }