-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Public API for Tree predictors #1837
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b1b1823
Internalize and explicitly implement ICanSAveInIniFormat, ICanSaveInS…
najeeb-kazmi 5bf7402
Internalize and explicitly implement IFeatureContributionMapper, IQua…
najeeb-kazmi 9fda3d6
Internalize and explicitly implement IValueMapperDist
najeeb-kazmi f4e35b2
Adding public constructors and sample
najeeb-kazmi a5aa3b9
nit
najeeb-kazmi 28f2fa8
Address comments
najeeb-kazmi 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
52 changes: 52 additions & 0 deletions
52
docs/samples/Microsoft.ML.Samples/Dynamic/FastTreeRegression.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,52 @@ | ||
using Microsoft.ML.Runtime.Api; | ||
using Microsoft.ML.Runtime.Data; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.ML.Samples.Dynamic | ||
{ | ||
public class FastTreeRegressionExample | ||
{ | ||
public static void FastTreeRegression() | ||
{ | ||
// 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 ml = new MLContext(); | ||
|
||
// Get a small dataset as an IEnumerable and convert it to an IDataView. | ||
var data = SamplesUtils.DatasetUtils.GetInfertData(); | ||
var trainData = ml.CreateStreamingDataView(data); | ||
|
||
// Preview of the data. | ||
// | ||
// Age Case Education Induced Parity PooledStratum RowNum ... | ||
// 26 1 0-5yrs 1 6 3 1 ... | ||
// 42 1 0-5yrs 1 1 1 2 ... | ||
// 39 1 0-5yrs 2 6 4 3 ... | ||
// 34 1 0-5yrs 2 4 2 4 ... | ||
// 35 1 6-11yrs 1 3 32 5 ... | ||
|
||
// A pipeline for concatenating the Parity and Induced columns together in the Features column. | ||
// We will train a FastTreeRegression model with 1 tree on these two columns to predict Age. | ||
string outputColumnName = "Features"; | ||
var pipeline = ml.Transforms.Concatenate(outputColumnName, new[] { "Parity", "Induced" }) | ||
.Append(ml.Regression.Trainers.FastTree(labelColumn: "Age", featureColumn: outputColumnName, numTrees: 1, numLeaves: 2, minDatapointsInLeaves: 1)); | ||
|
||
var model = pipeline.Fit(trainData); | ||
|
||
// Get the trained model parameters. | ||
var modelParams = model.LastTransformer.Model; | ||
|
||
// Let's see where an example with Parity = 1 and Induced = 1 would end up in the single trained tree. | ||
var testRow = new VBuffer<float>(2, new[] { 1.0f, 1.0f }); | ||
// Use the path object to pass to GetLeaf, which will populate path with the IDs of th nodes from root to leaf. | ||
List<int> path = default; | ||
// Get the ID of the leaf this example ends up in tree 0. | ||
var leafID = modelParams.GetLeaf(0, in testRow, ref path); | ||
// Get the leaf value for this leaf ID in tree 0. | ||
var leafValue = modelParams.GetLeafValue(0, leafID); | ||
Console.WriteLine("The leaf value in tree 0 is: " + leafValue); | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.