Пример #1
0
        public void TestNoObservations()
        {
            IList <BasicData> list = new List <BasicData>();
            var kmeans             = new KMeansClustering(3);

            kmeans.InitForgy(list);
        }
Пример #2
0
        /// <summary>
        /// Process the file and cluster.
        /// </summary>
        ///
        /// <param name="outputFile">The output file.</param>
        /// <param name="clusters">The number of clusters.</param>
        /// <param name="theAnalyst">The analyst to use.</param>
        /// <param name="iterations">The number of iterations to use.</param>
        public void Process(FileInfo outputFile, int clusters,
                            EncogAnalyst theAnalyst, int iterations)
        {
            StreamWriter tw = PrepareOutputFile(outputFile);

            ResetStatus();

            var cluster = new KMeansClustering(clusters,
                                               _data);

            cluster.Iteration(iterations);

            int clusterNum = 0;

            foreach (IMLCluster cl  in  cluster.Clusters)
            {
                foreach (IMLData item  in  cl.Data)
                {
                    var       row      = (ClusterRow)item;
                    int       clsIndex = row.Input.Count - 1;
                    LoadedRow lr       = row.Row;
                    lr.Data[clsIndex] = "" + clusterNum;
                    WriteRow(tw, lr);
                }
                clusterNum++;
            }

            ReportDone(false);
            tw.Close();
        }
Пример #3
0
        public byte[] Denoise(byte[] data, int width, int height, int padding, out int it)
        {
            it          = 0;
            byte[,] arr = Utils.ByteArr1DTo2D(data, width, height);
            bool[,] vi  = (bool[, ])Array.CreateInstance(typeof(bool), height, width);

            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    vi[i, j] = false;
                }
            }

            List <Point> points = new List <Point>();
            List <int>   areas  = new List <int>();

            for (int i = padding; i < height - padding; ++i)
            {
                for (int j = padding; j < width - padding; ++j)
                {
                    if (vi[i, j])
                    {
                        continue;
                    }
                    if (arr[i, j] == backgroundColor)
                    {
                        continue;
                    }
                    int sum = 0;
                    dfsSearch(arr, vi, i, j, width, height, padding, ref sum);
                    points.Add(new Point(j, i));
                    areas.Add(sum);
                }
            }

            if (points.Count > 1)
            {
                Vector2[] vs = areas.Select(i => new Vector2(i, 0)).ToArray();
                it = 0;
                KMeansCluster result = KMeansClustering.AnnealCluster(vs, 2, out it);

                int bigcluster = (result[0].Centroid.ModSqr() > result[1].Centroid.ModSqr()) ? 0 : 1;
                for (int i = 0; i < points.Count; ++i)
                {
                    if (result.ClusterIndex[i] != bigcluster)
                    {
                        //dfsColor(arr, points[i].Y, points[i].X, width, height, 1);
                        dfsClear(arr, points[i].Y, points[i].X, width, height, 1);
                    }
                }
            }
            else
            {
                dfsColor(arr, points[0].Y, points[0].X, width, height, 1);
            }

            byte[] bytes = Utils.ByteArr2DTo1D(arr);
            return(bytes);
        }
Пример #4
0
        private void ClusterWith99BlogsSomeBlogsShouldAlwayBeClusteredTogether(string[] blogsThatShouldBeClusteredTogether)
        {
            // Arrange

            var sut = new KMeansClustering(new PearsonCorrelationSimilarityAlgorithm());

            for (var iteration = 0; iteration <= 10; iteration++)
            {
                // Act

                var result = sut.Cluster(this.blogs, 5, -1).ToArray();

                // Assert

                Assert.Equal(5, result.Count());

                Centroid centroidWithBlogsThatShouldBeClusteredTogether = null;

                foreach (var centroid in result)
                {
                    if (centroid.Blogs.Any(b => b.Name == blogsThatShouldBeClusteredTogether.First()))
                    {
                        centroidWithBlogsThatShouldBeClusteredTogether = centroid;
                        break;
                    }
                }

                foreach (var blogName in blogsThatShouldBeClusteredTogether)
                {
                    Assert.Contains(centroidWithBlogsThatShouldBeClusteredTogether.Blogs, b => b.Name == blogName);
                }
            }
        }
Пример #5
0
        public Image <Gray, byte> Process(Image <Gray, byte> src)
        {
            Connectivity.Connect8 = this.Connect8;
            var cl = src.connectLevel(1, 8, 0).Domains.ToList();

            var dst = src.Clone();

            if (cl.Count > 1)
            {
                Vector2[] vs = cl.Select(i => new Vector2(i.Area, 0)).ToArray();
                KMeansClustering.MaxIterate = this.MaxIteration;
                var result = KMeansClustering.AnnealCluster(vs, 2);

                int foreCluster = (result[0].Centroid.ModSqr() > result[1].Centroid.ModSqr()) ? 0 : 1;
                for (int i = 0; i < cl.Count; ++i)
                {
                    if (result.ClusterIndex[i] != foreCluster)
                    {
                        foreach (var p in cl[i].Points)
                        {
                            dst[p] = new Gray(0);
                        }
                    }
                }
            }

            return(dst);
        }
Пример #6
0
    public void AddProteinIngredient(string path, Bounds bounds, List <Vector4> atomSpheres, Color color,
                                     List <float> clusterLevels = null,
                                     bool nolod = false)
    {
        if (SceneHierarchy.Contains(path))
        {
            throw new Exception("Invalid protein path: " + path);
        }
        if (ProteinIngredientNames.Contains(path))
        {
            throw new Exception("Invalid protein path: " + path);
        }

        if (clusterLevels != null)
        {
            if (NumLodLevels != 0 && NumLodLevels != clusterLevels.Count)
            {
                throw new Exception("Uneven cluster levels number: " + path);
            }
        }
        if (color == null)
        {
            color = MyUtility.GetRandomColor();
        }


        AddIngredientToHierarchy(path);


        ProteinColors.Add(color);
        ProteinToggleFlags.Add(1);
        ProteinIngredientNames.Add(path);
        ProteinRadii.Add(AtomHelper.ComputeRadius(atomSpheres));

        ProteinAtomCount.Add(atomSpheres.Count);
        ProteinAtomStart.Add(ProteinAtoms.Count);
        ProteinAtoms.AddRange(atomSpheres);

        if (clusterLevels != null)
        {
            NumLodLevels = clusterLevels.Count;
            foreach (var level in clusterLevels)
            {
                var            numClusters = Math.Max(atomSpheres.Count * level, 5);
                List <Vector4> clusterSpheres;
                if (!nolod)
                {
                    clusterSpheres = KMeansClustering.GetClusters(atomSpheres, (int)numClusters);
                }
                else
                {
                    clusterSpheres = new List <Vector4>(atomSpheres);
                }
                ProteinAtomClusterCount.Add(clusterSpheres.Count);
                ProteinAtomClusterStart.Add(ProteinAtomClusters.Count);
                ProteinAtomClusters.AddRange(clusterSpheres);
            }
        }
    }
Пример #7
0
        public void TestGeneral()
        {
            var kmeans = new KMeansClustering(5);

            Assert.AreEqual(5, kmeans.K);
            kmeans.RandomGeneration = new BasicGenerateRandom();
            kmeans.DistanceMetric   = new EuclideanDistance();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int clusterNumber = (int)numericUpDown1.Value;

            IClustering clustering = new KMeansClustering(clusterNumber);

            Clustering(clustering);
        }
        public ActionResult <IEnumerable <Centroid> > Get(int numberOfClusters, int numberOfIterations)
        {
            var blogs            = blogDataRepository.GetBlogData();
            var pearson          = new PearsonCorrelationSimilarityAlgorithm();
            var kMeansClustering = new KMeansClustering(pearson);

            return(kMeansClustering.Cluster(blogs, numberOfClusters, numberOfIterations).ToArray());
        }
Пример #10
0
        public void TestNoDimension()
        {
            IList <BasicData> list = new List <BasicData>();

            list.Add(new BasicData(0));
            var kmeans = new KMeansClustering(3);

            kmeans.InitForgy(list);
        }
Пример #11
0
        protected internal override void onStart()
        {
            base.onStart();

            // set weights between input and rbf layer using kmeans
            KMeansClustering kmeans = new KMeansClustering(TrainingSet);

            kmeans.NumberOfClusters = neuralNetwork.getLayerAt(1).NeuronsCount;             // set number of clusters as number of rbf neurons
            kmeans.doClustering();

            // get clusters (centroids)
            Cluster[] clusters = kmeans.Clusters;

            // assign each rbf neuron to one cluster
            // and use centroid vectors to initialize neuron's input weights
            Layer rbfLayer = neuralNetwork.getLayerAt(1);
            int   i        = 0;

            foreach (Neuron neuron in rbfLayer.Neurons)
            {
                KVector  centroid     = clusters[i].Centroid;
                double[] weightValues = centroid.Values;
                int      c            = 0;
                foreach (Connection conn in neuron.InputConnections)
                {
                    conn.Weight.Value = weightValues[c];
                    c++;
                }
                i++;
            }

            // get cluster centroids as list
            List <KVector> centroids = new List <KVector>();

            foreach (Cluster cluster in clusters)
            {
                centroids.Add(cluster.Centroid);
            }

            // use KNN to calculate sigma param - gausssian function width for each neuron
            KNearestNeighbour knn = new KNearestNeighbour();

            knn.DataSet = centroids;

            int n = 0;

            foreach (KVector centroid in centroids)
            {
                // calculate and set sigma for each neuron in rbf layer
                KVector[] nearestNeighbours = knn.getKNearestNeighbours(centroid, k);
                double    sigma             = calculateSigma(centroid, nearestNeighbours);  // calculate in method
                Neuron    neuron            = rbfLayer.getNeuronAt(n);
                ((Gaussian)neuron.TransferFunction).Sigma = sigma;
                i++;
            }
        }
Пример #12
0
        public void TestMaxClusters()
        {
            var kmeans = new KMeansClustering(12);

            kmeans.RandomGeneration = new BasicGenerateRandom(22);
            kmeans.InitRandom(GetDataSet());
            int iterations = kmeans.Iteration(1000);

            Assert.AreEqual(1, iterations);
        }
Пример #13
0
        public IEnumerable <Iris> Irises()
        {
            var         kMeansCluster = new KMeansClustering(Iris.GetFromFile(), 3, 4);
            var         kclusters     = kMeansCluster.Start2();
            List <Iris> irises        = new List <Iris>();

            for (int i = 0; i < 3; i++)
            {
                irises.AddRange(Iris.Convert(kclusters.ElementAt(i), Iris.irisNames[i], ""));
            }
            return(irises);
        }
Пример #14
0
    // public static Vector3 GetBiomtCenter(List<Matrix4x4> transforms, Vector3 center)
    // {
    //     if (transforms.Count <= 0) return Vector3.zero;

    //     var bbMin = new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
    //     var bbMax = new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);

    //     foreach (var transform in transforms)
    //     {
    //var euler = MyUtility.euler_from_matrix(transform);
    //var rotBiomt = MyUtility.MayaRotationToUnity(euler);
    //var offset = MyUtility.QuaternionTransform(rotBiomt,center);//Helper.RotationMatrixToQuaternion(matBiomt), GetCenter());
    //var posBiomt = new Vector3(-transform.m03, transform.m13, transform.m23);

    //         bbMin = Vector3.Min(bbMin, new Vector3(posBiomt.x, posBiomt.y, posBiomt.z));
    //         bbMax = Vector3.Max(bbMax, new Vector3(posBiomt.x, posBiomt.y, posBiomt.z));
    //     }

    //     var bbSize = bbMax - bbMin;
    //     var bbCenter = bbMin + bbSize * 0.5f;
    //     var bounds = new Bounds(bbCenter, bbSize);

    //     return bounds.center;
    // }

    public static List <List <Vector4> > ComputeLodProxies(List <Vector4> atomSpheres, List <float> clusterLevelFactors)
    {
        var lodProxies = new List <List <Vector4> >();

        foreach (var level in clusterLevelFactors)
        {
            var numClusters     = Math.Max(atomSpheres.Count * level, 5);
            var loxProxySpheres = (level >= 1) ? new List <Vector4>(atomSpheres) : KMeansClustering.GetClusters(atomSpheres, (int)numClusters);
            if (level < 1)
            {
                OverwriteRadii(ref loxProxySpheres, 0);
            }
            lodProxies.Add(loxProxySpheres);
        }

        return(lodProxies);
    }
        public AnomalyDetection(IOrchestrator orcherstratorProxy, IDataAggregator aggregatorProxy)
        {
            orcherstratorProxy.Detection.Subscribe(this, async signal =>
            {
                int cluster = 0;
                if (_kMeansClustering != null)
                {
                    lock (_syncClustering)
                        if (_kMeansClustering != null)
                        {
                            cluster = _kMeansClustering.Classify(new double[] { signal.Value });
                        }
                }

                if (cluster < 0)
                {
                    System.Console.WriteLine("_____________________________Anomaly detected__________________________________");
                    await Anomaly.PublishAsync(new Anomaly()
                    {
                        Temperature = signal
                    });
                }
                return(MessageResult.Ok);
            });

            aggregatorProxy.Aggregate.Subscribe(this, async(sampleReference) =>
            {
                //if the messages has been stored and forwarded, but the file has been deleted (e.g. a restart)
                //then the message can be empty (null)
                if (sampleReference == null)
                {
                    return(MessageResult.Ok);
                }

                System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

                lock (_syncSample)
                    _sample = sampleReference.Message.Values;

                lock (_syncClustering)
                    _kMeansClustering = new KMeansClustering(_sample, _numClusters);

                return(MessageResult.Ok);
            });
        }
        public AnomalyDetection(IOrchestrator preprocessor, IDataSampling trainer)
        {
            preprocessor.Detection.Subscribe(this, async signal =>
            {
                int cluster = 0;
                if (_kMeansClustering != null)
                {
                    lock (_syncClustering)
                        if (_kMeansClustering != null)
                        {
                            cluster = _kMeansClustering.Classify(new double[] { signal.Value, signal.Minimum, signal.Maximum });
                        }
                }

                if (cluster < 0)
                {
                    System.Console.WriteLine("_____________________________Anomaly detected__________________________________");
                    await Anomaly.PublishAsync(new Anomaly()
                    {
                        Temperature = signal
                    });
                }
                return(MessageResult.Ok);
            });

            trainer.Samples.Subscribe(this, async(sampleReference) =>
            {
                //if the messages has been stored and forwarded, but the file has been deleted (e.g. a restart)
                //then the message can be empty (null)
                if (sampleReference == null)
                {
                    return(MessageResult.Ok);
                }

                System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

                lock (_syncSample)
                    _sample = sampleReference.Message.Data.Select(e => new double[] { e.Value, e.Minimum, e.Maximum }).ToArray();

                lock (_syncClustering)
                    _kMeansClustering = new KMeansClustering(_sample, _numClusters);

                return(MessageResult.Ok);
            });
        }
Пример #17
0
        public void Normalize()
        {
            // Processing horizontal clustering
            int[] clusters = new KMeansClustering(Width).Process(Lines);
            if (clusters.Length != Lines.Count)
            {
                throw new Exception("Error during clustering, use debugging for more info.");
            }
            for (int i = 0; i < Lines.Count; i++)
            {
                Lines[i].Bounds.Left = clusters[i];
            }

            // Processing vertical clustering
            FixDistancesAndHeight();

            FixTextLineOrder(clusters);
        }
Пример #18
0
        public void KMeans_MainTest(int expectedClusters, int expectedClusterCount)
        {
            //Generate random vectors
            var vectors = new List <VectorNd>();
            var cir     = new Circle(Plane.WorldXY, 10);
            var pts     = new List <Point3d>();

            for (var i = 0; i < expectedClusters; i++)
            {
                var pt = cir.PointAt(( double )i / expectedClusters);
                pts.Add(pt);
                vectors.AddRange(CreateClusterAround(pt, 1, expectedClusterCount));
            }

            Assert.True(vectors.Count == expectedClusters * expectedClusterCount);
            var eventCheck = false;
            // When
            var kMeans = new KMeansClustering(100, expectedClusters, vectors);

            kMeans.IterationCompleted += (sender, args) =>
            {
                Assert.True(args.Iteration >= 0);
                Assert.True(args.Clusters.Count == expectedClusters);
                eventCheck = true;
            };
            kMeans.Run();
            //Assert the Iteration completed event has been raised
            Assert.True(eventCheck);
            // Then
            kMeans.Clusters.ForEach(
                cluster =>
            {
                Assert.NotEmpty(cluster);
                var first   = new Point3d(cluster[0][0], cluster[0][1], cluster[0][2]);
                var closest = pts.First(pt => pt.DistanceTo(first) <= 2);
                foreach (var vector in cluster)
                {
                    var pt   = new Point3d(vector[0], vector[1], vector[2]);
                    var dist = pt.DistanceTo(closest);
                    Assert.True(dist >= 0);
                }
            });
        }
Пример #19
0
        public void TestCluster()
        {
            var set = new BasicMLDataSet();

            int i;

            for (i = 0; i < Data.Length; i++)
            {
                set.Add(new BasicMLData(Data[i]));
            }

            var kmeans = new KMeansClustering(2, set);

            kmeans.Iteration();

            i = 1;
            foreach (IMLCluster cluster in kmeans.Clusters)
            {
                IMLDataSet  ds = cluster.CreateDataSet();
                IMLDataPair pair;
                pair = ds[0];
                double t = pair.Input[0];

                for (int j = 0; j < ds.Count; j++)
                {
                    pair = ds[j];

                    for (j = 0; j < pair.Input.Count; j++)
                    {
                        if (t > 10)
                        {
                            Assert.IsTrue(pair.Input[j] > 10);
                        }
                        else
                        {
                            Assert.IsTrue(pair.Input[j] < 10);
                        }
                    }
                }

                i++;
            }
        }
Пример #20
0
    //--------------------------------------------------------------

    #region Protein_functions

    public void AddIngredient(string ingredientName, Bounds bounds, List <Vector4> atomSpheres, Color color, List <float> clusterLevels = null)
    {
        if (ProteinNames.Contains(ingredientName))
        {
            return;
        }

        if (NumLodLevels != 0 && NumLodLevels != clusterLevels.Count)
        {
            throw new Exception("Uneven cluster levels number: " + ingredientName);
        }

        if (color == null)
        {
            color = Helper.GetRandomColor();
        }

        ProteinNames.Add(ingredientName);
        ProteinColors.Add(color);
        ProteinToggleFlags.Add(1);
        ProteinBoundingSpheres.Add(Vector3.Magnitude(bounds.extents));

        ProteinAtomCount.Add(atomSpheres.Count);
        ProteinAtomStart.Add(ProteinAtoms.Count);
        ProteinAtoms.AddRange(atomSpheres);

        if (clusterLevels != null)
        {
            NumLodLevels = clusterLevels.Count;

            foreach (var level in clusterLevels)
            {
                var numClusters    = Math.Max(atomSpheres.Count * level, 5);
                var clusterSpheres = KMeansClustering.GetClusters(atomSpheres, (int)numClusters);

                ProteinAtomClusterCount.Add(clusterSpheres.Count);
                ProteinAtomClusterStart.Add(ProteinAtomClusters.Count);
                ProteinAtomClusters.AddRange(clusterSpheres);
            }
        }
    }
Пример #21
0
    public static string decodeBitsAdvanced(string bits)
    {
        if (bits.Length == 0 || !bits.Contains('1'))
        {
            return("");
        }
        bits = bits.Trim('0');
        Regex      rx1       = new Regex(@"1+");
        Regex      rx0       = new Regex(@"0+");
        List <int> oneUnits  = rx1.Matches(bits).Select(g => g.Length).ToList();
        List <int> zeroUnits = rx0.Matches(bits).Select(g => g.Length).ToList();

        KMeansClustering oneClusters  = new KMeansClustering(2, oneUnits);
        KMeansClustering zeroClusters = new KMeansClustering(3, zeroUnits);

        oneClusters.FindClusters();
        zeroClusters.FindClusters();
        System.Console.WriteLine();

        // // spaces between words
        // bits = bits.Replace(new string('0', spaceUnit), "   ");

        // // pause between characters in word
        // bits = bits.Replace(new string('0', pauseUnit), " ");

        // // pause between dots and dashes
        // bits = bits.Replace(new string('0', inWordUnit), "#");

        // // get dashes
        // bits = bits.Replace(new string('1', dashUnit), "-");

        // // get dots
        // bits = bits.Replace(new string('1', dotUnit), ".");

        // // remove placeholders from string
        // bits = bits.Replace("#", "");

        return(bits);
    }
Пример #22
0
        public void TestClusterRandom()
        {
            var kmeans = new KMeansClustering(4)
            {
                RandomGeneration = new BasicGenerateRandom(44)
            };

            kmeans.InitRandom(GetDataSet());
            int iterations = kmeans.Iteration(1000);

            Assert.AreEqual(2, iterations);

            Cluster cluster1 = kmeans.Clusters[0];
            Cluster cluster2 = kmeans.Clusters[1];
            Cluster cluster3 = kmeans.Clusters[2];
            Cluster cluster4 = kmeans.Clusters[3];

            Assert.AreEqual(3, cluster1.Observations.Count);
            Assert.AreEqual(1, cluster2.Observations.Count);
            Assert.AreEqual(6, cluster3.Observations.Count);
            Assert.AreEqual(2, cluster4.Observations.Count);
        }
Пример #23
0
        /// <summary>
        /// Perform the example.
        /// </summary>
        public void Run()
        {
            // read the iris data from the resources
            Assembly assembly = Assembly.GetExecutingAssembly();
            var      res      = assembly.GetManifestResourceStream("AIFH_Vol1.Resources.iris.csv");

            // did we fail to read the resouce
            if (res == null)
            {
                Console.WriteLine("Can't read iris data from embedded resources.");
                return;
            }

            // load the data
            var     istream = new StreamReader(res);
            DataSet ds      = DataSet.Load(istream);

            istream.Close();

            // perform the cluster
            IList <BasicData> observations = ds.ExtractUnsupervisedLabeled(4);
            KMeansClustering  kmeans       = new KMeansClustering(3);

            kmeans.InitForgy(observations);
            int iterations = kmeans.Iteration(1000);

            Console.WriteLine("Finished after " + iterations + " iterations.");

            for (int i = 0; i < kmeans.K; i++)
            {
                Cluster cluster = kmeans.Clusters[i];
                Console.WriteLine("* * * Cluster #" + i);
                foreach (BasicData d in cluster.Observations)
                {
                    Console.WriteLine(d.ToString());
                }
            }
        }
Пример #24
0
        public static Dictionary<string, int> Cluster(IEnumerable<string> itemIds, double[,] features, int numClusters, string centerPath = "",
            ClusteringAlgorithm algorithm = ClusteringAlgorithm.KMeans)
        {
            Console.WriteLine("Clustering...");

            //features = Normalized(features);

            Console.WriteLine("Features normalized.");

            var dm = new DoubleMatrix(features);
            ClusterSet clusters = null;

            if (algorithm == ClusteringAlgorithm.KMeans)
            {
                var km = new KMeansClustering(dm);
                km.Cluster(numClusters);

                Console.WriteLine("Num Clusters: {0}, Num Items: {1}, Num Iterations: {2}", km.K, km.N, km.Iterations);

                if (centerPath != "")
                {
                    var cWriter = new StreamWriter(centerPath);
                    km.FinalCenters.WriteAsCSV(cWriter);
                    cWriter.Close();
                }

                clusters = km.Clusters;
            }
            else
            {
                var nmf = new NMFClustering<NMFDivergenceUpdate>();

                nmf.Factor(dm, numClusters);

                if (nmf.Converged)
                {
                    var uWriter = new StreamWriter(Paths.AmazonBooksUsersCluster + ".nmf");
                    var iWriter = new StreamWriter(Paths.AmazonBooksItemsCluster + ".nmf");

                    nmf.W.WriteAsCSV(uWriter);
                    nmf.H.WriteAsCSV(iWriter);

                    uWriter.Flush();
                    iWriter.Flush();

                    uWriter.Close();
                    iWriter.Close();

                    File.WriteAllLines(Paths.AmazonBooksUsersCluster + ".con", nmf.Connectivity.ToTabDelimited().Split('\n'));

                    clusters = nmf.ClusterSet;

                    File.WriteAllLines(Paths.AmazonBooksUsersCluster + ".cluster", clusters.Clusters.Select(c => c.ToString()));

                    Console.WriteLine("Successfully wrote decompose matrixes.");

                }
                else
                {
                    Console.WriteLine("Factorization failed to converge in {0} iterations.", nmf.MaxFactorizationIterations);
                }

            }

            return itemIds.Zip(clusters.Clusters, (i, c) => new { ItemId = i, Cluster = c }).ToDictionary(i => i.ItemId, i => i.Cluster);
        }
Пример #25
0
        public Image <Gray, Byte> Denoise(Image <Gray, Byte> img, int padding, out int it)
        {
            it = 0;
            var output = img.Clone();
            int width = img.Width, height = img.Height;

            if (padding > 0)
            {
                for (int p = 0; p < padding; ++p)
                {
                    for (int i = 0; i < height; ++i)
                    {
                        // 左右两边
                        output[i, 0 + p]         = ColorDefination.BackColor;
                        output[i, width - 1 - p] = ColorDefination.BackColor;
                    }
                    for (int j = 0; j < width; ++j)
                    {
                        // 上下两边
                        output[0 + p, j]          = ColorDefination.BackColor;
                        output[height - 1 - p, j] = ColorDefination.BackColor;
                    }
                }
            } // 清除宽为padding的边框

            bool[,] vi = (bool[, ])Array.CreateInstance(typeof(bool), height, width);
            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    vi[i, j] = false;
                }
            } // 不知道会不会自动初始化成false,如果会,这段可以去掉

            List <Point> points = new List <Point>();
            List <int>   areas  = new List <int>();

            for (int i = padding; i < height - padding; ++i)
            {
                for (int j = padding; j < width - padding; ++j)
                {
                    if (vi[i, j])
                    {
                        continue;
                    }
                    if (img[i, j].Equals(ColorDefination.BackColor))
                    {
                        continue;
                    }
                    //int sum = 0;
                    //dfsSearch(img, vi, i, j, padding, ref sum);
                    Point p   = new Point(j, i);
                    int   sum = bfsSearch(img, vi, p, padding);
                    points.Add(p);
                    areas.Add(sum);
                }
            }

            if (points.Count > 1)
            {
                //var vs = areas.Select(d => (double)d).ToArray();
                //KMeans<double> kmeans = new KMeans<double>(vs, 2,
                //    (a, b) => Math.Abs(a - b),
                //    arr => arr.Average());
                //var result = kmeans.AnnealCluster(
                //    (a, b) => a + b,
                //    (a, b) => a - b,
                //    (a, b) => a / b);
                Vector2[] vs     = areas.Select(i => new Vector2(i, 0)).ToArray();
                var       result = KMeansClustering.AnnealCluster(vs, 2, out it);

                int foreCluster = (result[0].Centroid.ModSqr() > result[1].Centroid.ModSqr()) ? 0 : 1;
                //int foreCluster = (result[0].Center > result[1].Center) ? 0 : 1;
                for (int i = 0; i < points.Count; ++i)
                {
                    if (result.ClusterIndex[i] != foreCluster)
                    {
                        bfsClear(output, points[i], padding);
                    }
                }
            }
            else
            {
                //dfsColor(output, points[0].Y, points[0].X, padding);
            }

            return(output);
        }
Пример #26
0
        static void Main( string[] args )
        {
            // Class NMathChart and NMathStatsChart provide static methods for plotting NMath
              // types using Syncfusion Essential Chart for Windows Forms controls.

              // EXAMPLE 1: CURVE FITTING

              // This NMath code fits a 4-parameter logistic function to data measuring the evolution
              // of an algal bloom in the Adriatic Sea.
              DoubleVector x = new DoubleVector( 11, 15, 18, 23, 26, 31, 39, 44, 54, 64, 74 );
              DoubleVector y = new DoubleVector( 0.00476, 0.0105, 0.0207, 0.0619, 0.337, 0.74, 1.7, 2.45, 3.5, 4.5, 5.09 );
              DoubleVector start = new DoubleVector( 4, 0.1 );
              OneVariableFunctionFitter<TrustRegionMinimizer> fitter =
            new OneVariableFunctionFitter <TrustRegionMinimizer>( AnalysisFunctions.FourParameterLogistic );
              DoubleVector solution = fitter.Fit( x, y, start );

              // For prototyping and debugging console applications, Show() plots common NMath types
              // and displays the chart in a default form.
              int numInterpolatedValues = 100;
              NMathChart.Show( fitter, x, y, solution, numInterpolatedValues );

              // The default look of the chart is governed by static properties: DefaultSize,
              // DefaultTitleFont, DefaultAxisTitleFont, DefaultMajorGridLineColor, and DefaultMarker.

              // For more control, ToChart() returns an instance of Syncfusion.Windows.Forms.Chart.ChartControl,
              // which can be customized as desired.
              ChartControl chart = NMathChart.ToChart( fitter, x, y, solution, numInterpolatedValues );
              chart.Titles[0].Text = "Algal Bloom in the Adriatic Sea";
              chart.PrimaryXAxis.Title = "Days";
              chart.PrimaryYAxis.Title = "Size (mm2)";
              chart.Series[0].Text = "Observed";
              chart.Series[1].Text = "Fitted 4PL";
              chart.BackColor = Color.Beige;
              NMathChart.Show( chart );

              // If you are developing a Windows Forms application using the Designer, add a ChartControl
              // to your form, then update it with an NMath object using the appropriate Update() function
              // after initialization.

              // InitializeComponent();
              // NMathChart.Update( ref this.chart1, fitter, x, y, solution, numInterpolatedValues );

              // EXAMPLE 2: FFT

              // This chart shows a complex signal vector with three component sine waves.
              int n = 100;
              DoubleVector t = new DoubleVector( n, 0, 0.1 );
              DoubleVector signal = new DoubleVector( n );
              for( int i = 0; i < n; i++ )
              {
            signal[i] = Math.Sin( 2 * Math.PI * t[i] ) + 2 * Math.Sin( 2 * Math.PI * 2 * t[i] ) + 3 * Math.Sin( 2 * Math.PI * 3 * t[i] );
              }
              chart = NMathChart.ToChart( signal, new NMathChart.Unit( 0, 0.1, "Time (s)" ) );
              chart.Titles[0].Text = "Signal";
              chart.ChartArea.PrimaryYAxis.Title = "Voltage";
              NMathChart.Show( chart );

              // We use NMath to compute the forward discrete fourier transform, then plot the power in the frequency domain.
              DoubleForward1DFFT fft = new DoubleForward1DFFT( n );
              fft.FFTInPlace( signal );
              DoubleSymmetricSignalReader reader = fft.GetSignalReader( signal );
              DoubleComplexVector unpacked = reader.UnpackSymmetricHalfToVector();
              chart = NMathChart.ToChart( unpacked, new NMathChart.Unit( 0, 0.1, "Frequency (Hz)" ) );
              chart.Titles[0].Text = "FFT";
              chart.ChartArea.PrimaryYAxis.Title = "Power";
              NMathChart.Show( chart );

              // EXAMPLE 3: PEAK FINDING

              // NMath class PeakFinderSavitzkyGolay uses smooth Savitzky-Golay derivatives to find peaks in data.
              // A peak is defined as a smoothed derivative zero crossing.
              double step_size = 0.1;
              x = new DoubleVector( 1000, 0.01, step_size );
              y = NMathFunctions.Sin( x ) / x;
              int width = 5;
              int polynomial_degree = 4;
              PeakFinderSavitzkyGolay pf = new PeakFinderSavitzkyGolay( y, width, polynomial_degree );
              pf.AbscissaInterval = step_size;
              pf.SlopeSelectivity = 0;
              pf.RootFindingTolerance = 0.0001;
              pf.LocatePeaks();

              // Plot the peaks.
              double xmin = 20;
              double xmax = 50;
              NMathChart.Show( pf, xmin, xmax );

              // EXAMPLE 4: K-MEANS CLUSTERING

              // The k-means clustering method assigns data points into k groups such that the sum of squares from points
              // to the computed cluster centers is minimized. Here we cluster 30 points in 3-dimensional space into 5 clusters.
              DoubleMatrix data = new DoubleMatrix( @"30 x 3 [
            0.62731478808400   0.71654239725005   0.11461282117064
            0.69908013774534   0.51131144816890   0.66485556714021
            0.39718395379261   0.77640121193349   0.36537389168912
            0.41362889533818   0.48934547589850   0.14004445653473
            0.65521294635567   0.18590445122522   0.56677280030311
            0.83758509883186   0.70063540514612   0.82300831429067
            0.37160803224266   0.98270880190626   0.67394863209536
            0.42525315848265   0.80663774928874   0.99944730494940
            0.59466337145257   0.70356765500360   0.96163640714857
            0.56573857208571   0.48496371932457   0.05886216545559
            1.36031117091978   1.43187338560697   1.73265064912939
            1.54851281373460   1.63426595631548   1.42222658611939
            1.26176956987179   1.80302634023193   1.96136999885631
            1.59734484793384   1.08388100700103   1.07205923855201
            1.04927799659601   1.94546278791039   1.55340796803039
            1.57105749438466   1.91594245989412   1.29198392114244
            1.70085723323733   1.60198742363800   1.85796351308408
            1.96228825871716   1.25356057873233   1.33575513868621
            1.75051823194427   1.87345080554039   1.68020385037051
            1.73999304537847   1.51340070999628   1.05344442131849
            2.35665553727760   2.67000386489368   2.90898934903532
            2.49830459603553   2.20087641229516   2.59624713810572
            2.43444053822029   2.27308816154697   2.32895530216404
            2.56245841710735   2.62623463865051   2.47819442572535
            2.61662113016546   2.53685169481751   2.59717077926034
            2.11333998089856   2.05950405092050   2.16144875489995
            2.89825174061313   2.08896175947532   2.82947425087386
            2.75455137523865   2.27130817438170   2.95612240635488
            2.79112319571067   2.40907231577105   2.59554799520203
            2.81495206793323   2.47404145037448   2.02874821321149 ]" );
              KMeansClustering km = new KMeansClustering( data );
              ClusterSet clusters = km.Cluster( 5 );

              // We have to specify which plane to plot.
              int xColIndex = 0;
              int yColIndex = 1;
              NMathStatsChart.Show( clusters, data, xColIndex, yColIndex );
        }
Пример #27
0
        public void TestEarlyIteration()
        {
            var kmeans = new KMeansClustering(3);

            kmeans.Iteration();
        }
Пример #28
0
        public void TestTooManyClusters()
        {
            var kmeans = new KMeansClustering(13);

            kmeans.InitRandom(GetDataSet());
        }
Пример #29
0
        public static Dictionary <string, int> Cluster(IEnumerable <string> itemIds, double[,] features, int numClusters, string centerPath = "",
                                                       ClusteringAlgorithm algorithm = ClusteringAlgorithm.KMeans)
        {
            Console.WriteLine("Clustering...");

            //features = Normalized(features);

            Console.WriteLine("Features normalized.");

            var        dm       = new DoubleMatrix(features);
            ClusterSet clusters = null;

            if (algorithm == ClusteringAlgorithm.KMeans)
            {
                var km = new KMeansClustering(dm);
                km.Cluster(numClusters);

                Console.WriteLine("Num Clusters: {0}, Num Items: {1}, Num Iterations: {2}", km.K, km.N, km.Iterations);

                if (centerPath != "")
                {
                    var cWriter = new StreamWriter(centerPath);
                    km.FinalCenters.WriteAsCSV(cWriter);
                    cWriter.Close();
                }

                clusters = km.Clusters;
            }
            else
            {
                var nmf = new NMFClustering <NMFDivergenceUpdate>();

                nmf.Factor(dm, numClusters);

                if (nmf.Converged)
                {
                    var uWriter = new StreamWriter(Paths.AmazonBooksUsersCluster + ".nmf");
                    var iWriter = new StreamWriter(Paths.AmazonBooksItemsCluster + ".nmf");

                    nmf.W.WriteAsCSV(uWriter);
                    nmf.H.WriteAsCSV(iWriter);

                    uWriter.Flush();
                    iWriter.Flush();

                    uWriter.Close();
                    iWriter.Close();

                    File.WriteAllLines(Paths.AmazonBooksUsersCluster + ".con", nmf.Connectivity.ToTabDelimited().Split('\n'));

                    clusters = nmf.ClusterSet;

                    File.WriteAllLines(Paths.AmazonBooksUsersCluster + ".cluster", clusters.Clusters.Select(c => c.ToString()));

                    Console.WriteLine("Successfully wrote decompose matrixes.");
                }
                else
                {
                    Console.WriteLine("Factorization failed to converge in {0} iterations.", nmf.MaxFactorizationIterations);
                }
            }

            return(itemIds.Zip(clusters.Clusters, (i, c) => new { ItemId = i, Cluster = c }).ToDictionary(i => i.ItemId, i => i.Cluster));
        }
Пример #30
0
 public MainWindow()
 {
     InitializeComponent();
     _mapLocations     = new List <IClusterableLocation>();
     _kMeansClustering = new KMeansClustering(1000);
 }
Пример #31
0
        /// <summary>
        /// The drawing for direct-elimination round
        /// </summary>
        /// <param name="round">The round to draw</param>
        /// <returns>The list of games of this round</returns>
        public static List <Match> Draw(KnockoutRound round)
        {
            List <Match> res = new List <Match>();


            List <Club>[] hats = new List <Club>[] { new List <Club>(), new List <Club>() };

            if (round.randomDrawingMethod == RandomDrawingMethod.Ranking)
            {
                GroupsRound   previousRound       = round.Tournament.rounds[round.Tournament.rounds.IndexOf(round) - 1] as GroupsRound;
                List <Club>[] clubsByRankPosition = new List <Club> [previousRound.maxClubsInGroup];
                List <Club>   allClubs            = new List <Club>();

                for (int i = 0; i < clubsByRankPosition.Length; i++)
                {
                    clubsByRankPosition[i] = new List <Club>();
                }

                for (int i = 0; i < previousRound.groupsCount; i++)
                {
                    List <Club> ranking = previousRound.Ranking(i);
                    for (int j = 0; j < ranking.Count; j++)
                    {
                        clubsByRankPosition[j].Add(ranking[j]);
                    }
                }

                for (int i = 0; i < clubsByRankPosition.Length; i++)
                {
                    clubsByRankPosition[i].Sort(new ClubRankingComparator(previousRound.matches));
                    allClubs.AddRange(clubsByRankPosition[i]);
                }

                allClubs = new List <Club>(allClubs.GetRange(0, round.clubs.Count));
                hats[0].AddRange(allClubs.GetRange(0, round.clubs.Count / 2));
                hats[1].AddRange(allClubs.GetRange(round.clubs.Count / 2, round.clubs.Count / 2));
            }
            else if (round.randomDrawingMethod == RandomDrawingMethod.Coefficient)
            {
                List <Club> allClubs = new List <Club>(round.clubs);
                allClubs.Sort(new ClubComparator(ClubAttribute.CONTINENTAL_COEFFICIENT));
                hats[0].AddRange(allClubs.GetRange(0, round.clubs.Count / 2));
                hats[1].AddRange(allClubs.GetRange(round.clubs.Count / 2, round.clubs.Count / 2));
            }
            else if (round.randomDrawingMethod == RandomDrawingMethod.Geographic)
            {
                List <Club> allClubs             = new List <Club>(round.clubs);
                int         currentClubsByGroups = allClubs.Count;
                int         groupsNumber         = 1;
                while ((currentClubsByGroups / 2) % 2 == 0 && (allClubs.Count / groupsNumber) > 20)
                {
                    currentClubsByGroups /= 2;
                    groupsNumber         *= 2;
                }
                KMeansClustering kmeans = new KMeansClustering(allClubs, groupsNumber);
                hats = kmeans.CreateClusters();
            }
            //Random
            else
            {
                List <Club> allClubs = new List <Club>(round.clubs);
                allClubs.Shuffle();
                for (int i = 0; i < allClubs.Count; i++)
                {
                    hats[i < allClubs.Count / 2 ? 0 : 1].Add(allClubs[i]);
                }
            }


            RoundProgrammation programmation = round.programmation;
            int currentGeographicHat         = 0;

            for (int i = 0; i < round.clubs.Count / 2; i++)
            {
                Club home;
                Club away;
                if (round.randomDrawingMethod != RandomDrawingMethod.Geographic)
                {
                    int hat = Session.Instance.Random(0, 2);
                    home = DrawClub(hats[hat]);
                    away = DrawClub(hats[hat == 1 ? 0 : 1]);
                }
                else
                {
                    if (hats[currentGeographicHat].Count < 2)
                    {
                        currentGeographicHat++;
                    }
                    home = DrawClub(hats[currentGeographicHat]);
                    away = DrawClub(hats[currentGeographicHat]);
                }

                DateTime day = GetRoundProgrammationDate(round, programmation);

                if (round.rules.Contains(Rule.AtHomeIfTwoLevelDifference))
                {
                    Club[] switchedTeams = SwitchTeams(home, away);
                    home = switchedTeams[0];
                    away = switchedTeams[1];
                }
                res.Add(new Match(home, away, day, !round.twoLegs));
            }

            TVSchedule(res, round.programmation.tvScheduling, 0);
            if (round.twoLegs)
            {
                CreateSecondLegKnockOutRound(res, round, programmation);
            }
            return(res);
        }
Пример #32
0
        public void Start()
        {
            //Read the data from the files
            var file1DataRaw = AlgorithmHelpers.ReadMatrixFromFile(@"class_1.dat");
            var file2DataRaw = AlgorithmHelpers.ReadMatrixFromFile(@"class_2.dat");

            ClassA = AlgorithmHelpers.ScaleDown(AlgorithmHelpers.ChooseFeatures(file1DataRaw));
            ClassB = AlgorithmHelpers.ScaleDown(AlgorithmHelpers.ChooseFeatures(file2DataRaw));

            m_maxX = ClassA.Max(0)[0] > ClassB.Max(0)[0] ? ClassA.Max(0)[0] : ClassB.Max(0)[0];
            m_minX = ClassA.Min(0)[0] < ClassB.Min(0)[0] ? ClassA.Min(0)[0] : ClassB.Min(0)[0];

            //Fill the charts with the data
            m_view.ShowTrainingData(ClassA, ClassB);
            //Clear the list view
            m_view.ClearListView();
            //Fill it with the confusion matrix for each algorithm per iteration
            var statistics = new ConfusionMatrix[4, 5];


            //Merge the two data sets
            //and run kmeans
            var kmeans = new KMeansClustering(AlgorithmHelpers.MergeArrays(file1DataRaw, file2DataRaw),
                                              m_view.Iterations,
                                              m_view.ThetaStep);

            var idx = kmeans.Classify();

            m_view.ClustersTextUpdate(idx.Distinct().Length.ToString());

            m_view.ZeroProgressBar();
            m_view.StepProgressBar();

            //Partition m_iterations times and run the algorithms
            for (int i = 0; i < m_iterations; ++i)
            {
                m_view.PerformStep();
                //Partition its class to training and testing set
                var partitions = new DataPartition[] { AlgorithmHelpers.Partition(ClassA), AlgorithmHelpers.Partition(ClassB) };

                //Create the training data
                var trainingPair   = AlgorithmHelpers.CreateDataPair(partitions[0].Item1, partitions[1].Item1);
                var trainingSet    = trainingPair.Item1;
                var trainingOutput = trainingPair.Item2;

                //Create the testing data
                var testingPair   = AlgorithmHelpers.CreateDataPair(partitions[0].Item2, partitions[1].Item2);
                var testingSet    = testingPair.Item1;
                var testingOutput = testingPair.Item2;

                //Some functions need the training output to be a vector of doubles
                var doubleTO = trainingOutput
                               .Select(x => new[] { Convert.ToDouble(x) })
                               .ToArray();

                for (int k = 1; k < 3; ++k)
                {
                    var nn = new KNearestNeighboursRuntime(k, trainingSet, trainingOutput, testingSet, testingOutput);

                    if (BestKNN == null)
                    {
                        BestKNN = nn.Execute();
                    }
                    else
                    {
                        var iter = nn.Execute();
                        if (iter.Accuracy > BestKNN.Accuracy)
                        {
                            BestKNN = iter;
                        }
                    }
                }

                var perceptron = new PerceptronRuntime(trainingSet, doubleTO, testingSet, testingOutput);
                perceptron.Finished += perceptron_Finished;

                var leastSquare   = new LeastSquaresRuntime(AlgorithmHelpers.JaggedToMD(trainingSet), AlgorithmHelpers.JaggedToMD(doubleTO), AlgorithmHelpers.JaggedToMD(testingSet), testingOutput);
                var neuralNetwork = new ParallelNeuralNetworkRuntime(trainingSet, doubleTO, testingSet, testingOutput);

                neuralNetwork.Finished += neuralNetwork_Finished;
                //Compute the confusion matrices for the four classifiers
                statistics[0, i] = perceptron.Execute();
                statistics[1, i] = leastSquare.Execute();
                //Use the most accurate K of KNN
                statistics[2, i] = BestKNN;
                statistics[3, i] = neuralNetwork.Execute();
            }

            //Update the classifier lines in the charts
            //with the most accurate of the 5 iterations
            m_view.ChartUpdate("", "classifier", MostAccuratePerceptron.Item1);
            m_view.ChartUpdate("", "classifier1", MostAccurateNN.Item1);

            //Process the array with the Confusion Matrices
            //and update the list view
            var processed = AlgorithmHelpers.ProcessStatistics(statistics);

            m_view.UpdateStatisticsListView(processed);
        }