Ejemplo n.º 1
0
        public static IPredictorProducing <Float> Create(IHostEnvironment env, ModelLoadContext ctx)
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(ctx, nameof(ctx));
            ctx.CheckAtModel(GetVersionInfo());
            var         predictor = new LinearBinaryPredictor(env, ctx);
            ICalibrator calibrator;

            ctx.LoadModelOrNull <ICalibrator, SignatureLoadModel>(env, out calibrator, @"Calibrator");
            if (calibrator == null)
            {
                return(predictor);
            }
            if (calibrator is IParameterMixer)
            {
                return(new ParameterMixingCalibratedPredictor(env, predictor, calibrator));
            }
            return(new SchemaBindableCalibratedPredictor(env, predictor, calibrator));
        }
Ejemplo n.º 2
0
        public void SaveText(TextWriter writer, LinearBinaryPredictor parent, RoleMappedSchema schema, int paramCountCap)
        {
            Contracts.AssertValue(_env);
            _env.CheckValue(writer, nameof(writer));
            _env.AssertValueOrNull(parent);
            _env.AssertValueOrNull(schema);
            writer.WriteLine();
            writer.WriteLine("*** MODEL STATISTICS SUMMARY ***   ");
            writer.WriteLine("Count of training examples:\t{0}", _trainingExampleCount);
            writer.WriteLine("Residual Deviance:         \t{0}", _deviance);
            writer.WriteLine("Null Deviance:             \t{0}", _nullDeviance);
            writer.WriteLine("AIC:                       \t{0}", 2 * _paramCount + _deviance);

            if (parent == null)
            {
                return;
            }

            var coeffStats = GetCoefficientStatistics(parent, schema, paramCountCap);

            if (coeffStats == null)
            {
                return;
            }

            writer.WriteLine();
            writer.WriteLine("Coefficients statistics:");
            writer.WriteLine("Coefficient    \tEstimate\tStd. Error\tz value  \tPr(>|z|)");

            foreach (var coeffStat in coeffStats)
            {
                writer.WriteLine("{0,-15}\t{1,-10:G7}\t{2,-10:G7}\t{3,-10:G7}\t{4}",
                                 coeffStat.Name,
                                 coeffStat.Estimate,
                                 coeffStat.StandardError,
                                 coeffStat.ZScore,
                                 DecorateProbabilityString(coeffStat.PValue));
            }

            writer.WriteLine("---");
            writer.WriteLine("Significance codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1");
        }
Ejemplo n.º 3
0
        public void AddStatsColumns(List <IColumn> list, LinearBinaryPredictor parent, RoleMappedSchema schema, ref VBuffer <ReadOnlyMemory <char> > names)
        {
            _env.AssertValue(list);
            _env.AssertValueOrNull(parent);
            _env.AssertValue(schema);

            long count = _trainingExampleCount;

            list.Add(RowColumnUtils.GetColumn("Count of training examples", NumberType.I8, ref count));
            var dev = _deviance;

            list.Add(RowColumnUtils.GetColumn("Residual Deviance", NumberType.R4, ref dev));
            var nullDev = _nullDeviance;

            list.Add(RowColumnUtils.GetColumn("Null Deviance", NumberType.R4, ref nullDev));
            var aic = 2 * _paramCount + _deviance;

            list.Add(RowColumnUtils.GetColumn("AIC", NumberType.R4, ref aic));

            if (parent == null)
            {
                return;
            }

            Single biasStdErr;
            Single biasZScore;
            Single biasPValue;

            if (!TryGetBiasStatistics(parent.Statistics, parent.Bias, out biasStdErr, out biasZScore, out biasPValue))
            {
                return;
            }

            var biasEstimate = parent.Bias;

            list.Add(RowColumnUtils.GetColumn("BiasEstimate", NumberType.R4, ref biasEstimate));
            list.Add(RowColumnUtils.GetColumn("BiasStandardError", NumberType.R4, ref biasStdErr));
            list.Add(RowColumnUtils.GetColumn("BiasZScore", NumberType.R4, ref biasZScore));
            list.Add(RowColumnUtils.GetColumn("BiasPValue", NumberType.R4, ref biasPValue));

            var weights = default(VBuffer <Single>);

            parent.GetFeatureWeights(ref weights);
            var estimate = default(VBuffer <Single>);
            var stdErr   = default(VBuffer <Single>);
            var zScore   = default(VBuffer <Single>);
            var pValue   = default(VBuffer <Single>);
            ValueGetter <VBuffer <ReadOnlyMemory <char> > > getSlotNames;

            GetUnorderedCoefficientStatistics(parent.Statistics, ref weights, ref names, ref estimate, ref stdErr, ref zScore, ref pValue, out getSlotNames);

            var slotNamesCol = RowColumnUtils.GetColumn(MetadataUtils.Kinds.SlotNames,
                                                        new VectorType(TextType.Instance, stdErr.Length), getSlotNames);
            var slotNamesRow = RowColumnUtils.GetRow(null, slotNamesCol);
            var colType      = new VectorType(NumberType.R4, stdErr.Length);

            list.Add(RowColumnUtils.GetColumn("Estimate", colType, ref estimate, slotNamesRow));
            list.Add(RowColumnUtils.GetColumn("StandardError", colType, ref stdErr, slotNamesRow));
            list.Add(RowColumnUtils.GetColumn("ZScore", colType, ref zScore, slotNamesRow));
            list.Add(RowColumnUtils.GetColumn("PValue", colType, ref pValue, slotNamesRow));
        }
Ejemplo n.º 4
0
        private IEnumerable <CoefficientStatistics> GetUnorderedCoefficientStatistics(LinearBinaryPredictor parent, RoleMappedSchema schema)
        {
            Contracts.AssertValue(_env);
            _env.CheckValue(parent, nameof(parent));

            if (!_coeffStdError.HasValue)
            {
                yield break;
            }

            var weights = parent.Weights2 as IReadOnlyList <Single>;

            _env.Assert(_paramCount == 1 || weights != null);
            _env.Assert(_coeffStdError.Value.Length == weights.Count + 1);

            var names = default(VBuffer <ReadOnlyMemory <char> >);

            MetadataUtils.GetSlotNames(schema, RoleMappedSchema.ColumnRole.Feature, weights.Count, ref names);

            Single[]     stdErrorValues = _coeffStdError.Value.Values;
            const Double sqrt2          = 1.41421356237; // Math.Sqrt(2);

            bool denseStdError = _coeffStdError.Value.IsDense;

            int[]    stdErrorIndices = _coeffStdError.Value.Indices;
            Single[] zScores         = new Single[_paramCount - 1];
            for (int i = 1; i < _paramCount; i++)
            {
                int wi = denseStdError ? i - 1 : stdErrorIndices[i] - 1;
                _env.Assert(0 <= wi && wi < weights.Count);
                var name = names.GetItemOrDefault(wi).ToString();
                if (string.IsNullOrEmpty(name))
                {
                    name = $"f{wi}";
                }
                var weight   = weights[wi];
                var stdError = stdErrorValues[i];
                var zScore   = zScores[i - 1] = weight / stdError;
                var pValue   = 1 - (Single)ProbabilityFunctions.Erf(Math.Abs(zScore / sqrt2));
                yield return(new CoefficientStatistics(name, weight, stdError, zScore, pValue));
            }
        }