Skip to content

Instantly share code, notes, and snippets.

@mdfarragher
Created November 7, 2019 14:23
Show Gist options
  • Save mdfarragher/fc00028624cbc2f968cc6fcc7c37ba1c to your computer and use it in GitHub Desktop.
Save mdfarragher/fc00028624cbc2f968cc6fcc7c37ba1c to your computer and use it in GitHub Desktop.
// step 4: one-hot encode the longitude
var pipeline3 = pipeline2.Append(context.Transforms.Categorical.OneHotEncoding(
inputColumnName: "BinnedLongitude",
outputColumnName: "EncodedLongitude"
))
// step 5: one-hot encode the latitude
.Append(context.Transforms.Categorical.OneHotEncoding(
inputColumnName: "BinnedLatitude",
outputColumnName: "EncodedLatitude"
))
// step 6: cross the two one-hot encoded columns
.Append(context.Transforms.CustomMapping<FromLocation, ToLocation>(
(input, output) => {
output.Location = new float[input.EncodedLongitude.Length * input.EncodedLatitude.Length];
var index = 0;
for (var i = 0; i < input.EncodedLongitude.Length; i++)
for (var j = 0; j < input.EncodedLatitude.Length; j++)
output.Location[index++] = input.EncodedLongitude[i] * input.EncodedLatitude[j];
},
contractName: "Location"
))
// step 7: remove all the columns we don't need anymore
.Append(context.Transforms.DropColumns(
"MedianHouseValue",
"Longitude",
"Latitude",
"BinnedLongitude",
"BinnedLatitude",
"EncodedLongitude",
"EncodedLatitude"
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment