public sparseMatrixRow Clone() { sparseMatrixRow r = new sparseMatrixRow(lable); List <int> dimsN = new List <int>(); List <double> valuesN = new List <double>(); for (int i = 0; i < dims.Count; i++) { dimsN.Add(dims[i]); valuesN.Add(values[i]); } r.dims = dimsN; r.values = valuesN; r.FileName = this.FileName; return(r); }
public bool getMatrixFromString(string theMatrixInAString) { matrix.Clear(); string[] matrixInRows = Regex.Split(theMatrixInAString, "[\n|\r]"); ClassDescriptionDictionary = new Dictionary <int, string>(); string lastFileName = ""; //Declare the PathCandidates we will use Regex spaceSplitter = new Regex(@"[\s]+"); Regex doubleDotSplitter = new Regex(@":"); //Parse and load to Ram foreach (string read in matrixInRows) { //Keep us safe from comments if (read.StartsWith("#ClassDescription")) { string[] cols = Regex.Split(read, "\t"); ClassDescriptionDictionary.Add(int.Parse(cols[1]), cols[2]); continue; } else if (read.StartsWith("#")) { lastFileName = read.Substring(1, read.Length - 1); continue; } if (read.Length == 0) { continue; } List <int> dims = new List <int>(); List <double> values = new List <double>(); string[] line = spaceSplitter.Split(read); int lable = int.Parse(line[0]); for (int i = 1; i < line.Length; i++) { string[] info = doubleDotSplitter.Split(line[i]); try { dims.Add(int.Parse(info[0])); values.Add(double.Parse(info[1])); } catch (Exception e) { throw new Exception(e.Message + "\n" + info[0].ToString() + " " + info[1].ToString() + "\n" + "Line " + i.ToString() + "\n" + line[i].ToString()); } } sparseMatrixRow row = new sparseMatrixRow(lable, dims, values); row.FileName = lastFileName; matrix.Add(row); } return(true); }
//------ public void addRow(sparseMatrixRow theRow) { this.matrix.Add(theRow); }
/// <summary> /// This method compresses the matrix only to two vectors, one from the positive class /// and another from the negative. This is achieved by averaging the values for each dimension /// </summary> /// public void shatterMatrix() { SortedDictionary <int, double> pos = new SortedDictionary <int, double>(); SortedDictionary <int, double> neg = new SortedDictionary <int, double>(); int noPosVectors = 0; int noNegVectors = 0; //Sum EveryBody foreach (sparseMatrixRow r in matrix) { if (r.Lable > 0) { //Positive Class noPosVectors++; for (int i = 0; i < r.Dims.Count; i++) { if (pos.ContainsKey(r.Dims[i])) { pos[r.Dims[i]] += r.Values[i]; } else { pos.Add(r.Dims[i], r.Values[i]); } } } else { //Negative Class noNegVectors++; for (int i = 0; i < r.Dims.Count; i++) { if (neg.ContainsKey(r.Dims[i])) { neg[r.Dims[i]] += r.Values[i]; } else { neg.Add(r.Dims[i], r.Values[i]); } } } } List <int> posDims = new List <int>(); List <double> posValues = new List <double>(); List <int> negDims = new List <int>(); List <double> negValues = new List <double>(); foreach (KeyValuePair <int, double> kvp in pos) { posDims.Add(kvp.Key); posValues.Add(kvp.Value / noPosVectors); } foreach (KeyValuePair <int, double> kvp in neg) { negDims.Add(kvp.Key); negValues.Add(kvp.Value / noNegVectors); } this.matrix.Clear(); sparseMatrixRow posRow = new sparseMatrixRow(1, posDims, posValues); sparseMatrixRow negRow = new sparseMatrixRow(-1, negDims, negValues); this.matrix.Add(posRow); this.matrix.Add(negRow); }
//---------------------------------------------- //Private Methods /// <summary> /// If the index is not found, -1 will be returned /// </summary> /// <param name="row"></param> /// <param name="value"></param> /// <returns></returns> private static int getDimIndexFromValue(sparseMatrixRow row, int value) { return(row.Dims.IndexOf(value)); }
public SpectralNode(PatternTools.sparseMatrixRow v1) { myClusterRepresentation = PatternTools.pTools.MergeTwoInputVectors(v1, 1, v1, 1, false);; MyInputVectors.Add(v1); }
//Constructors ----------------------------------- public SpectralNode(PatternTools.sparseMatrixRow v1, PatternTools.sparseMatrixRow v2) { myInputVectors.Add(v1); myInputVectors.Add(v2); myClusterRepresentation = PatternTools.pTools.MergeTwoInputVectors(v1, 1, v2, 1, true); }