Skip to content
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

Refactored scenario tests (issue #32) #52

Merged
merged 5 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Models;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.TestFramework;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.ML.Scenarios
{
public partial class Top5Scenarios : BaseTestClass
public partial class ScenariosTests : BaseTestClass
{
/*
A real-estate firm Contoso wants to add a house price prediction to their ASP.NET/Xamarin application.
Expand Down Expand Up @@ -49,6 +52,64 @@ public async void PredictHousePriceModelTest()
Assert.InRange(prediction.Price, 260_000, 330_000);
}

[Fact(Skip = "Missing data set. See https://github.com/dotnet/machinelearning/issues/3")]
public void TrainAndPredictHousePriceModelTest()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KrzysztofCwalina specifically asked that we have 1 scenario test per file. Would it be OK to keep the tests separate?

{
string dataPath = GetDataPath("kc_house_data.csv");

var pipeline = new LearningPipeline();

pipeline.Add(new TextLoader<HousePriceData>(dataPath, useHeader: true, separator: ","));

pipeline.Add(new ColumnConcatenator(outputColumn: "NumericalFeatures",
"SqftLiving", "SqftLot", "SqftAbove", "SqftBasement", "Lat", "Long", "SqftLiving15", "SqftLot15"));

pipeline.Add(new ColumnConcatenator(outputColumn: "CategoryFeatures",
"Bedrooms", "Bathrooms", "Floors", "Waterfront", "View", "Condition", "Grade", "YearBuilt", "YearRenovated", "Zipcode"));

pipeline.Add(new CategoricalOneHotVectorizer("CategoryFeatures"));
pipeline.Add(new ColumnConcatenator(outputColumn: "Features",
"NumericalFeatures", "CategoryFeatures"));
pipeline.Add(new StochasticDualCoordinateAscentRegressor());

PredictionModel<HousePriceData, HousePricePrediction> model = pipeline.Train<HousePriceData, HousePricePrediction>();

HousePricePrediction prediction = model.Predict(new HousePriceData()
{
Bedrooms = 3,
Bathrooms = 2,
SqftLiving = 1710,
SqftLot = 4697,
Floors = 1.5f,
Waterfront = 0,
View = 0,
Condition = 5,
Grade = 6,
SqftAbove = 1710,
SqftBasement = 0,
YearBuilt = 1941,
YearRenovated = 0,
Zipcode = 98002,
Lat = 47.3048f,
Long = -122.218f,
SqftLiving15 = 1030,
SqftLot15 = 4705
});

Assert.InRange(prediction.Price, 260_000, 330_000);

string testDataPath = GetDataPath("kc_house_test.csv");
var testData = new TextLoader<HousePriceData>(testDataPath, useHeader: true, separator: ",");

var evaluator = new RegressionEvaluator();
RegressionMetrics metrics = evaluator.Evaluate(model, testData);
Assert.InRange(metrics.L1, 85_000, 89_000);
Assert.InRange(metrics.L2, 17_000_000_000, 19_000_000_000);
Assert.InRange(metrics.Rms, 130_500, 135_000);
Assert.InRange(metrics.LossFn, 17_000_000_000, 19_000_000_000);
Assert.Equal(.8, metrics.RSquared, 1);
}

public class HousePriceData
{
[Column(ordinal: "0")]
Expand Down Expand Up @@ -121,7 +182,7 @@ public class HousePricePrediction
public float Price;
}

public Top5Scenarios(ITestOutputHelper output) : base(output)
public ScenariosTests(ITestOutputHelper output) : base(output)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Microsoft.ML.Scenarios
{
public partial class Top5Scenarios
public partial class ScenariosTests
{
[Fact]
public void TrainAndPredictIrisModelTest()
Expand Down
73 changes: 0 additions & 73 deletions test/Microsoft.ML.Tests/Scenarios/Scenario_TrainPredictionModel.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Microsoft.ML.Scenarios
{
public partial class Top5Scenarios
public partial class ScenariosTests
{
public const string SentimentDataPath = "wikipedia-detox-250-line-data.tsv";
public const string SentimentTestPath = "wikipedia-detox-250-line-test.tsv";
Expand Down