private void CombineCore(ref VBuffer <Single> dst, VBuffer <Single>[] src, Single[] weights = null) { Host.AssertNonEmpty(src); Host.Assert(weights == null || Utils.Size(weights) == Utils.Size(src)); int count = Utils.Size(src); if (count == 0) { dst = new VBuffer <Single>(0, dst.Values, dst.Indices); return; } int len = GetClassCount(src); var values = dst.Values; if (Utils.Size(values) < len) { values = new Single[len]; } else { Array.Clear(values, 0, len); } int voteCount = 0; for (int i = 0; i < count; i++) { int index = VectorUtils.ArgMax(ref src[i]); if (index >= 0) { values[index]++; voteCount++; } } // Normalize by dividing by the number of votes. for (int i = 0; i < len; i++) { values[i] /= voteCount; } // Set the output to values. dst = new VBuffer <Single>(len, values, dst.Indices); }
protected override Delegate GetPredictedLabelGetter(IRow output, out Delegate scoreGetter) { Host.AssertValue(output); Host.Assert(output.Schema == Bindings.RowMapper.Schema); Host.Assert(output.IsColumnActive(Bindings.ScoreColumnIndex)); ValueGetter <VBuffer <Float> > mapperScoreGetter = output.GetGetter <VBuffer <Float> >(Bindings.ScoreColumnIndex); long cachedPosition = -1; VBuffer <Float> score = default(VBuffer <Float>); int scoreLength = Bindings.PredColType.KeyCount; ValueGetter <uint> predFn = (ref uint dst) => { EnsureCachedPosition(ref cachedPosition, ref score, output, mapperScoreGetter); Host.Check(score.Length == scoreLength); int index = VectorUtils.ArgMax(ref score); if (index < 0) { dst = 0; } else { dst = (uint)index + 1; } }; ValueGetter <VBuffer <Float> > scoreFn = (ref VBuffer <Float> dst) => { EnsureCachedPosition(ref cachedPosition, ref score, output, mapperScoreGetter); Host.Check(score.Length == scoreLength); score.CopyTo(ref dst); }; scoreGetter = scoreFn; return(predFn); }
protected override Single GetDifference(ref VBuffer <Single> valueX, ref VBuffer <Single> valueY) { return((VectorUtils.ArgMax(ref valueX) != VectorUtils.ArgMax(ref valueY)) ? 1 : 0); }