public static MetaNum Min(this IList <MetaNum> self) { if (self.Count == 0) { return(TypeTrait <MetaNum> .GetNaNOrRaiseException("No elements")); } var i = 0; while (TypeTrait <MetaNum> .IsNaN(self[0]) && i < self.Count - 1) { ++i; } MetaNum min = self[i]; for (++i; i < self.Count; ++i) { if (self[i] < min) { min = self[i]; } } return(min); }
public static MetaFloat Correlation(this IList <MetaNum> self, IList <MetaNum> other, bool skipNaN = true) { if (self.Count != other.Count) { return(MetaFloat.NaN); } if (skipNaN) { var s1 = new List <MetaNum>(self.Count); var s2 = new List <MetaNum>(self.Count); for (var i = 0; i < self.Count; ++i) { if (TypeTrait <MetaNum> .IsNaN(self[i]) || TypeTrait <MetaNum> .IsNaN(other[i])) { continue; } s1.Add(self[i]); s2.Add(other[i]); } return(Covariance(s1, s2) / s1.StandardDeviation() / s2.StandardDeviation()); } return(Covariance(self, other) / self.StandardDeviation() / other.StandardDeviation()); }
private Dictionary <Tuple <T1, T2>, int> CollectKeys2 <T1, T2>() { Debug.Assert(_keyColumns.Length == 2); var column1 = _dataMap[_keyColumns[0]].UnderlyingList; var column2 = _dataMap[_keyColumns[1]].UnderlyingList; var rowCount = _dataMap.MaxRowCount; var result = new Dictionary <Tuple <T1, T2>, int>(rowCount); for (var i = 0; i < rowCount; ++i) { T1 k1 = i >= column1.Count ? TypeTrait <T1> .GetNaN() : (T1)column1[i]; T2 k2 = i >= column2.Count ? TypeTrait <T2> .GetNaN() : (T2)column2[i]; var key = Tuple.Create(k1, k2); try { result.Add(key, i); } catch (ArgumentException ex) { throw new ArgumentException($"Duplicate key values: {key}", ex); } } return(result); }
public static int ArgMin(this IList <MetaNum> self) { if (self.Count == 0) { throw new InvalidCastException("No elements"); } var i = 0; while (TypeTrait <MetaNum> .IsNaN(self[0]) && i < self.Count) { ++i; } if (i == self.Count) { throw new InvalidCastException("No elements"); } MetaNum min = self[i]; int argmin = i; for (++i; i < self.Count; ++i) { if (self[i] < min) { min = self[i]; argmin = i; } } return(argmin); }
public void TestStringIsNaN() { Assert.False(TypeTrait <string> .IsNaN(" xxx ")); Assert.True(TypeTrait <string> .IsNaN(null)); Assert.True(TypeTrait <string> .IsNaN(string.Empty)); Assert.True(TypeTrait <string> .IsNaN("")); Assert.True(TypeTrait <string> .IsNaN("\t ")); }
public static void FillNaNFill(this IList <MetaNum> self, MetaNum fillValue) { for (var i = 0; i < self.Count; ++i) { if (TypeTrait <MetaNum> .IsNaN(self[i])) { self[i] = fillValue; } } }
public static void FillNaNFill <T>(this IList <T> self, T fillValue) { for (var i = 0; i < self.Count; ++i) { if (TypeTrait <T> .IsNaN(self[i])) { self[i] = fillValue; } } }
public static IList <bool> IsNaN <T>(this IList <T> self) { var result = new List <bool>(self.Count); foreach (var item in self) { result.Add(TypeTrait <T> .IsNaN(item)); } return(result); }
public static DataMap FromJagged <T>(T[][] source, string[] columnNames = null, bool transpose = false, IEqualityComparer <string> keyComparaer = null) { var result = new DataMap(keyComparaer); if (!transpose) { if (columnNames == null) { var c = source[0].Length; columnNames = new string[c]; for (var i = 0; i < c; ++i) { columnNames[i] = "Column" + i; } } for (var i = 0; i < columnNames.Length; ++i) { var data = new List <T>(source.Length); result.Add(columnNames[i], data); for (var j = 0; j < source.Length; ++j) { var row = source[j]; if (i < row.Length) { data.Add(row[i]); } else { data.Add(TypeTrait <T> .GetNaN()); } } } } else { if (columnNames == null) { var c = source.Length; columnNames = new string[c]; for (var i = 0; i < c; ++i) { columnNames[i] = "Column" + i; } } for (var i = 0; i < columnNames.Length; ++i) { result.Add(columnNames[i], source[i]); } } return(result); }
public static int CountNaN(this IList <MetaNum> self) { int count = 0; foreach (var value in self) { if (TypeTrait <MetaNum> .IsNaN(value)) { ++count; } } return(count); }
public static List <MetaNum> RemoveNaN(this IList <MetaNum> self) { var result = new List <MetaNum>(self.Count); foreach (var value in self) { if (!TypeTrait <MetaNum> .IsNaN(value)) { result.Add(value); } } return(result); }
public static MetaNum Mode(this IList <MetaNum> self, bool skipNaN = true) { if (self.Count == 0) { return(TypeTrait <MetaNum> .GetNaNOrRaiseException("No elements")); } var values = self.ToArray(); Array.Sort(values); MetaNum currentValue = values[0]; MetaNum bestValue = currentValue; var currentCount = 1; var bestCount = 1; int i = 1; if (skipNaN) { // After sort, NaNs should be collected to the first location of the sequence. while (TypeTrait <MetaNum> .IsNaN(values[i])) { ++i; } } for (; i < values.Length; ++i) { if (currentValue == values[i]) { currentCount += 1; } else { currentValue = values[i]; currentCount = 1; } if (currentCount > bestCount) { bestCount = currentCount; bestValue = currentValue; } } return(bestValue); }
public double getRadiusRulesInsideCircleZodiac(TypeTrait typeTrait) { double divTrait = 0.0; switch (typeTrait) { case TypeTrait.Petit: divTrait = 1.0 + DIVTRAITPETIT; break; case TypeTrait.Grand: divTrait = 1.0 + DIVTRAITGRAND; break; } return((getRadiusTotal() * (((CIRCLE1 - CIRCLE0) / divTrait) + CIRCLE0)) / 100); // - CIRCLE1 }
public static MetaFloat Covariance(this IList <MetaNum> self, IList <MetaNum> other, bool unbiased = true, bool skipNaN = true) { if (self.Count != other.Count) { return(MetaFloat.NaN); } var mean0 = self.Mean(skipNaN); var mean1 = other.Mean(skipNaN); if (MetaFloat.IsNaN(mean0) || MetaFloat.IsNaN(mean1)) { return(MetaFloat.NaN); } int actualCount = self.Count; MetaFloat c = (MetaFloat)0.0; for (int i = 0; i < self.Count; ++i) { if (TypeTrait <MetaNum> .IsNaN(self[i]) || TypeTrait <MetaNum> .IsNaN(other[i])) { if (skipNaN) { --actualCount; continue; } else { return(MetaFloat.NaN); } } var a = (MetaFloat)self[i] - mean0; var b = (MetaFloat)other[i] - mean1; c += a * b; } if (unbiased) { return(c / (actualCount - 1)); } else { return(c / actualCount); } }
public static MetaFloat Variance(this IList <MetaNum> self, bool unbiased = true, bool skipNaN = true) { if (self.Count == 0) { return(MetaFloat.NaN); } MetaFloat mean = Mean(self, skipNaN); if (MetaFloat.IsNaN(mean)) { return(MetaFloat.NaN); } MetaFloat variance = (MetaFloat)0.0; int actualCount = self.Count; foreach (var value in self) { MetaFloat v = (MetaFloat)value; if (TypeTrait <MetaFloat> .IsNaN(v)) { if (skipNaN) { --actualCount; continue; } else { return(MetaFloat.NaN); } } MetaFloat x = v - mean; variance += x * x; } if (unbiased) { return(variance / (actualCount - 1)); } else { return(variance / actualCount); } }
private Dictionary <T1, int> CollectKeys1 <T1>() { Debug.Assert(_keyColumns.Length == 1); var column1 = _dataMap[_keyColumns[0]].UnderlyingList; var rowCount = _dataMap.MaxRowCount; var result = new Dictionary <T1, int>(rowCount); for (var i = 0; i < rowCount; ++i) { T1 k1 = i >= column1.Count ? TypeTrait <T1> .GetNaN() : (T1)column1[i]; result.Add(k1, i); } return(result); }
public static List <MetaNum> FillNaN(this IList <MetaNum> self, MetaNum fillValue) { var result = new List <MetaNum>(self.Count); foreach (var value in self) { if (TypeTrait <MetaNum> .IsNaN(value)) { result.Add(fillValue); } else { result.Add(value); } } return(result); }
// Histogram private static Tuple <int[], int> CollectHistogram(IList <MetaNum> data, HistogramInterval intervals) { var result = new int[intervals.BinCount]; var total = 0; foreach (var value in data) { if (TypeTrait <MetaNum> .IsNaN(value)) { continue; } var bin = (int)Math.Floor(((double)value - intervals.AdjustedLower) / intervals.BinWidth); ++result[bin]; ++total; } return(Tuple.Create(result, total)); }
private Dictionary <Tuple <T1, T2, T3, T4, T5, T6>, int> CollectKeys6 <T1, T2, T3, T4, T5, T6>() { Debug.Assert(_keyColumns.Length == 6); var column1 = _dataMap[_keyColumns[0]].UnderlyingList; var column2 = _dataMap[_keyColumns[1]].UnderlyingList; var column3 = _dataMap[_keyColumns[2]].UnderlyingList; var column4 = _dataMap[_keyColumns[3]].UnderlyingList; var column5 = _dataMap[_keyColumns[4]].UnderlyingList; var column6 = _dataMap[_keyColumns[5]].UnderlyingList; var rowCount = _dataMap.MaxRowCount; var result = new Dictionary <Tuple <T1, T2, T3, T4, T5, T6>, int>(rowCount); for (var i = 0; i < rowCount; ++i) { T1 k1 = i >= column1.Count ? TypeTrait <T1> .GetNaN() : (T1)column1[i]; T2 k2 = i >= column2.Count ? TypeTrait <T2> .GetNaN() : (T2)column2[i]; T3 k3 = i >= column3.Count ? TypeTrait <T3> .GetNaN() : (T3)column3[i]; T4 k4 = i >= column4.Count ? TypeTrait <T4> .GetNaN() : (T4)column4[i]; T5 k5 = i >= column5.Count ? TypeTrait <T5> .GetNaN() : (T5)column5[i]; T6 k6 = i >= column6.Count ? TypeTrait <T6> .GetNaN() : (T6)column6[i]; var key = Tuple.Create(k1, k2, k3, k4, k5, k6); try { result.Add(key, i); } catch (ArgumentException ex) { throw new ArgumentException($"Duplicate key values: {key}", ex); } } return(result); }
public static MetaNum Sum(this IList <MetaNum> self, bool skipNaN = true, int minCount = 0) { MetaNum sum = (MetaNum)0.0; int count = 0; foreach (var value in self) { if (skipNaN && TypeTrait <MetaNum> .IsNaN(value)) { continue; } sum += value; ++count; } if (count < minCount) { return(TypeTrait <MetaNum> .GetNaN()); } return(sum); }
public static MetaNum Product(this IList <MetaNum> self, bool skipNaN = true, int minCount = 0) { MetaNum product = (MetaNum)1; int count = 0; foreach (var value in self) { if (skipNaN && TypeTrait <MetaNum> .IsNaN(value)) { continue; } product *= value; ++count; } if (count < minCount) { return(TypeTrait <MetaNum> .GetNaN()); } return(product); }
public override DataMap TransformToDataMap(SeriesBase data) { var listMap = new Dictionary <string, T[]>(); var size = data.Count; string firstKey = null; foreach (DictionaryEntry entry in _encoding) { if (firstKey == null && _oneHotType != OneHotType.OneHot) { firstKey = entry.Key as string; continue; } listMap[entry.Key as string] = new T[size]; } int i = 0; switch (_oneHotType) { case OneHotType.OneHot: foreach (var e in data) { var name = e.ToString(); if (listMap.TryGetValue(name, out var list)) { list[i] = TypeTrait <T> .GetOne(); } ++i; } break; case OneHotType.DropFirst: foreach (var e in data) { var name = e.ToString(); if (name != firstKey && listMap.TryGetValue(name, out var list)) { list[i] = TypeTrait <T> .GetOne(); } ++i; } break; case OneHotType.EffectCoding: foreach (var e in data) { var name = e.ToString(); if (name != firstKey) { if (listMap.TryGetValue(name, out var list)) { list[i] = TypeTrait <T> .GetOne(); } } else { foreach (var entry in listMap) { entry.Value[i] = TypeTrait <T> .GetOne(); } } ++i; } break; } var result = new DataMap(); var keys = listMap.Keys.ToArray(); Array.Sort(keys); foreach (var k in keys) { var columnName = string.Format(_columnNameFormat, k); result[columnName] = listMap[k]; } return(result); }
public void TestDoubleIsNaN() { Assert.True(TypeTrait <double> .IsNaN(double.NaN)); Assert.False(TypeTrait <double> .IsNaN(10.2)); }
public void TestDateTimeIsNaN() { Assert.False(TypeTrait <DateTime> .IsNaN(DateTime.Now)); }