public double Overlap(int[] sdr1, int[] sdr2) { Assert.AreEqual(sdr1.Length, sdr2.Length); int sum = ArrayUtils.Sum(ArrayUtils.And(sdr1, sdr2)); return(sum / (double)ArrayUtils.Sum(sdr1)); }
public double Overlap(int[] sdr1, int[] sdr2) { Assert.AreEqual(sdr1.Length, sdr2.Length); int sum = ArrayUtils.Sum(ArrayUtils.And(sdr1, sdr2)); // Console.WriteLine("and = " + Arrays.toString(ArrayUtils.where(ArrayUtils.and(sdr1, sdr2), ArrayUtils.WHERE_1))); // Console.WriteLine("sum = " + ArrayUtils.sum(ArrayUtils.and(sdr1, sdr2))); return((double)sum / (double)ArrayUtils.Sum(sdr1)); }
private static void NonzeroFilterGroup(int minValids, bool percentage, IMatrixData mdata, Parameters param, bool oneGroup, double threshold, double threshold2, FilteringMode filterMode, IList <string[]> groupCol ) { List <int> valids = new List <int>(); List <int> notvalids = new List <int>(); string[] groupVals = ArrayUtils.UniqueValuesPreserveOrder(groupCol); Array.Sort(groupVals); int[][] groupInds = CalcGroupInds(groupVals, groupCol); for (int i = 0; i < mdata.RowCount; i++) { int[] counts = new int[groupVals.Length]; int[] totals = new int[groupVals.Length]; for (int j = 0; j < groupInds.Length; j++) { for (int k = 0; k < groupInds[j].Length; k++) { if (groupInds[j][k] >= 0) { totals[groupInds[j][k]]++; } } if (PerseusPluginUtils.IsValid(mdata.Values.Get(i, j), threshold, threshold2, filterMode)) { for (int k = 0; k < groupInds[j].Length; k++) { if (groupInds[j][k] >= 0) { counts[groupInds[j][k]]++; } } } } bool[] groupValid = new bool[counts.Length]; for (int j = 0; j < groupValid.Length; j++) { groupValid[j] = PerseusPluginUtils.Valid(counts[j], minValids, percentage, totals[j]); } if (oneGroup ? ArrayUtils.Or(groupValid) : ArrayUtils.And(groupValid)) { valids.Add(i); } else { notvalids.Add(i); } } PerseusPluginUtils.FilterRowsNew(mdata, param, valids.ToArray()); }
public static bool IsValidRowNumFilter(double[] row, Relation[] relations, bool and) { Dictionary <int, double> vars = new Dictionary <int, double>(); for (int j = 0; j < row.Length; j++) { vars.Add(j, row[j]); } bool[] results = new bool[relations.Length]; for (int j = 0; j < relations.Length; j++) { results[j] = relations[j].NumEvaluateDouble(vars); } return(and ? ArrayUtils.And(results) : ArrayUtils.Or(results)); }
/** * Does a bitwise compare of the two bitmaps and returns a fractional * value between 0 and 1 of how similar they are. * 1 => identical * 0 => no overlapping bits * IGNORES difference in length (only compares bits of shorter list) e..g 11 and 1100101010 are "identical" * @see org.numenta.nupic.encoders.Encoder#closenessScores(gnu.trove.list.TDoubleList, gnu.trove.list.TDoubleList, boolean) */ public override List <double> ClosenessScores(List <double> expValues, List <double> actValues, bool fractional) { List <double> result = new List <double>(); double ratio = 1.0d; double expectedSum = expValues.Sum(); double actualSum = actValues.Sum(); if (actualSum > expectedSum) { double diff = actualSum - expectedSum; if (diff < expectedSum) { ratio = 1 - diff / expectedSum; } else { ratio = 1 / diff; } } int[] expectedInts = ArrayUtils.ToIntArray(expValues.ToArray()); int[] actualInts = ArrayUtils.ToIntArray(actValues.ToArray()); int[] overlap = ArrayUtils.And(expectedInts, actualInts); int overlapSum = ArrayUtils.Sum(overlap); double r = 0.0; if (expectedSum != 0) { r = overlapSum / expectedSum; } r = r * ratio; if (LOGGER.IsDebugEnabled) { LOGGER.Debug(string.Format("closenessScores for expValues: {0} and actValues: {1} is: {2}", Arrays.ToString(expectedInts), actualInts, r)); } result.Add(r); return(result); }
public void TestAnd() { int[] a = new int[] { 0, 0, 0, 0, 1, 1, 1 }; int[] b = new int[] { 0, 0, 0, 0, 1, 1, 1 }; int[] result = ArrayUtils.And(a, b); Assert.IsTrue(Arrays.AreEqual(a, result)); a = new int[] { 0, 0, 0, 0, 1, 0, 1 }; result = ArrayUtils.And(a, b); Assert.IsTrue(Arrays.AreEqual(a, result)); a = new int[] { 0, 0, 0, 0, 0, 0, 0 }; result = ArrayUtils.And(a, b); Assert.IsTrue(Arrays.AreEqual(a, result)); a = new int[] { 1, 1, 1, 1, 0, 0, 0 }; int[] expected = new int[] { 0, 0, 0, 0, 0, 0, 0 }; result = ArrayUtils.And(a, b); Assert.IsTrue(Arrays.AreEqual(expected, result)); }
public void ProcessData(IMatrixData mdata, Parameters param, ref IMatrixData[] supplTables, ref IDocumentData[] documents, ProcessInfo processInfo) { const bool rows = true; int minValids = PerseusPluginUtils.GetMinValids(param, out bool percentage); ParameterWithSubParams <int> modeParam = param.GetParamWithSubParams <int>("Mode"); int modeInd = modeParam.Value; if (modeInd != 0 && mdata.CategoryRowNames.Count == 0) { processInfo.ErrString = "No grouping is defined."; return; } PerseusPluginUtils.ReadValuesShouldBeParams(param, out FilteringMode filterMode, out double threshold, out double threshold2); if (modeInd != 0) { int gind = modeParam.GetSubParameters().GetParam <int>("Grouping").Value; string[][] groupCol = mdata.GetCategoryRowAt(gind); if (param.GetParam <int>("Filter mode").Value == 2) { //discarded List <int> valids = new List <int>(); List <int> notvalids = new List <int>(); string[] groupVals = ArrayUtils.UniqueValuesPreserveOrder(groupCol); Array.Sort(groupVals); int[][] groupInds = CalcGroupInds(groupVals, groupCol); for (int i = 0; i < mdata.RowCount; i++) { int[] counts = new int[groupVals.Length]; int[] totals = new int[groupVals.Length]; for (int j = 0; j < groupInds.Length; j++) { for (int k = 0; k < groupInds[j].Length; k++) { if (groupInds[j][k] >= 0) { totals[groupInds[j][k]]++; } } if (PerseusPluginUtils.IsValid(mdata.Values.Get(i, j), threshold, threshold2, filterMode)) { for (int k = 0; k < groupInds[j].Length; k++) { if (groupInds[j][k] >= 0) { counts[groupInds[j][k]]++; } } } } bool[] groupValid = new bool[counts.Length]; for (int j = 0; j < groupValid.Length; j++) { groupValid[j] = PerseusPluginUtils.Valid(counts[j], minValids, percentage, totals[j]); } if (modeInd == 2 ? ArrayUtils.Or(groupValid) : ArrayUtils.And(groupValid)) { valids.Add(i); } else { notvalids.Add(i); } } supplTables = new[] { PerseusPluginUtils.CreateSupplTabSplit(mdata, notvalids.ToArray()) }; } NonzeroFilterGroup(minValids, percentage, mdata, param, modeInd == 2, threshold, threshold2, filterMode, groupCol); } else { if (param.GetParam <int>("Filter mode").Value == 2) { supplTables = new[] { PerseusPluginUtils.NonzeroFilter1Split(rows, minValids, percentage, mdata, param, threshold, threshold2, filterMode) }; } PerseusPluginUtils.NonzeroFilter1(rows, minValids, percentage, mdata, param, threshold, threshold2, filterMode); } }