/// <summary> /// print the linear model as code /// </summary> public static void SaveAsCode(TextWriter writer, ref VBuffer <Float> weights, Float bias, RoleMappedSchema schema, string codeVariable = "output") { Contracts.CheckValue(writer, nameof(writer)); Contracts.CheckValueOrNull(schema); var featureNames = default(VBuffer <ReadOnlyMemory <char> >); MetadataUtils.GetSlotNames(schema, RoleMappedSchema.ColumnRole.Feature, weights.Length, ref featureNames); int numNonZeroWeights = 0; writer.Write(codeVariable); writer.Write(" = "); VBufferUtils.ForEachDefined(ref weights, (idx, value) => { if (Math.Abs(value - 0) >= Epsilon) { if (numNonZeroWeights > 0) { writer.Write(" + "); } writer.Write(FloatUtils.ToRoundTripString(value)); writer.Write("*"); if (featureNames.Count > 0) { writer.Write(FeatureNameAsCode(featureNames.GetItemOrDefault(idx).ToString(), idx)); } else { writer.Write("f_" + idx); } numNonZeroWeights++; } }); if (numNonZeroWeights > 0) { writer.Write(" + "); } writer.Write(FloatUtils.ToRoundTripString(bias)); writer.WriteLine(";"); }