// ---
// jupyter:
//   jupytext:
//     text_representation:
//       extension: .fs
//       format_name: light
//       format_version: '1.5'
//       jupytext_version: 1.14.4
//   kernelspec:
//     display_name: .NET (F#)
//     language: F#
//     name: .net-fsharp
// ---

#r "nuget: Microsoft.ML"
open Microsoft.ML
open System

// Create classes to map data to:

// +
[<CLIMutable>]
type SourceData =
    {
        Date: DateTime
    }

[<CLIMutable>]
type MappedData =
    {
        mutable SourceDate: DateTime

        mutable Month: int

        mutable IsWeekend: bool
    }
// -

// Instantiate a new MLContext and create some sample data:

// +
let context = new MLContext()

let data =
    [
        { Date = DateTime.Parse("2019-08-20") }
        { Date = DateTime.Parse("2019-07-06") }
        { Date = DateTime.Parse("2019-06-16") }
        { Date = DateTime.Parse("2019-05-14") }
    ]
data
// -

// Create a new DataView from the sample data:

let dataView = context.Data.LoadFromEnumerable<SourceData>(data)

// Here is where we define a CustomMapping to map from SourceData to MappedData:

let mapper source mapped =
    mapped.SourceDate <- source.Date
    mapped.Month <- source.Date.Month
    mapped.IsWeekend <- source.Date.DayOfWeek = DayOfWeek.Saturday || source.Date.DayOfWeek = DayOfWeek.Sunday
let mapperAction = new Action<SourceData, MappedData>(mapper)

// Fit the encoder to our data:

let encoder = context.Transforms.CustomMapping(mapperAction, "MapDate")

// Transform our data using the fitted transformer:

let transformer = encoder.Fit(dataView)

// Transform our data using the fitted transformer:

let transformedDataView = transformer.Transform(dataView)

// Print our transformed data:

context.Data.CreateEnumerable<MappedData>(transformedDataView, reuseRowObject = true)



StarcoderdataJupyterScriptsDedupFiltered