Exemple #1
0
        // Simulate with test data, output: predicted class label
        public int Simulate(Vector TestData, double Spread)
        {
            this.Spread = Spread;

            for (int i = 0; i < NumberOfClass; i++)
            {
                int classLabel = this.ClassLabel[i];
                for (int j = 0; j < GetNumberOfInstances(classLabel); j++)
                {
                    ClassProbability[i] += Math.Exp(-Math.Pow((TrainingData[classLabel][j] - TestData).Norm(), 2) / Math.Pow((2 * Spread), 2));
                }
                ClassProbability[i] /= GetNumberOfInstances(classLabel);
            }

            double Max = ClassProbability[0];
            int MaxIndex = 0;

            for (int i = 1; i < NumberOfClass; i++)
            {
                if (ClassProbability[i] > Max)
                {
                    MaxIndex = i;
                    Max = ClassProbability[i];
                }
            }

            return ClassLabel[MaxIndex];
        }
Exemple #2
0
        public int Simulate(Vector TestData)
        {
            double y = 0.0;
            for (int i = 0; i < NumberOfCenter; i++)
            {
                double[] centerRow = CenterMatrix.GetRow(i);
                Vector centerVector = new Vector(centerRow);

                y += WeightMatrix.GetRow(i)[0] * GaussRBF(TestData, centerVector);
            }

            return (int)Math.Round(y);
        }
Exemple #3
0
 public void AddData(Vector Data)
 {
     this.Data.Add(Data);
     this.Dim = Data.Length;
 }
Exemple #4
0
        public void Train()
        {
            // Vector to matrices
            for (int i = 0; i < TrainingData.Count; i++)
            {
                for (int j = 0; j < TrainingData[i].Length; j++)
                {
                    DataMatrix[i, j] = TrainingData[i][j];
                }
                TargetMatrix[i, 0] = TargetData[i];
            }

            // TODO: Center calculation using K-means

            // Compute phi
            PhiMatrix = new Matrix(NumberOfData, NumberOfCenter);
            for (int i = 0; i < NumberOfData; i++)
            {
                for (int j = 0; j < NumberOfCenter; j++)
                {
                    double[] dataRow = DataMatrix.GetRow(i);
                    double[] centerRow = CenterMatrix.GetRow(j);
                    Vector dataVector = new Vector(dataRow);
                    Vector centerVector = new Vector(centerRow);

                    PhiMatrix[i, j] = GaussRBF(dataVector, centerVector);
                }
            }

            // Calculate weight
            WeightMatrix = new Matrix(NumberOfCenter, 1);
            WeightMatrix = (PhiMatrix.Transpose() * PhiMatrix).Invert() * PhiMatrix.Transpose() * TargetMatrix;
        }
Exemple #5
0
 public double GaussRBF(Vector X, Vector Center)
 {
     return Math.Exp(-Math.Pow((X - Center).Norm(), 2) / Math.Pow((2 * this.Spread), 2));
 }
Exemple #6
0
 public void AddTrainingData(int ClassLabel, Vector Data)
 {
     TargetData.Add(ClassLabel);
     TrainingData.Add(Data);
 }
Exemple #7
0
 public void AddTrainingData(int ClassLabel, Vector Data)
 {
     TrainingData[ClassLabel].Add(Data);
 }
Exemple #8
0
 public double Dot(Vector V)
 {
     if (this.Length != V.Length) throw new Exception("Vector dimension mismatch!");
     double sum = 0.0;
     for (int i = 0; i < this.Length; i++)
     {
         sum += Value[i] * V[i];
     }
     return sum;
 }