Пример #1
0
 public Cluster(Node parent)
 {
     this.clusterPair = null;
     this.items = new List<Vector>();
     this.mean = null;
     this.parent = parent;
 }
Пример #2
0
 // The special constructor is used to deserialize values.
 public Sample(SerializationInfo info, StreamingContext context)
 {
     x = (Vector)info.GetValue("x", typeof(Vector));
     y = (Vector)info.GetValue("y", typeof(Vector));
     label = (double)info.GetValue("label", typeof(double));
     id = (int)info.GetValue("id", typeof(int));
 }
Пример #3
0
 public Sample(double[] input, double[] output, double label, int id)
 {
     this.x = new Vector(input);
     this.y = new Vector(output);
     this.id = id;
     this.label = label;
 }
Пример #4
0
 // The special constructor is used to deserialize values.
 public Cluster(SerializationInfo info, StreamingContext context)
 {
     clusterPair = (ClusterPair)info.GetValue("clusterPair", typeof(ClusterPair));
     items = (List<Vector>)info.GetValue("items", typeof(List<Vector>));
     mean = (Vector)info.GetValue("mean", typeof(Vector));
     meanMDF = (ILArray<double>)info.GetValue("meanMDF", typeof(ILArray<double>));
     parent = (Node)info.GetValue("parent", typeof(Node));
 }
Пример #5
0
        public void GetDistance_GetCorrectDistance()
        {
            Vector v1 = new Vector(new double[] { 1, 3, 4 });
            Vector v2 = new Vector(new double[] { 7, 2, 3 });

            double distance = Math.Round(v1.GetDistance(v2), 3);

            Assert.AreEqual(distance, 6.164);
        }
Пример #6
0
        public void Equals_VectorNotEqualsToVector()
        {
            Vector vector1 = new Vector(new double[] { 1.0, 2.0, 0, 3.0 });
            Vector vector2 = new Vector(new double[] { 1.0, 0.0, 0, 3.0 });

            bool equals = vector1.EqualsToVector(vector2);

            Assert.IsFalse(equals);
        }
Пример #7
0
        public void GetMDFDistance_GetCorrectDistance()
        {
            Vector v = new Vector(new double[] { 1, 3, 4 });
            v.ValuesMDF = new double[] { 1, 3, 4 };
            ILArray<double> a2 = new double[] { 7, 2, 3 };

            double distance = Math.Round(v.GetMDFDistance(a2), 3);

            Assert.AreEqual(distance, 6.164);
        }
Пример #8
0
        public void AddItem(Vector vector, double label)
        {
            Vector newItem = new Vector(vector.Values.ToArray());
            newItem.Label = label;
            newItem.Id = this.items.Count + 1;

            this.items.Add(newItem);
            // update mean
            this.UpdateMean(newItem);
        }
Пример #9
0
        public void Add_AddToVectorCorrectVector()
        {
            Vector vector = new Vector(new double[] { 1.0, 2.0, 3.0});
            Vector vector2 = new Vector(new double[] { 2.0, 3.0, 4.0 });

            vector.Add(vector2);

            Assert.AreEqual(vector.Values[0], 3.0);
            Assert.AreEqual(vector.Values[1], 5.0);
            Assert.AreEqual(vector.Values[2], 7.0);
        }
Пример #10
0
        public void GetIdOfClosestVector_GetCorrectId()
        {
            List<Vector> vectors = new List<Vector>();
            vectors.Add(new Vector(new double[] { 1.0, 2.0, 3.0 }, 1.0, 0));
            vectors.Add(new Vector(new double[] { 2.0, 3.0, 4.0 }, 1.0, 1));
            vectors.Add(new Vector(new double[] { 2.0, 2.0, 2.0 }, 1.0, 2));
            vectors.Add(new Vector(new double[] { 7.0, 8.0, 6.0 }, 1.0, 3));

            Vector vector = new Vector(new double[] { 2.0, 3.0, 5.0 });

            int result = vector.GetIdOfClosestVector(vectors);

            Assert.AreEqual(result, 1);
        }
Пример #11
0
        public void GetMeanOfDataWithLabel_ReturnCorrectMean()
        {
            Params.inputDataDimension = 3;

            Samples samples = new Samples();
            samples.Items.Add(new Sample(new double[] { 1, 2, 3 }, 1, 0));
            samples.Items.Add(new Sample(new double[] { 2, 3, 4 }, 1, 0));
            samples.Items.Add(new Sample(new double[] { 1, 2, 3 }, 2, 0));
            samples.Items.Add(new Sample(new double[] { 2, 3, 4 }, 2, 0));
            samples.Items.Add(new Sample(new double[] { 5, 6, 7 }, 2, 0));
            samples.Items.Add(new Sample(new double[] { 5, 6, 7 }, 3, 0));

            Vector result1 = samples.GetMeanOfDataWithLabel(1.0);

            Vector result2 = new Vector(new double[] { 1.5, 2.5, 3.5 });

            Assert.AreEqual(result1.Values[0], result1.Values[0]);
            Assert.AreEqual(result1.Values[1], result1.Values[1]);
            Assert.AreEqual(result1.Values[2], result1.Values[2]);
        }
Пример #12
0
 public bool EqualsToVector(Vector vector)
 {
     if (this.values.Length != vector.values.Length) throw new InvalidOperationException("Not the same count of attributes");
     for (int i = 0; i < this.values.Length; i++)
     {
         if (this.values[i] != vector.values[i]) return false;
     }
     return true;
 }
Пример #13
0
        public double GetDistance(Vector vector)
        {
            if (this.values.Count() != vector.values.Count()) throw new InvalidOperationException("Not the same count of attributes");

            ILArray<double> result = this.values - vector.values;
            result = result * result;

            return Math.Sqrt(result.Sum());
        }
Пример #14
0
        public static Vector GetVarianceOfVectors(List<Vector> vectors, Vector mean)
        {
            Vector result = new Vector(vectors[0].values.Length, vectors[0].valuesMDF.Length, 0.0);

            foreach (var item in vectors)
            {
                Vector diff = new Vector(item.valuesMDF.ToArray());
                diff.SubtractMDF(mean);
                diff.MultiplyMDF(diff);
                result.AddMDF(diff);
            }

            result.DivideMDF(vectors.Count - 1);

            return result;
        }
Пример #15
0
        public void Add(Vector vector)
        {
            if (this.values.Length != vector.values.Length) throw new InvalidOperationException("Not the same count of attributes");

            this.values = this.values + vector.values;
        }
Пример #16
0
        /// <summary>
        /// Update mean with amnesic average
        /// </summary>
        /// <param name="vector">Vector, which should be add to mean</param>
        protected void UpdateMean(Vector vector)
        {
            #warning this must be remade according to F. Amnesic average with parameters t1, t2

            double t = (double)this.items.Count;

            //double multiplier1 = (t - 1) / t;
            //double multiplier2 = 1 / t;
            double multiplier1 = (t - 1 - this.GetAmnesicParameter(t)) / t;
            double multiplier2 = (1 + this.GetAmnesicParameter(t)) / t;

            this.mean.Multiply(multiplier1);
            Vector incrementPart = new Vector(vector.Values.ToArray());

            incrementPart.Multiply(multiplier2);

            this.mean.Add(incrementPart);
        }
Пример #17
0
        public static Vector GetMeanOfVectorsMDF(List<Vector> vectors)
        {
            Vector result = new Vector(vectors[0].values.Length, vectors[0].valuesMDF.Length, 0.0);

            foreach (Vector item in vectors)
            {
                result.AddMDF(item);
            }

            result.DivideMDF((double)vectors.Count);

            return result;
        }
Пример #18
0
 private double GetLabelOfClosestY(Vector YMean)
 {
     double minDistance = double.MaxValue;
     double resultLabel = 0;
     foreach (var item in this.labelOutputVectors)
     {
         double distance = item.Value.GetDistance(YMean);
         if (distance < minDistance)
         {
             minDistance = distance;
             resultLabel = item.Key;
         }
     }
     return resultLabel;
 }
Пример #19
0
        public void SubtractMDF(Vector vector)
        {
            if (this.valuesMDF.Length != vector.valuesMDF.Length) throw new InvalidOperationException("Not the same count of attributes");

            this.valuesMDF = this.valuesMDF - vector.valuesMDF;
        }
Пример #20
0
 public void UpdateMeanMdf(Vector newItem)
 {
     ILArray<double> newsample = newItem.ValuesMDF.ToArray();
     this.meanMDF = (this.meanMDF * ((double)(this.countOfSamples - 1) / (double)this.countOfSamples)) + ((1 / (double)this.countOfSamples) * newsample);
 }
Пример #21
0
        public Vector GetOutputVectorFromBmp(Bitmap bmp)
        {
            if(Params.outputDataDimension != bmp.Width * bmp.Height)
            {
                throw new InvalidOperationException("Unable to create output from bmp of another dimensionality.");
            }

            List<double> attributes = new List<double>();

            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    Color color = bmp.GetPixel(j,i);
                    attributes.Add((int)((color.R * .3) + (color.G * .59) + (color.B * .11)));
                }
            }

            Vector output = new Vector(attributes.ToArray());
            return output;
        }
Пример #22
0
        private void EvaluateSwap()
        {
            if (this.clusterPairs.Count < Params.q)
            {
                throw new InvalidOperationException("Small count of microclusters, impossible to swap");
            }

            // random selection is deleted, because of unification of results
            // select q random samples
            //List<Vector> centres = new List<Vector>();
            //Random random = new Random();
            //List<int> randoms = new List<int>();
            //for (int i = 0; i < Params.q; i++)
            //{
            //    bool randomIsNotUnique = true;
            //    int r = 0;

            //    while (randomIsNotUnique)
            //    {
            //        r = random.Next(this.clusterPairs.Count);
            //        randomIsNotUnique = randoms.Contains(r);
            //    }
            //    //Console.WriteLine("Center like cluster: " + r.ToString());
            //    randoms.Add(r);

            //    Vector vector = new Vector(this.clusterPairs[r].X.Mean.Values.ToArray());
            //    vector.Id = i;
            //    centres.Add(vector);
            //}

            #region instead of random

            List<Vector> centres = new List<Vector>();
            for (int i = 0; i < Params.q; i++)
            {
                Vector vector = new Vector(this.clusterPairs[i].X.Mean.Values.ToArray());
                vector.Id = i;
                centres.Add(vector);
            }

            #endregion

            // evaluate to which center vector belongs
            foreach (ClusterPair clPair in clusterPairs)
            {
                clPair.CurrentToPrev();
                clPair.CurrentCenter = clPair.X.Mean.GetIdOfClosestVector(centres);
            }

            // while assignment is not changed, do
            bool next = true;
            int whil = 1;
            while (next)
            {
                //Console.WriteLine("While cycle " + whil.ToString());
                for (int i = 0; i < centres.Count; i++)
                {
                    //select all vectors assignet to center
                    List<Vector> assignedVectors = clusterPairs.Where(cp => cp.CurrentCenter == centres[i].Id).Select(cp => cp.X.Mean).ToList<Vector>();
                    // update center
                    if (assignedVectors.Count > 0)
                    {
                        Vector v = new Vector(Params.inputDataDimension, 0.0);
                        double dis1 = centres[i].GetDistance(v);
                        centres[i] = Vector.GetMeanOfVectors(assignedVectors);
                        double dis2 = centres[i].GetDistance(v);

                        //Console.WriteLine("Center " + i.ToString() + " Distance 1: " + dis1.ToString() + " Distance 2: " + dis2.ToString());
                    }
                    centres[i].Id = i;
                }

                // update assignments
                // evaluate to which center vector belongs
                foreach (ClusterPair clPair in clusterPairs)
                {
                    clPair.CurrentToPrev();
                    clPair.CurrentCenter = clPair.X.Mean.GetIdOfClosestVector(centres);

                    //Console.WriteLine("Change cl. " + clPair.Id.ToString() + " from " + clPair.PreviousCenter.ToString() + " to " + clPair.CurrentCenter.ToString());
                }

                next = this.AssigmentIsChanged();

                whil++;
            }
        }
Пример #23
0
        public void Substract_RestulVectorIsCorrect()
        {
            Vector vector = new Vector(new double[] { 1.0, 2.0, 3.0 });
            Vector vector2 = new Vector(new double[] { 2.0, 4.0, 6.0 });

            vector.Subtract(vector2);

            Assert.AreEqual(vector.Values[0], -1.0);
            Assert.AreEqual(vector.Values[1], -2.0);
            Assert.AreEqual(vector.Values[2], -3.0);
        }
Пример #24
0
        /// <summary>
        /// Update MDF mean with amnesic average
        /// </summary>
        /// <param name="vector">Vector, which should be add to mean</param>
        protected void UpdateMeanMDF(ILArray<double> vector)
        {
            double t = (double)this.items.Count;

            //double multiplier1 = (t - 1) / t;
            //double multiplier2 = 1 / t;
            double multiplier1 = (t - 1 - this.GetAmnesicParameter(t)) / t;
            double multiplier2 = (1 + this.GetAmnesicParameter(t)) / t;

            Vector incrementPart = new Vector(vector.ToArray());

            this.meanMDF = (this.meanMDF * multiplier1) + (vector * multiplier2);
        }
Пример #25
0
 public void Multiply(Vector multiplier)
 {
     this.values = this.values * multiplier.values;
 }
Пример #26
0
        public void AddItemNonLeaf(Vector newItem)
        {
            newItem.Id = this.items.Count + 1;

            this.items.Add(newItem);
            // update mean
            this.UpdateMean(newItem);

            //node.CountGSOManifold();

            this.UpdateMeanAndCovMatrixMDF(newItem.ValuesMDF.ToArray());
        }
Пример #27
0
 public void MultiplyMDF(Vector multiplier)
 {
     this.valuesMDF = this.valuesMDF * multiplier.valuesMDF;
 }
Пример #28
0
        public static int GetIdOfClosestVector(Vector vector, List<Vector> vectors)
        {
            int result = 0;
            double minDistance = double.MaxValue;

            foreach (Vector item in vectors)
            {
                double distance = vector.GetDistance(item);
                if (distance < minDistance)
                {
                    result = item.Id;
                    minDistance = distance;
                }
            }

            return result;
        }
Пример #29
0
        public Vector GetOutputFromKnownSamples(Sample sample)
        {
            if (!this.outputs.ContainsKey(sample.Label))
            {
                this.outputs[sample.Label] = new MappedValue(){ Mean = new Vector(sample.X.Values.ToArray()), Count = 1};
            }
            else
            {
                this.outputs[sample.Label].Count++;
                int count = this.outputs[sample.Label].Count;
                this.outputs[sample.Label].Mean.Multiply(((double)count-1.0)/(double)count);
                Vector addPart = new Vector(sample.X.Values.ToArray());
                addPart.Multiply(1 / (double)count);
                this.outputs[sample.Label].Mean.Add(addPart);
            }

            return new Vector(this.outputs[sample.Label].Mean.Values.ToArray());
        }
Пример #30
0
 public void AddItemWithoutUpdatingStats(Vector vector)
 {
     this.items.Add(vector);
 }