// 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]; }
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); }
public void AddData(Vector Data) { this.Data.Add(Data); this.Dim = Data.Length; }
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; }
public double GaussRBF(Vector X, Vector Center) { return Math.Exp(-Math.Pow((X - Center).Norm(), 2) / Math.Pow((2 * this.Spread), 2)); }
public void AddTrainingData(int ClassLabel, Vector Data) { TargetData.Add(ClassLabel); TrainingData.Add(Data); }
public void AddTrainingData(int ClassLabel, Vector Data) { TrainingData[ClassLabel].Add(Data); }
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; }