Beispiel #1
0
 private static ClusteringMetrics ClusteringDelta(
     ClusteringMetrics a, ClusteringMetrics b)
 {
     return(new ClusteringMetrics(
                nmi: a.Nmi - b.Nmi,
                avgMinScore: a.AvgMinScore - b.AvgMinScore,
                dbi: a.Dbi - b.Dbi));
 }
Beispiel #2
0
 private void LogResult(string algorithm, ClusteringMetrics clusteringMetrics)
 {
     Console.WriteLine($"------------- {algorithm} - EVALUATION RESULTS -------------");
     Console.WriteLine($"AVG MIN SCORE = {clusteringMetrics.AverageDistance}");
     Console.WriteLine($"DBI = {clusteringMetrics.DaviesBouldinIndex}");
     Console.WriteLine($"NMI = {clusteringMetrics.NormalizedMutualInformation}");
     Console.WriteLine($"------------- {algorithm} - END EVALUATION -------------");
 }
 public static void PrintClusteringMetrics(string name, ClusteringMetrics metrics)
 {
     Console.WriteLine($"*************************************************");
     Console.WriteLine($"*       Metrics for {name} clustering model      ");
     Console.WriteLine($"*------------------------------------------------");
     Console.WriteLine($"*       Average Distance: {metrics.AverageDistance}");
     Console.WriteLine($"*       Davies Bouldin Index is: {metrics.DaviesBouldinIndex}");
     Console.WriteLine($"*************************************************");
 }
 /// <summary>
 /// Check that a <see cref="ClusteringMetrics"/> object is valid.
 /// </summary>
 /// <param name="metrics">The metrics object.</param>
 public static void AssertMetrics(ClusteringMetrics metrics)
 {
     Assert.True(metrics.AverageDistance >= 0);
     Assert.True(metrics.DaviesBouldinIndex >= 0);
     if (!double.IsNaN(metrics.NormalizedMutualInformation))
     {
         Assert.True(metrics.NormalizedMutualInformation >= 0 && metrics.NormalizedMutualInformation <= 1);
     }
 }
 public static void PrintClusteringMetrics(string name, ClusteringMetrics metrics)
 {
     Console.WriteLine($"*************************************************");
     Console.WriteLine($"*       Metrics for {name} clustering model      ");
     Console.WriteLine($"*------------------------------------------------");
     Console.WriteLine($"*       AvgMinScore: {metrics.AvgMinScore}");
     Console.WriteLine($"*       DBI is: {metrics.Dbi}");
     Console.WriteLine($"*************************************************");
 }
Beispiel #6
0
 /// <summary>
 /// Check that a <see cref="ClusteringMetrics"/> object is valid.
 /// </summary>
 /// <param name="metrics">The metrics object.</param>
 public static void AssertMetrics(ClusteringMetrics metrics)
 {
     Assert.True(metrics.AvgMinScore >= 0);
     Assert.True(metrics.Dbi >= 0);
     if (!double.IsNaN(metrics.Nmi))
     {
         Assert.True(metrics.Nmi >= 0 && metrics.Nmi <= 1);
     }
 }
Beispiel #7
0
 public static void PrintClusteringMetrics(string name, ClusteringMetrics metrics)
 {
     Console.WriteLine($"*************************************************");
     Console.WriteLine($"*       {name} 聚类模型的指标      ");
     Console.WriteLine($"*------------------------------------------------");
     Console.WriteLine($"*       平均距离: {metrics.AverageDistance}");
     Console.WriteLine($"*       Davies Bouldin指数现为: {metrics.DaviesBouldinIndex}");
     Console.WriteLine($"*************************************************");
 }
Beispiel #8
0
        // Pretty-print of ClusteringMetrics object.
        private static void PrintMetrics(ClusteringMetrics metrics)
        {
            Console.WriteLine($"Normalized Mutual Information: " +
                              $"{metrics.NormalizedMutualInformation:F2}");

            Console.WriteLine($"Average Distance: " +
                              $"{metrics.AverageDistance:F2}");

            Console.WriteLine($"Davies Bouldin Index: " +
                              $"{metrics.DaviesBouldinIndex:F2}");
        }
Beispiel #9
0
        public void Train(string trainingFileName, string testingFileName)
        {
            if (!System.IO.File.Exists(trainingFileName))
            {
                Console.WriteLine($"Failed to find training data file ({trainingFileName}");

                return;
            }

            if (!System.IO.File.Exists(testingFileName))
            {
                Console.WriteLine($"Failed to find test data file ({testingFileName}");

                return;
            }

            var trainingDataView = GetDataView(trainingFileName);

            var dataProcessPipeline = MlContext.Transforms.Concatenate(
                FEATURES,
                nameof(FileData.IsBinary),
                nameof(FileData.IsMZHeader),
                nameof(FileData.IsPKHeader));

            var trainer          = MlContext.Clustering.Trainers.KMeans(featureColumnName: FEATURES, numberOfClusters: 3);
            var trainingPipeline = dataProcessPipeline.Append(trainer);
            var trainedModel     = trainingPipeline.Fit(trainingDataView);

            MlContext.Model.Save(trainedModel, trainingDataView.Schema, ModelPath);

            var testingDataView = GetDataView(testingFileName);

            IDataView testDataView = trainedModel.Transform(testingDataView);

            ClusteringMetrics modelMetrics = MlContext.Clustering.Evaluate(
                data: testDataView,
                labelColumnName: "Label",
                scoreColumnName: "Score",
                featureColumnName: FEATURES);

            Console.WriteLine($"Average Distance: {modelMetrics.AverageDistance}");
            Console.WriteLine($"Davies Bould Index: {modelMetrics.DaviesBouldinIndex}");
            Console.WriteLine($"Normalized Mutual Information: {modelMetrics.NormalizedMutualInformation}");
        }
Beispiel #10
0
        /// <summary>
        /// Evaluates scored clustering data.
        /// </summary>
        /// <param name="data">The scored data.</param>
        /// <param name="score">The name of the score column in <paramref name="data"/>.</param>
        /// <param name="label">The name of the optional label column in <paramref name="data"/>.</param>
        /// <param name="features">The name of the optional feature column in <paramref name="data"/>.</param>
        /// <returns>The evaluation results.</returns>
        public ClusteringMetrics Evaluate(IDataView data, string score, string label = null, string features = null)
        {
            Host.CheckValue(data, nameof(data));
            Host.CheckNonEmpty(score, nameof(score));

            var roles = new List <KeyValuePair <RoleMappedSchema.ColumnRole, string> >();

            roles.Add(RoleMappedSchema.CreatePair(MetadataUtils.Const.ScoreValueKind.Score, score));

            if (label != null)
            {
                roles.Add(RoleMappedSchema.ColumnRole.Label.Bind(label));
            }

            if (features != null)
            {
                roles.Add(RoleMappedSchema.ColumnRole.Feature.Bind(features));
            }

            var rolesMappedData = new RoleMappedData(data, opt: false, roles.ToArray());

            var resultDict = Evaluate(rolesMappedData);

            Host.Assert(resultDict.ContainsKey(MetricKinds.OverallMetrics));
            var overall = resultDict[MetricKinds.OverallMetrics];

            ClusteringMetrics result;

            using (var cursor = overall.GetRowCursor(i => true))
            {
                var moved = cursor.MoveNext();
                Host.Assert(moved);
                result = new ClusteringMetrics(Host, cursor, _calculateDbi);
                moved  = cursor.MoveNext();
                Host.Assert(!moved);
            }
            return(result);
        }
Beispiel #11
0
        public async Task <IActionResult> OnPost(IFormFile file, string train, string predict, string download)
        {
            // var fileName = file.FileName;
            using (var stream = new MemoryStream())
            {
                file.CopyTo(stream);
                var customers           = new ExcelImportProcessor().GetImportData(stream);
                var rfmCalculateService = new RfmCalculateService();
                var calculatedScores    = rfmCalculateService.CalculateRfmScores(customers);

                var segmentator = new CustomersSegmentator(_hostingEnvironment.ContentRootPath);
                if (!string.IsNullOrEmpty(train))
                {
                    var businessData = calculatedScores.Select(x => new Rfm
                    {
                        R = x.R,
                        F = x.F,
                        M = x.M
                    }).ToList();
                    _clusteringMetrics            = segmentator.Train(businessData);
                    ViewData["ClusteringMetrics"] = _clusteringMetrics.AvgMinScore.ToString();
                }
                else if (!string.IsNullOrEmpty(predict))
                {
                    var businessData = calculatedScores.Select(x => new ClusteringData
                    {
                        R = x.R,
                        F = x.F,
                        M = x.M
                    }).ToList();
                    var predictions        = segmentator.Predict(businessData);
                    var predictedCustomers = calculatedScores.Select((t, i) => new PredictionResult
                    {
                        CustomerId = t.CustomerId.ToString(), R = t.R, F = t.F, M = t.M, Cluster = predictions[i]
                    }).ToList();
                    ViewData["PredictedCustomers"] = predictedCustomers;
                }
                else if (!string.IsNullOrEmpty(download))
                {
                    var businessData = calculatedScores.Select(x => new ClusteringData
                    {
                        R = x.R,
                        F = x.F,
                        M = x.M
                    }).ToList();
                    var predictions        = segmentator.Predict(businessData);
                    var predictedCustomers = calculatedScores.Select((t, i) => new PredictionResult
                    {
                        CustomerId = t.CustomerId.ToString(), R = t.R, F = t.F, M = t.M, Cluster = predictions[i]
                    }).ToList();
                    ViewData["PredictedCustomers"] = predictedCustomers;
                    var          groupedCustomers = predictedCustomers.GroupBy(x => x.Cluster);
                    ExcelPackage excel            = new ExcelPackage();
                    foreach (var group in groupedCustomers)
                    {
                        var workSheet = excel.Workbook.Worksheets.Add("Cluster" + group.Key.ToString());
                        workSheet.TabColor         = System.Drawing.Color.Black;
                        workSheet.DefaultRowHeight = 12;
                        //Header of table
                        //
                        workSheet.Row(1).Height = 20;
                        workSheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        workSheet.Row(1).Style.Font.Bold           = true;
                        workSheet.Cells[1, 1].Value = "CustomerID";
                        workSheet.Cells[1, 2].Value = "R";
                        workSheet.Cells[1, 3].Value = "F";
                        workSheet.Cells[1, 4].Value = "M";
                        //Body of table
                        //
                        int recordIndex = 2;
                        foreach (var customer in group)
                        {
                            workSheet.Cells[recordIndex, 1].Value = customer.CustomerId;
                            workSheet.Cells[recordIndex, 2].Value = customer.R;
                            workSheet.Cells[recordIndex, 3].Value = customer.F;
                            workSheet.Cells[recordIndex, 4].Value = customer.M;
                            recordIndex++;
                        }
                        workSheet.Column(1).AutoFit();
                        workSheet.Column(2).AutoFit();
                        workSheet.Column(3).AutoFit();
                        workSheet.Column(4).AutoFit();
                    }
                    string excelName = "ClusteredData.xlsx";
                    using (var memoryStream = new MemoryStream())
                    {
                        excel.SaveAs(memoryStream);
                        var content = memoryStream.ToArray();

                        return(File(
                                   content,
                                   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                   excelName));
                    }
                    //using (var memoryStream = new MemoryStream())
                    //{
                    //    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    //    Response.Headers.Add("content-disposition", "attachment; filename=" + excelName + ".xlsx");
                    //    excel.SaveAs(memoryStream);
                    //    Response.OutputStream. memoryStream.WriteTo();
                    //    Response.Flush();
                    //    Response.End();
                    //}
                }
            }
            return(null);
        }