public void Test_Training() { double[][] clusterCentars = new double[3][]; clusterCentars[0] = new double[] { 5.0, 5.0 }; clusterCentars[1] = new double[] { 15.0, 15.0 }; clusterCentars[2] = new double[] { 30.0, 30.0 }; string[] attributes = new string[] { "Height", "Weight" }; var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5); int numAttributes = attributes.Length; // 2 in this demo (height,weight) int numClusters = 3; // vary this to experiment (must be between 2 and number data tuples) int maxCount = 300; // trial and error ClusteringSettings clusterSettings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: 1, Replace: true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(clusterSettings); //AnomalyDetectionAPI(clusterSettings), Constractor should not be null when run Training() AnomalyDetectionResponse response; for (int i = 0; i < 5; i++) { response = kmeanApi.Training(rawData, clusterCentars); Assert.True(response.Code == 0); } //Save Cluster and Instance in json response = kmeanApi.Save("interface.json"); Assert.True(response.Code == 0); }
public void Test() { AnomalyDetectionAPI AnoDet_Api = new AnomalyDetectionAPI(null, 0); ClusteringSettings Settings; SaveLoadSettings SaveObject; SaveLoadSettings LoadObject; AnomalyDetectionResponse ImportData; //TODO: Remove user specific paths from application. string FilePath = @"C:\Users\mhoshen\Desktop\DataSet\SampleDataSet.csv"; double[][] RawData = Helpers.cSVtoDoubleJaggedArray(FilePath); string SavePath = @"C:\Users\mhoshen\Desktop\DataSet" + "json"; ImportData = SaveLoadSettings.JSON_Settings(SavePath, out SaveObject, true); string LoadimpPath = @"C:\Users\mhoshen\Desktop\DataSet" + ".json"; ImportData = SaveLoadSettings.JSON_Settings(LoadimpPath, out LoadObject, true); int kmeansMaxIterations = 5; int numClusters = 2; int numOfAttributes = 2; if (LoadimpPath.Contains("DataSet")) { Settings = new ClusteringSettings(RawData, kmeansMaxIterations, numClusters, numOfAttributes, SaveObject, Replace: true); } else { Settings = new ClusteringSettings(RawData, kmeansMaxIterations, numClusters, numOfAttributes, SaveObject, 1, false, LoadObject, Replace: true); } ImportData = AnoDet_Api.ImportNewDataForClustering(Settings); }
public AnomalyDetectionResponse CheckSampleInCluster([FromBody] Sample sample) { int detectedCluster; double[] sampleResult; if (sample.YAxis.HasValue && !sample.ZAxis.HasValue) { sampleResult = new double[] { sample.XAxis, (double)sample.YAxis } } ; else if (sample.YAxis.HasValue && sample.ZAxis.HasValue) { sampleResult = new double[] { sample.XAxis, (double)sample.YAxis, (double)sample.ZAxis } } ; else { sampleResult = new double[] { sample.XAxis } }; try { AnomalyDetectionAPI kApi = new AnomalyDetectionAPI(); CheckingSampleSettings SampleSettings2 = new CheckingSampleSettings(sample.FilePath, sampleResult, sample.Tolerance); var response = kApi.CheckSample(SampleSettings2, out detectedCluster); return(response); }catch (Exception ex) { return(new AnomalyDetectionResponse(200, ex.Message)); } }
// checks and returns result of pattern testing (1 for matching, 0 otherwise) private static double[] PatternTesting(double[][] rawData, int numClusters, AnomalyDetectionAPI kmeanApi, int tolerance) { CheckingSampleSettings SampleSettings; int clusterIndex; bool fitsPattern; double[] result = new double[rawData.Length / numClusters]; for (int i = 0; i < rawData.Length; i = i + numClusters) { fitsPattern = true; // check each centroid of each function for (int j = 0; j < numClusters; j++) { // check centroids SampleSettings = new CheckingSampleSettings(null, rawData[i + j], tolerance: tolerance); kmeanApi.CheckSample(SampleSettings, out clusterIndex); // if a centroid doesn't belong to any cluster if (clusterIndex == -1) { fitsPattern = false; break; } } if (fitsPattern) { result[i / numClusters] = 1; } else { result[i / numClusters] = 0; } } // contains results of pattern testing (1 for matching, 0 otherwise) return(result); }
public void TestClusterCalcuation() { // // In test we know where are positions of centroids. // We will now create data around known centroids and let alorithm // find centroids. double[][] clusterCentars = new double[3][]; clusterCentars[0] = new double[] { 5.0, 5.0 }; clusterCentars[1] = new double[] { 15.0, 15.0 }; clusterCentars[2] = new double[] { 30.0, 30.0 }; string[] attributes = new string[] { "Height", "Weight" }; var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5); int numAttributes = attributes.Length; // 2 in this demo (height,weight) int numClusters = 3; // vary this to experiment (must be between 2 and number data tuples) int maxCount = 300; // trial and error SaveLoadSettings persistenceProviderSettings; var resp = SaveLoadSettings.JSON_Settings("model.json", out persistenceProviderSettings, false); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(rawData, numClusters); ClusteringSettings clusterSettings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, persistenceProviderSettings, KmeansAlgorithm: 1, Replace: true); AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(clusterSettings); Assert.True(response.Code == 0); int detectedCluster; double[] Sample = new double[] { 26, 28 }; CheckingSampleSettings SampleSettings = new CheckingSampleSettings(null, Sample, 3); response = kmeanApi.CheckSample(SampleSettings, out detectedCluster); Assert.True(response.Code == 0); Assert.True(detectedCluster == 0); double[] Sample2 = new double[] { 150, 16 }; CheckingSampleSettings SampleSettings2 = new CheckingSampleSettings(null, Sample2, 3); response = kmeanApi.CheckSample(SampleSettings2, out detectedCluster); Assert.True(response.Code == 1); Assert.True(detectedCluster == -1);// Out of all clusters. double[] Sample3 = new double[] { 16, 14 }; CheckingSampleSettings SampleSettings3 = new CheckingSampleSettings(null, Sample3, 3); response = kmeanApi.CheckSample(SampleSettings3, out detectedCluster); Assert.True(response.Code == 0); Assert.True(detectedCluster == 1); double[] Sample4 = new double[] { 6, 4 }; CheckingSampleSettings SampleSettings4 = new CheckingSampleSettings(null, Sample4, 3); response = kmeanApi.CheckSample(SampleSettings4, out detectedCluster); Assert.True(response.Code == 0); Assert.True(detectedCluster == 2); }
public Cluster[] GetClusteredData(string clusterFilePath) { Cluster[] cluster; AnomalyDetectionResponse AnoResponse; AnomalyDetectionAPI api = new AnomalyDetectionAPI(); AnoResponse = api.GetClusters(clusterFilePath, out cluster); return(cluster); }
public double[][] GetPreviousData(int dataId, string path) { AnomalyDetectionAPI kApi = new AnomalyDetectionAPI(); string filePath = $"{Directory.GetCurrentDirectory()}\\Instance Result\\CheckSample.json"; double[][] oldData; var response = kApi.GetPreviousSamples(filePath, out oldData); return(oldData); }
public void ContinuousTrainData() { double[][] initialCentroids = new double[4][]; initialCentroids[0] = new double[] { 0.2, -4.0 }; initialCentroids[1] = new double[] { 0.2, -6.0 }; initialCentroids[2] = new double[] { 0.4, -4.0 }; initialCentroids[3] = new double[] { 0.4, -6.0 }; string[] attributes = new string[] { "x", "y" }; int numAttributes = attributes.Length; // 2 in this demo (x,y) int numClusters = 4; int maxCount = 300; SaveLoadSettings sett; var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function1.json", out sett, true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(null, numClusters); LearningApi api = new LearningApi(loadMetaData1()); api.UseActionModule <object[][], object[][]>((input, ctx) => { var rawDatalist = getRealDataSample(@"C:\Data\Function1.csv").ToList(); double[][] oldSamples; var nn = kmeanApi.GetPreviousSamples(sett, out oldSamples); if (oldSamples != null) { foreach (var old in oldSamples) { var row = old.Cast <object>().ToArray(); rawDatalist.Add(row); } } return(rawDatalist.ToArray()); }); //this call must be first in the pipeline api.UseDefaultDataMapper(); api.UseGaussNormalizer(); var rawData = api.Run() as double[][]; Helpers.WriteToCSVFile(rawData); ClusteringSettings Settings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, sett, KmeansAlgorithm: 1, InitialGuess: true, Replace: true); AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(Settings); }
public Cluster[] GetClusteredData([FromBody] Dictionary <string, string> clusterFilePath) { var file = clusterFilePath["Path"]; Cluster[] cluster; AnomalyDetectionResponse AnoResponse; AnomalyDetectionAPI api = new AnomalyDetectionAPI(); AnoResponse = api.GetClusters(file, out cluster); return(cluster); }
public void Test_GetPreviousSamples() { AnomalyDetectionAPI kApi = new AnomalyDetectionAPI(); string filePath = $"{Directory.GetCurrentDirectory()}\\Instance Result\\CheckSample.json"; double[][] oldData; var response = kApi.GetPreviousSamples(filePath, out oldData); Assert.True(response.Code == 0); Assert.True(oldData != null); Assert.True(oldData.Length != 0); }
public void Training() { int cnt = 0; double[][] initialCentroids = new double[4][]; initialCentroids[0] = new double[] { 0.4, 25.0 }; initialCentroids[1] = new double[] { 0.4, 15.0 }; initialCentroids[2] = new double[] { 0.6, 15.0 }; initialCentroids[3] = new double[] { 0.6, 25.0 }; string[] attributes = new string[] { "x", "y" }; int numAttributes = attributes.Length; int numClusters = 4; int maxCount = 300; ClusteringSettings clusterSettings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: 1, Replace: true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(clusterSettings); //AnomalyDetectionAPI(clusterSettings), Constractor should not be null when run Training() AnomalyDetectionResponse response; // Creates learning api object LearningApi api = new LearningApi(loadMetaData1()); api.UseActionModule <object[][], object[][]>((input, ctx) => { var rawDataArray = getData(cnt); return(rawDataArray); }); api.UseDefaultDataMapper(); api.UseGaussNormalizer(); // for (int i = 0; i < 15; i++) { cnt = i; var rawData = api.Run() as double[][]; response = kmeanApi.Training(rawData, initialCentroids); Helpers.WriteToCSVFile(kmeanApi.GetCentroid(), $"Data\\Centroid{i}.csv"); //response = kmeanApi.Save($"Function{i}.json"); } }
public void Test_GetResults() { SaveLoadSettings persistenceProviderSettings; //For checking sample, file path should be Instace Result path var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function1.json", out persistenceProviderSettings, false); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(null, 0); ClusteringResults[] detectedCluster; var response = kmeanApi.GetResults(persistenceProviderSettings, out detectedCluster); Assert.True(response.Code == 0); }
/// <summary> /// Save is a function that saves an instance of AnomalyDetectionAPI. /// </summary> /// <param name="SaveObject">settings to save the AnomalyDetectionAPI instance</param> /// <param name="Instance">AnomalyDetectionAPI object to be saved</param> /// <returns>a code and a message that state whether the function succeeded or encountered an error. When the function succeeds, it will return: /// <ul style="list-style-type:none"> /// <li> - Code: 0, "OK" </li> /// </ul> /// </returns> public AnomalyDetectionResponse Save(SaveLoadSettings SaveObject, AnomalyDetectionAPI Instance) { try { //Directory.CreateDirectory(Path.GetDirectoryName(SaveObject.ModelPath)); FileStream fs = new FileStream(SaveObject.ModelPath, FileMode.Create); DataContractJsonSerializer JSONSerializer = new DataContractJsonSerializer(typeof(AnomalyDetectionAPI)); JSONSerializer.WriteObject(fs, Instance); fs.Dispose(); return(new AnomalyDetectionResponse(0, "OK")); } catch (Exception Ex) { return(new AnomalyDetectionResponse(400, "Function <Save -JSON- (AnomalyDetecionAPI)>: Unhandled exception:\t" + Ex.ToString())); } }
public void ContinuousTrainData2() { int cnt = 0; double[][] initialCentroids = new double[4][]; initialCentroids[0] = new double[] { 40.0, 10.0 }; initialCentroids[1] = new double[] { 20.0, 10.0 }; initialCentroids[2] = new double[] { 40.0, 20.0 }; initialCentroids[3] = new double[] { 20.0, 20.0 }; string[] attributes = new string[] { "x", "y" }; int numAttributes = attributes.Length; // 2 in this demo (x,y) int numClusters = 4; int maxCount = 300; SaveLoadSettings sett; var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function1.json", out sett, true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(null, numClusters, initialCentroids); LearningApi api = new LearningApi(loadMetaData1()); api.UseActionModule <object[][], object[][]>((input, ctx) => { var rawDatalist = getData(cnt); return(rawDatalist); }); //this call must be first in the pipeline api.UseDefaultDataMapper(); api.UseGaussNormalizer(); for (int i = 0; i < 15; i++) { cnt = i; var rawData = api.Run() as double[][]; ClusteringSettings Settings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, sett, KmeansAlgorithm: 1, InitialGuess: true, Replace: true); AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(Settings); } }
public void Test_GetClusters() { double[][] clusterCentars = new double[3][]; clusterCentars[0] = new double[] { 5.0, 5.0 }; clusterCentars[1] = new double[] { 15.0, 15.0 }; clusterCentars[2] = new double[] { 30.0, 30.0 }; double[][] initialCentroids = new double[3][]; clusterCentars[0] = new double[] { 5.0, 5.0 }; clusterCentars[1] = new double[] { 15.0, 15.0 }; clusterCentars[2] = new double[] { 30.0, 30.0 }; string[] attributes = new string[] { "Height", "Weight" }; var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5); int numAttributes = attributes.Length; // 2 in this demo (height,weight) int numClusters = 3; // vary this to experiment (must be between 2 and number data tuples) int maxCount = 300; // trial and error ClusteringSettings Settings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: 1, InitialGuess: true, Replace: true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(Settings); AnomalyDetectionResponse response = kmeanApi.Training(rawData, clusterCentars); Assert.True(response.Code == 0); Cluster[] clusters; response = kmeanApi.GetClusters(null, out clusters); // Path can be null if GetClusters() runs after Training() Assert.True(response.Code == 0); Assert.True(clusters.Length != 0); Assert.True(clusters != null); AnomalyDetectionAPI kApi = new AnomalyDetectionAPI(); string filePath = $"{Directory.GetCurrentDirectory()}\\Cluster Result\\CheckSample.json"; Cluster[] clusters1; response = kmeanApi.GetClusters(null, out clusters1); Assert.True(response.Code == 0); Assert.True(clusters.Length != 0); Assert.True(clusters != null); }
public void TestObjestWithCentroid() { double[][] initialCentroids = new double[4][]; initialCentroids[0] = new double[] { 0.2, -4.0 }; initialCentroids[1] = new double[] { 0.2, -6.0 }; initialCentroids[2] = new double[] { 0.4, -4.0 }; initialCentroids[3] = new double[] { 0.4, -6.0 }; string[] attributes = new string[] { "x", "y" }; var data = getRealDataSample(@"C:\Data\Function1.csv"); List <double[]> list = new List <double[]>(); foreach (var n in data) { double[] d = new double[n.Length]; for (int i = 0; i < n.Length; i++) { d[i] = double.Parse(n[i].ToString()); } list.Add(d); } var rawData = list.ToArray(); int numAttributes = attributes.Length; // 2 in this demo (height,weight) int numClusters = 4; // vary this to experiment (must be between 2 and number data tuples) int maxCount = 300; // trial and error SaveLoadSettings sett; var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function1.json", out sett, true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(rawData, numClusters); ClusteringSettings Settings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, sett, KmeansAlgorithm: 1, InitialGuess: true, Replace: true); AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(Settings); }
public AnomalyDetectionResponse Training([FromBody] TrainingContent trainingContent) { ClusteringSettings clusterSettings = new ClusteringSettings(trainingContent.KmeansMaxIterations, trainingContent.NumOfClusters, trainingContent.NumOfAttributes, KmeansAlgorithm: 1, Replace: true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(clusterSettings); AnomalyDetectionResponse response = null; try { double[][] rawData = null; foreach (var file in trainingContent.CsvFilePaths) { rawData = dataProvider(file); response = kmeanApi.Training(rawData); } if (response.Code == 0) { response = kmeanApi.Save(trainingContent.SavePath); } return(response); } catch (Exception Ex) { if (Ex is System.IO.FileNotFoundException) { response = new AnomalyDetectionResponse(200, "File not found"); } else if (Ex is System.IO.DirectoryNotFoundException) { response = new AnomalyDetectionResponse(204, "Directory not found"); } else { response = new AnomalyDetectionResponse(202, "File cannot be loaded"); } return(response); } }
public void TestWithNormalize_GaussAndCentroid() { double[][] initialCentroids = new double[4][]; initialCentroids[0] = new double[] { 0.2, -4.0 }; initialCentroids[1] = new double[] { 0.2, -6.0 }; initialCentroids[2] = new double[] { 0.4, -4.0 }; initialCentroids[3] = new double[] { 0.4, -6.0 }; string[] attributes = new string[] { "x", "y" }; // Creates learning api object LearningApi api = new LearningApi(loadMetaData1()); //Real dataset must be defined as object type, because data can be numeric, binary and classification api.UseActionModule <object[][], object[][]>((input, ctx) => { return(getRealDataSample(@"C:\Data\Function15.csv")); }); //this call must be first in the pipeline api.UseDefaultDataMapper(); api.UseGaussNormalizer(); var rawData = api.Run() as double[][]; int numAttributes = attributes.Length; // 2 in this demo (height,weight) int numClusters = 4; // vary this to experiment (must be between 2 and number data tuples) int maxCount = 300; // trial and error SaveLoadSettings sett; var resp = SaveLoadSettings.JSON_Settings(@"C:\Data\Function15.json", out sett, true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(rawData, numClusters); ClusteringSettings Settings = new ClusteringSettings(rawData, maxCount, numClusters, numAttributes, sett, KmeansAlgorithm: 1, InitialGuess: true, Replace: true); AnomalyDetectionResponse response = kmeanApi.ImportNewDataForClustering(Settings); }
public void Test_BalancedClusters() { // prepare the 2d functions for clustering double[][] mFun = Helpers.cSVtoDoubleJaggedArray(@"C:\Users\KywAnn\Desktop\Anomaly Detection Functions Normalized\Sine 3 HP\Sine 3 HP.csv"); // normalize functions mFun = NormalizeData(mFun); // load the original function double[][] rawData = getSimilarFunctionsData(mFun, 1); // Settings for the K-Means Alg int maxCount = 500; int maxNumberOfClusters = 5; int minNumberOfClusters = 2; int numAttributes = 2; int KAlg = 2; // recommended number of clusters int recNumClusters; ClusteringSettings clusterSettings = new ClusteringSettings(maxCount, 2, numAttributes, KmeansAlgorithm: KAlg, Replace: true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(clusterSettings); // get the suggested number of clusters bas on the balanced clusters method AnomalyDetectionResponse ar = kmeanApi.RecommendedNumberOfClusters(rawData, maxCount, numAttributes, maxNumberOfClusters, minNumberOfClusters, 3, null, out recNumClusters, kmeansAlgorithm: KAlg); }
public AnomalyDetectionResponse Training(string[] csvFilePath, string savePath, string loadimpPath, int numClusters, int numOfAttributes, int kmeansMaxIterations) { ClusteringSettings clusterSettings = new ClusteringSettings(kmeansMaxIterations, numClusters, numOfAttributes, KmeansAlgorithm: 1, Replace: true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(clusterSettings); AnomalyDetectionResponse response; try { double[][] rawData = null; foreach (var file in csvFilePath) { rawData = dataProvider(file); } response = kmeanApi.Training(rawData); response = kmeanApi.Save(savePath); return(response); } catch (Exception Ex) { if (Ex is System.IO.FileNotFoundException) { response = new AnomalyDetectionResponse(200, "File not found"); } else if (Ex is System.IO.DirectoryNotFoundException) { response = new AnomalyDetectionResponse(204, "Directory not found"); } else { response = new AnomalyDetectionResponse(202, "File cannot be loaded"); } return(response); } }
public void Test_PatternReccognition() { // directory to load string myDirectory = @"C:\Users\KywAnn\Desktop\Anomaly Detection Pattern Recognition\Generated Similar Functions\NL1\"; // directory to save string saveDirectory = @"C:\Users\KywAnn\Desktop\Anomaly Detection Pattern Recognition\LastRun\"; // functions' names string[] FunctionPaths = new string[] { "Sine 5 HP SimilarFunctions NL1.csv", "Triangular 4 HP SimilarFunctions NL1.csv", "TriangularHWR SimilarFunctions NL1.csv" }; // Settings for the K-Means Alg int maxCount = 500; int numClusters = 4; int numAttributes = 2; int KAlg = 2; ClusteringSettings clusterSettings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: KAlg, Replace: true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(clusterSettings); double[][] mFun; // Number of functions (training + testing) int NumFun; // Number of training functions int NumTrainFun = 800; // limit between training and testing functions int NumFunLimit = 0; AnomalyDetectionResponse response; double[][] rawData; double[][] Centroids; // load and cluster all desired functions for (int s = 0; s < FunctionPaths.Length; s++) { mFun = Helpers.cSVtoDoubleJaggedArray(myDirectory + FunctionPaths[s]); mFun = NormalizeData(mFun); NumFun = mFun.Length - 1; for (int i = NumFunLimit; i < NumFun; i++) { rawData = getSimilarFunctionsData(mFun, i + 1); response = kmeanApi.Training(rawData); Centroids = kmeanApi.GetCentroid(); // save centroids if (i == NumFunLimit) { Helpers.Write2CSVFile(Centroids, saveDirectory + "Centroids Of " + FunctionPaths[s]); } else { Helpers.Write2CSVFile(Centroids, saveDirectory + "Centroids Of " + FunctionPaths[s], true); } } // only cluster testing data for the rest of the functions NumFunLimit = NumTrainFun; } // get centroids for pattern recognition rawData = Helpers.cSVtoDoubleJaggedArray(saveDirectory + "Centroids Of " + FunctionPaths[0]); // get only the training centroids double[][] trainData = new double[NumFunLimit * numClusters][]; for (int i = 0; i < NumFunLimit * numClusters; i++) { // get training data trainData[i] = rawData[i]; } clusterSettings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: KAlg, Replace: true); kmeanApi = new AnomalyDetectionAPI(clusterSettings); response = kmeanApi.Training(trainData); // get & save the centroids of clustered centroids Centroids = kmeanApi.GetCentroid(); Helpers.Write2CSVFile(Centroids, saveDirectory + "Calculated Centroids.csv"); // get & save max distance per cluster double[] maxDistance = kmeanApi.GetInClusterMaxDistance(); double[][] tempMaxDistance = new double[1][]; tempMaxDistance[0] = maxDistance; Helpers.Write2CSVFile(tempMaxDistance, saveDirectory + "Calculated Max Distance.csv"); // start testing for pattern recognition int tolerance = 0; // start testing centroids of similar functions double[][] testData = new double[rawData.Length - NumFunLimit * numClusters][]; for (int i = 0; i < testData.Length; i++) { // get testing data testData[i] = rawData[i + NumFunLimit * numClusters]; } // contains results of pattern testing (1 for matching, 0 otherwise) double[][] testFun = new double[FunctionPaths.Length][]; // check if the pattern match for similar functions (fits in all clusters) testFun[0] = PatternTesting(testData, numClusters, kmeanApi, tolerance); // start testing centroids different functions for (int p = 1; p < FunctionPaths.Length; p++) { // get testing data testData = Helpers.cSVtoDoubleJaggedArray(saveDirectory + "Centroids Of " + FunctionPaths[p]); // check if the pattern match for differnt functions (fits in all clusters) testFun[p] = PatternTesting(testData, numClusters, kmeanApi, tolerance); } // save results Helpers.Write2CSVFile(testFun, saveDirectory + "Results.csv"); }
public void GenerateAndTest() { // Settings to Generate Similar functions string FunctionName = "Function0"; string directory = @"C:\Users\KywAnn\Desktop\Anomaly Detection Functions Normalized\" + FunctionName + "\\"; // number of similar functions int NumSimFunc = 999; // added noise level (distortion) between -NL & NL int NL = 1; // Settings for the K-Means Alg int maxCount = 500; int numClusters = 6; int numAttributes = 2; int KAlg = 2; int Runs = 5; // generate the similar functions GenerateSimilarFunctions(directory + FunctionName + ".csv", NumSimFunc, NL); // prepare the 2d functions for clustering double[][] mFun = Helpers.cSVtoDoubleJaggedArray(directory + "\\NL" + NL + "\\" + FunctionName + " SimilarFunctions NL" + NL + ".csv"); // normalize the functions mFun = NormalizeData(mFun); // save the normalized similar functions Helpers.Write2CSVFile(mFun, directory + "\\NL" + NL + "\\" + FunctionName + " SimilarFunctions Normalized NL" + NL + ".csv"); int NumFun = mFun.Length - 1; ClusteringSettings clusterSettings; AnomalyDetectionAPI kmeanApi; AnomalyDetectionResponse response; double[][] Centroids; // original Centroids double[][] oCentroids; // matched Centroids double[][] mCentroids; for (int k = 2; k < numClusters + 1; k++) { oCentroids = new double[k][]; clusterSettings = new ClusteringSettings(maxCount, k, numAttributes, KmeansAlgorithm: KAlg, Replace: true); kmeanApi = new AnomalyDetectionAPI(clusterSettings); for (int j = 0; j < Runs; j++) { // save directory string SavePath = directory + "NL" + NL + "\\" + FunctionName + " SimilarFunctions Centroids NL" + NL + " KA" + KAlg + " C" + k + " I" + maxCount + " R" + (j + 1) + ".csv"; for (int i = 0; i < NumFun; i++) { // cluster each function double[][] rawData = getSimilarFunctionsData(mFun, i + 1); response = kmeanApi.Training(rawData); Centroids = kmeanApi.GetCentroid(); // match the centroids centroids if (i == 0) { oCentroids = Centroids; mCentroids = Centroids; } else { mCentroids = matchCentroids(Centroids, oCentroids); } // save centroids if (i == 0) { // save or overwrite Helpers.Write2CSVFile(mCentroids, SavePath); } else { // append Helpers.Write2CSVFile(mCentroids, SavePath, true); } } // save in a different format to plot results easily in excel Special2DWrite2CSV(SavePath, k); } } }
public void Test_CheckSample() { double[][] clusterCentars = new double[3][]; clusterCentars[0] = new double[] { 5.0, 5.0 }; clusterCentars[1] = new double[] { 15.0, 15.0 }; clusterCentars[2] = new double[] { 30.0, 30.0 }; double[][] initialCentroids = new double[3][]; clusterCentars[0] = new double[] { 5.0, 5.0 }; clusterCentars[1] = new double[] { 15.0, 15.0 }; clusterCentars[2] = new double[] { 30.0, 30.0 }; string[] attributes = new string[] { "Height", "Weight" }; var rawData = Helpers.CreateSampleData(clusterCentars, 2, 10000, 0.5); int numAttributes = attributes.Length; // 2 in this demo (height,weight) int numClusters = 3; // vary this to experiment (must be between 2 and number data tuples) int maxCount = 300; // trial and error ClusteringSettings Settings = new ClusteringSettings(maxCount, numClusters, numAttributes, KmeansAlgorithm: 1, InitialGuess: true, Replace: true); AnomalyDetectionAPI kmeanApi = new AnomalyDetectionAPI(Settings); AnomalyDetectionResponse response = kmeanApi.Training(rawData, clusterCentars); Assert.True(response.Code == 0); response = kmeanApi.Save("CheckSample.json"); Assert.True(response.Code == 0); int detectedCluster; double[] Sample = new double[] { 26, 28 }; CheckingSampleSettings SampleSettings = new CheckingSampleSettings(null, Sample, 3); //If path is null you should run Training() response = kmeanApi.CheckSample(SampleSettings, out detectedCluster); Assert.True(response.Code == 0); Assert.True(detectedCluster == 2); AnomalyDetectionAPI kApi = new AnomalyDetectionAPI(); string filePath = $"{Directory.GetCurrentDirectory()}\\Instance Result\\CheckSample.json"; double[] Sample2 = new double[] { 150, 16 }; CheckingSampleSettings SampleSettings2 = new CheckingSampleSettings(filePath, Sample2, 3); response = kApi.CheckSample(SampleSettings2, out detectedCluster); Assert.True(response.Code == 1); Assert.True(detectedCluster == -1);// Out of all clusters. double[] Sample3 = new double[] { 16, 14 }; CheckingSampleSettings SampleSettings3 = new CheckingSampleSettings(filePath, Sample3, 3); response = kApi.CheckSample(SampleSettings3, out detectedCluster); Assert.True(response.Code == 0); Assert.True(detectedCluster == 1); double[] Sample4 = new double[] { 6, 4 }; CheckingSampleSettings SampleSettings4 = new CheckingSampleSettings(filePath, Sample4, 3); response = kApi.CheckSample(SampleSettings4, out detectedCluster); Assert.True(response.Code == 0); Assert.True(detectedCluster == 0); }