-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Merged
michaelgsharp
merged 8 commits into
dotnet:master
from
michaelgsharp:datetime-transformer
Dec 17, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3dcf356
files and changes needed for the DateTimeTransformer
michaelgsharp 1bedb59
updates from PR comments
michaelgsharp bee2616
removed ability to drop columns from DateTimeTransformer
michaelgsharp 0d10ae7
changes from PR comments
michaelgsharp 6d5625b
updates from PR comments
michaelgsharp 02f808e
updates to fix build issues
michaelgsharp de2fc12
test for centos7
michaelgsharp a93e8bf
Fixed CentOS7 check
michaelgsharp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/Featurizers/DateTimeTransformer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?