Beispiel #1
0
 public static Prediction <LblT> ClassifyGroup <LblT, ExT>(IEnumerable <ExT> examples, IModel <LblT, ExT> model, GroupClassifyMethod method)
 {
     return(ClassifyGroup <LblT, ExT>(examples, model, method, /*lblCmp=*/ null)); // throws InvalidOperationException, ArgumentNullException
 }
Beispiel #2
0
        public static Prediction <LblT> ClassifyGroup <LblT, ExT>(IEnumerable <ExT> examples, IModel <LblT, ExT> model, GroupClassifyMethod method, IEqualityComparer <LblT> lblCmp)
        {
            Utils.ThrowException(examples == null ? new ArgumentNullException("examples") : null);
            Utils.ThrowException(model == null ? new ArgumentNullException("model") : null);
            Dictionary <LblT, double> tmp = new Dictionary <LblT, double>(lblCmp);

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

                            case GroupClassifyMethod.Sum:
                                tmp[lblInfo.Dat] += lblInfo.Key;
                                break;
                            }
                        }
                    }
                }
            }
            Prediction <LblT> aggrResult = new Prediction <LblT>();

            foreach (KeyValuePair <LblT, double> item in tmp)
            {
                aggrResult.Inner.Add(new KeyDat <double, LblT>(item.Value, item.Key));
            }
            aggrResult.Inner.Sort(DescSort <KeyDat <double, LblT> > .Instance);
            return(aggrResult);
        }
Beispiel #3
0
        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);
        }