Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2k
DateTimeTransformer featurizer#4521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3dcf3561bedb59bee26160d10ae76d5625b02f808ede2fc12a93e8bfFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.ML; | ||
| using Microsoft.ML.Data; | ||
| using Microsoft.ML.Featurizers; | ||
| namespace Samples.Dynamic | ||
| { | ||
| public static class DateTimeTransformer | ||
| { | ||
| private class DateTimeInput | ||
| { | ||
| public long Date; | ||
| } | ||
| public static void Example() | ||
| { | ||
| // Create a new ML context, for ML.NET operations. It can be used for | ||
| // exception tracking and logging, as well as the source of randomness. | ||
| var mlContext = new MLContext(); | ||
| // Create a small dataset as an IEnumerable. | ||
| // Future Date - 2025 June 30 | ||
| var samples = new[] { new DateTimeInput() { Date = 1751241600 } }; | ||
| // Convert training data to IDataView. | ||
| var dataview = mlContext.Data.LoadFromEnumerable(samples); | ||
| // A pipeline for splitting the time features into individual columns | ||
| var pipeline = mlContext.Transforms.FeaturizeDateTime("Date", "DTC"); | ||
| // The transformed data. | ||
| var transformedData = pipeline.Fit(dataview).Transform(dataview); | ||
| // Now let's take a look at what this did. We should have created 21 more columns with all the | ||
| // DateTime information split into its own columns | ||
| var featuresColumn = mlContext.Data.CreateEnumerable<TransformedData>( | ||
| transformedData, reuseRowObject: false); | ||
| // And we can write out a few rows | ||
| Console.WriteLine($"Features column obtained post-transformation."); | ||
| foreach (var featureRow in featuresColumn) | ||
| Console.WriteLine(featureRow.Date + ", " + featureRow.DTCYear + ", " + featureRow.DTCMonth + ", " + | ||
| featureRow.DTCDay + ", " + featureRow.DTCHour + ", " + featureRow.DTCMinute + ", " + | ||
| featureRow.DTCSecond + ", " + featureRow.DTCAmPm + ", " + featureRow.DTCHour12 + ", " + | ||
| featureRow.DTCDayOfWeek + ", " + featureRow.DTCDayOfQuarter + ", " + featureRow.DTCDayOfYear + | ||
| ", " + featureRow.DTCWeekOfMonth + ", " + featureRow.DTCQuarterOfYear + ", " + featureRow.DTCHalfOfYear + | ||
| ", " + featureRow.DTCWeekIso + ", " + featureRow.DTCYearIso + ", " + featureRow.DTCMonthLabel + ", " + | ||
| featureRow.DTCAmPmLabel + ", " + featureRow.DTCDayOfWeekLabel + ", " + featureRow.DTCHolidayName + ", " + | ||
| featureRow.DTCIsPaidTimeOff); | ||
| // Expected output: | ||
| // Features columns obtained post-transformation. | ||
| // 1751241600, 2025, 6, 30, 0, 0, 0, 0, 0, 1, 91, 180, 4, 2, 1, 27, 2025, June, am, Monday, , 0 | ||
| } | ||
| // These columns start with DTC because that is the prefix we picked | ||
| private sealed class TransformedData | ||
| { | ||
| public long Date { get; set; } | ||
| public int DTCYear { get; set; } | ||
| public byte DTCMonth { get; set; } | ||
| public byte DTCDay { get; set; } | ||
| public byte DTCHour { get; set; } | ||
| public byte DTCMinute { get; set; } | ||
| public byte DTCSecond { get; set; } | ||
| public byte DTCAmPm { get; set; } | ||
| public byte DTCHour12 { get; set; } | ||
| public byte DTCDayOfWeek { get; set; } | ||
| public byte DTCDayOfQuarter { get; set; } | ||
| public ushort DTCDayOfYear { get; set; } | ||
| public ushort DTCWeekOfMonth { get; set; } | ||
| public byte DTCQuarterOfYear { get; set; } | ||
| public byte DTCHalfOfYear { get; set; } | ||
| public byte DTCWeekIso { get; set; } | ||
| public int DTCYearIso { get; set; } | ||
| public string DTCMonthLabel { get; set; } | ||
| public string DTCAmPmLabel { get; set; } | ||
| public string DTCDayOfWeekLabel { get; set; } | ||
| public string DTCHolidayName { get; set; } | ||
| public byte DTCIsPaidTimeOff { get; set; } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Runtime.InteropServices; | ||
| using System.Security; | ||
| using System.Text; | ||
| @@ -219,5 +220,31 @@ internal static TypeId GetNativeTypeIdFromType(this Type type) | ||
| throw new InvalidOperationException($"Unsupported type {type}"); | ||
| } | ||
| // The Native Featurizers do not currently support CentOS7, this method checks the OS and returns true if it is CentOS7. | ||
| internal static bool OsIsCentOS7() | ||
| { | ||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
| { | ||
| using (Process process = new Process()) | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A more performant way of doing this check would be to read from the file directly. Starting a new process just to get this info is too heavy-weight IMO. | ||
| { | ||
| process.StartInfo.FileName = "/bin/bash"; | ||
| process.StartInfo.Arguments = "-c \"cat /etc/*-release\""; | ||
| process.StartInfo.UseShellExecute = false; | ||
| process.StartInfo.RedirectStandardOutput = true; | ||
| process.StartInfo.CreateNoWindow = true; | ||
| process.Start(); | ||
| string distro = process.StandardOutput.ReadToEnd().Trim(); | ||
| process.WaitForExit(); | ||
| if (distro.Contains("CentOS Linux 7")) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about RedHat 7?