示例#1
0
 public override sealed void Load(BinarySerializer reader)
 {
     BinWidth      = reader.ReadDouble();
     TagDistrTable = new TagDistrTable <SentimentLabel>(reader);
     mBinModel     = reader.ReadObject <IModel <SentimentLabel, SparseVector <double> > >();
     IsTrained     = reader.ReadBool();
 }
        protected virtual Prediction <LblT> GetPrediction(double[] scores, TagDistrTable <LblT> tagDistrTable)
        {
            IEnumerable <KeyDat <double, LblT> > labelProbs = mTagDistrTable.GetDistrValues(scores)
                                                              .OrderByDescending(kv => Math.Abs(kv.Value.GetValueOrDefault()))
                                                              .Select(kv => new KeyDat <double, LblT>(kv.Value.GetValueOrDefault(), kv.Key));

            return(new Prediction <LblT>(labelProbs));
        }
 public void Load(BinarySerializer reader)
 {
     mTagDistrTable = new TagDistrTable <LblT>(reader);
     mInnerModels   = new IModel <LblT, ExT> [reader.ReadInt()];
     for (int i = 0; i < mInnerModels.Length; i++)
     {
         mInnerModels[i] = reader.ReadObject <IModel <LblT, ExT> >();
     }
     IsTrained = reader.ReadBool();
 }
        protected BinVotingClassifier(IModel <LblT, ExT>[] innerModels, double binWidth = 0.05, params LblT[] excludedTags)
        {
            Preconditions.CheckArgument(typeof(LblT).IsEnum);
            Preconditions.CheckNotNull(innerModels);
            Preconditions.CheckArgument(innerModels.Length > 0);
            Preconditions.CheckArgumentRange(binWidth > 0);

            mInnerModels   = innerModels;
            mTagDistrTable = new EnumTagDistrTable <LblT>(innerModels.Length, binWidth, -5, 5, excludedTags)
            {
                CalcDistrFunc = (tagCounts, values, tag) => CalcLabelScore(tagCounts, values, tag)
            };
        }
示例#5
0
        private void UpdateDistrTable()
        {
            if (mExampleScores == null || mBinWidth == 0)
            {
                mTagDistrTable = null;
                return;
            }

            mTagDistrTable = new EnumTagDistrTable <SentimentLabel>(2, mBinWidth, -5, 5, SentimentLabel.Exclude)
            {
                CalcDistrFunc = (tagCounts, values, tag) => ((double)tagCounts[tag] + 1) / (tagCounts.Values.Sum() + tagCounts.Count)
            };

            foreach (ExampleScore es in mExampleScores)
            {
                mTagDistrTable.AddCount(es.Label, es.PosScore, es.NegScore);
            }
            mTagDistrTable.Calculate();
        }
示例#6
0
        public override void Train(ILabeledExampleCollection <SentimentLabel, SparseVector <double> > dataset)
        {
            Preconditions.CheckNotNull(dataset);
            Preconditions.CheckArgumentRange(TagDistrTable == null || TagDistrTable.NumOfDimensions == 2);

            mBinModel = CreateModel();
            mBinModel.Train(new LabeledDataset <SentimentLabel, SparseVector <double> >(dataset.Where(le => le.Label != SentimentLabel.Neutral)));

            TagDistrTable = new EnumTagDistrTable <SentimentLabel>(1, BinWidth, -5, 5, SentimentLabel.Exclude)
            {
                CalcDistrFunc = (tagCounts, values, tag) => ((double)tagCounts[tag] + 1) / (tagCounts.Values.Sum() + tagCounts.Count)     // use Laplace formula
            };
            foreach (LabeledExample <SentimentLabel, SparseVector <double> > le in dataset)
            {
                Prediction <SentimentLabel> prediction = mBinModel.Predict(le.Example);
                TagDistrTable.AddCount(le.Label, prediction.BestClassLabel == SentimentLabel.Positive ? prediction.BestScore : -prediction.BestScore);
            }
            TagDistrTable.Calculate();

            IsTrained = true;
        }