Skip to content

Instantly share code, notes, and snippets.

@mikekistler
Last active May 10, 2023 04:18
Show Gist options
  • Save mikekistler/d4a3510fde9463517b9f9ae84770bb9b to your computer and use it in GitHub Desktop.
Save mikekistler/d4a3510fde9463517b9f9ae84770bb9b to your computer and use it in GitHub Desktop.
Sample code to train a Multivariate Anomaly Detector model
string Train(AnomalyDetectorClient client, string dataSource, DateTimeOffset startTime, DateTimeOffset endTime, int maxTryout = 500)
{
var modelInfo = new ModelInfo(
dataSource,
startTime,
endTime);
AnomalyDetectionModel model = client.TrainMultivariateModel(modelInfo);
var modelId = model.ModelId;
// Wait until the model is ready. It usually takes several minutes
var modelStatus = model.ModelInfo.Status;
int tryoutCount = 0;
while (tryoutCount < maxTryout && modelStatus != ModelStatus.Ready && modelStatus != ModelStatus.Failed)
{
Thread.Sleep(5000);
model = client.GetMultivariateModel(modelId);
modelStatus = model.ModelInfo.Status;
tryoutCount++;
}
if (model.ModelInfo.Status == ModelStatus.Failed) {
Console.WriteLine("Model training failed: {0} - {1}",
model.ModelInfo.Errors[0].Code, model.ModelInfo.Errors[0].Message);
return null;
}
return modelId;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment