/// <summary> /// Returns a Single Rows of Data from a 2D array /// </summary> /// <param name="mainArray"></param> /// <param name="mainArrayRowIdx"></param> /// <param name="endColumnIdx"></param> /// <returns></returns> public double[] GetSingleRowData(double[][] mainArray, long mainArrayRowIdx, int endColumnIdx) { double[] data = SupportFunctions.GetLinearArray(mainArray, mainArrayRowIdx, endColumnIdx); return(data); }
private double computeKernel(long row1, long row2) { double value = 0; double[] point1 = SupportFunctions.GetLinearArray(_trainingData, row1, _trainingData.Length - 2); double[] point2 = SupportFunctions.GetLinearArray(_trainingData, row2, _trainingData.Length - 2); value = _kernel.compute(point1, point2); return(value); }
/// <summary> /// Get coefficients from the Langrange Multipliers /// /// This function assumed the last attribute in trainingData is the target value /// </summary> /// <returns></returns> public ModelKernel computeSupportVectors(double[][] trainingData, int indexTargetAttribute, IKernel kernel, int maxThreads) { //Update private Vars ----------------- _kernel = kernel; _lagrangeMul = new double[trainingData[0].Length]; _error_cache = new double[trainingData[0].Length]; SupportFunctions.InitArray(_lagrangeMul, maxThreads); _indexTarget = indexTargetAttribute; _trainingData = trainingData; //--------------------------- //main routine: int numChanged = 0; bool examineAll = true; while (numChanged > 0 || examineAll) { numChanged = 0; if (examineAll) { //loop I over all training examples for (int row = 0; row < trainingData[0].Length; row++) { numChanged += examineExample(row); } } else { //loop I over examples where alpha is not 0 & not C for (int row = 0; row < trainingData[0].Length; row++) { if (_lagrangeMul[row] != 0 && _lagrangeMul[row] != _C) { double[] data = SupportFunctions.GetLinearArray(trainingData, row); numChanged += examineExample(row); } } } if (examineAll) { examineAll = false; } else if (numChanged == 0) { examineAll = true; } } return(getModelKernel()); }
public double[] GetSingleTrainingRowDataForTest(int row) { //Remove these lines after some time /*double [] data = new double[_trainingData.Length - 1]; * for (int col = 0; col < data.Length; col++) * { * data[col] = * _trainingData[col][row]; * } */ double[] data = SupportFunctions.GetLinearArray(_trainingData, row, _trainingData.Length - 2); return(data); }
/// <summary> /// Returns a SOM Map with Name Id's listed for each node /// rather than weights /// </summary> /// <param name="data">Data Matrix to compare weights against. Preffered value is the training data.Columns in data should be equal to number of weights </param> /// <param name="names">String name of each row in data</param> /// <returns>StringBuilder object with map</returns> public string GetPrintedSOMMapWithNameIds(double [][] data, string [] names) { //Do checks if (data.Length != SomMap[0][0].GetNoOfWeights()) { throw new DataColumnMismatchException(); } if (data[0].Length != names.Length) //Name for each Row { throw new DataColumnMismatchException(); } StringBuilder sb = new StringBuilder(); Parallel.For(0, data.Length, new ParallelOptions { MaxDegreeOfParallelism = _maxParallelThreads }, xdim => //for (long xdim = 0; xdim < SomMap.Length; xdim++) //Parallelize this loop { double computedDistValue = double.MaxValue; string tmpLine = ""; for (long ydim = 0; ydim < SomMap[0].Length; ydim++) { //Find best data row for this unit for (int dataRow = 0; dataRow < data[0].Length; dataRow++) { double[] inputDataRow = SupportFunctions.GetLinearArray(data, dataRow, data.Length - 1); computedDistValue = SomMap[xdim][ydim].GetComputedDistance(inputDataRow); if (computedDistValue <= SomMap[xdim][ydim].NameIdDistance) { SomMap[xdim][ydim].NameId = names[dataRow]; SomMap[xdim][ydim].NameIdDistance = computedDistValue; } } //DataRow tmpLine += SomMap[xdim][ydim].NameId + " "; } //ydim sb.AppendLine(tmpLine); }); //xdim return(sb.ToString()); }
public override double RunModelForSingleData(double[] data) { double value = 0; VerifyDataForRun(data); if (data.Length != _data.Length - 1) { throw new InvalidDataException(); } KeyValuePair <int, double>[] allValues = new KeyValuePair <int, double> [_data[0].Length]; //Compute distance per Row Parallel.For(0, _data[0].Length, new ParallelOptions { MaxDegreeOfParallelism = _maxParallelThreads }, row => { double[] tArray = SupportFunctions.GetLinearArray(_data, row, _data.Length - 2); double distance = _distanceMeasure.getDistanceVector(tArray, data); allValues[row] = new KeyValuePair <int, double>(row, distance); }); List <KeyValuePair <int, double> > distList = SupportFunctions.GetSortedKeyValuePair(allValues); double[] targetValues = new double[_k]; //Get lowest k distancesPerRow and their count //for (int ii= 0; ii < _k;ii++) Parallel.For(0, _k, new ParallelOptions { MaxDegreeOfParallelism = _maxParallelThreads }, ii => { int row = distList[ii].Key; targetValues[ii] = _data[_origTargetAttributeIndex][row]; }); //Get the mode value = Dispersion.Mode(targetValues); return(value); }
} //End of procedure #region Support Functions /// <summary> /// Returns the ModelKernel /// </summary> /// <returns></returns> private ModelKernel getModelKernel() { ModelKernel mk = new ModelKernel(); mk.Kernel = _kernel; for (long row = 0; row < _lagrangeMul.Length; row++) { if (_lagrangeMul[row] > 0) { //Get Support Vectors double[] sv = //Do not get target value index SupportFunctions.GetLinearArray(_trainingData, row, _trainingData.Length - 2); //Get alpha mk.Alphas.Add(_lagrangeMul[row]); mk.TargetValues.Add(_trainingData[_indexTarget][row]); mk.SupportVectors.Add(sv); //getThreshold } } mk.Threshold = _threshold; return(mk); }
/// <summary> /// Copies a single row of a 2D jagged array into /// a 1D array /// </summary> /// <param name="mainArray"></param> /// <param name="mainArrayRowIdx"></param> /// <param name="endColumnIdx"></param> /// <returns></returns> public double[] GetLinearArray(double [][] mainArray, long mainArrayRowIdx, int endColumnIdx) { return(SupportFunctions.GetLinearArray(mainArray, mainArrayRowIdx, endColumnIdx)); }