public void ValueMapOneValueTest() { var data = new[] { new TestClass() { A = "bar", B = "test", C = "foo" } }; var dataView = ML.Data.LoadFromEnumerable(data); var keys = new List <string>() { "foo", "bar", "test", "wahoo" }; var values = new List <int>() { 1, 2, 3, 4 }; var lookupMap = DataViewHelper.CreateDataView(Env, keys, values, ValueMappingTransformer.DefaultKeyColumnName, ValueMappingTransformer.DefaultValueColumnName, false); var estimator = new ValueMappingEstimator <string, int>(Env, lookupMap, lookupMap.Schema[ValueMappingTransformer.DefaultKeyColumnName], lookupMap.Schema[ValueMappingTransformer.DefaultValueColumnName], new[] { ("D", "A"), ("E", "B"), ("F", "C") });
/// <summary> /// Create a <see cref="ValueMappingEstimator"/>, which converts value types into keys, loading the keys to use from <paramref name="keyValuePairs"/>. /// </summary> /// <typeparam name="TInputType">The key type.</typeparam> /// <typeparam name="TOutputType">The value type.</typeparam> /// <param name="catalog">The conversion transform's catalog</param> /// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>. /// The output data types can be primitives or vectors of numeric, text, boolean, <see cref="System.DateTime"/>, <see cref="System.DateTimeOffset"/> or <see cref="DataViewRowId"/> types.</param> /// <param name="keyValuePairs">Specifies the mapping that will be performed. The keys will be mapped to the values as specified in the <paramref name="keyValuePairs"/>.</param> /// <param name="inputColumnName">Name of the column to transform. /// If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source. /// The input data types can be primitives or vectors of numeric, text, boolean, <see cref="System.DateTime"/>, <see cref="System.DateTimeOffset"/> or <see cref="DataViewRowId"/> types. /// </param> /// <param name="treatValuesAsKeyType">Whether to treat the values as a key.</param> /// <returns>An instance of the <see cref="ValueMappingEstimator"/></returns> /// <example> /// <format type="text/markdown"> /// <![CDATA[ /// [!code-csharp[MapValue](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Conversion/MapValue.cs)] /// ]]></format> /// </example> public static ValueMappingEstimator <TInputType, TOutputType> MapValue <TInputType, TOutputType>( this TransformsCatalog.ConversionTransforms catalog, string outputColumnName, IEnumerable <KeyValuePair <TInputType, TOutputType> > keyValuePairs, string inputColumnName = null, bool treatValuesAsKeyType = false) { var keys = keyValuePairs.Select(pair => pair.Key); var values = keyValuePairs.Select(pair => pair.Value); var lookupMap = DataViewHelper.CreateDataView(catalog.GetEnvironment(), keys, values, ValueMappingTransformer.DefaultKeyColumnName, ValueMappingTransformer.DefaultValueColumnName, treatValuesAsKeyType); return(new ValueMappingEstimator <TInputType, TOutputType>(catalog.GetEnvironment(), lookupMap, lookupMap.Schema[ValueMappingTransformer.DefaultKeyColumnName], lookupMap.Schema[ValueMappingTransformer.DefaultValueColumnName], new[] { (outputColumnName, inputColumnName ?? outputColumnName) }));