public void StringMatrixExtensions_ToF64Matrix() { var stringMatrix = new StringMatrix(InputData, 2, 3); var actual = stringMatrix.ToF64Matrix(); Assert.AreEqual(new F64Matrix(InputData.Select(v => FloatingPointConversion.ToF64(v)).ToArray(), 2, 3), actual); }
public void FloatingPointConversion_ToF64() { var value = "1.2354236"; var actual = FloatingPointConversion.ToF64(value); Assert.AreEqual(1.2354236, actual); }
/// <summary> /// Parse array of feature importance values from text dump of XGBoost trees. /// </summary> /// <param name="textTrees"></param> /// <param name="numberOfFeatures"></param> /// <returns></returns> public static double[] ParseFromTreeDump(string[] textTrees, int numberOfFeatures) { var importanceType = new string[] { "gain=" }; var newLine = new string[] { "\n" }; var rawFeatureImportance = new double[numberOfFeatures]; foreach (var tree in textTrees) { var lines = tree.Split(newLine, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { if (!line.Contains("[") || line.Contains("booster")) { // Continue if line does not contain a split. continue; } // Extract feature name from string between [] var name = line.Split('[')[1].Split(']')[0].Split('<')[0]; // Extract featureIndex var featureIndex = int.Parse(name.Split('f')[1]); // extract gain or cover var gain = FloatingPointConversion.ToF64(line.Split(importanceType, StringSplitOptions.RemoveEmptyEntries)[1].Split(',')[0]); // add to featureImportance rawFeatureImportance[featureIndex] += gain; } } return(rawFeatureImportance); }
/// <summary> /// Parses the leaf value from a line. /// </summary> /// <param name="line"></param> /// <returns></returns> public static double ParseLeafValue(string line) { var valueLine = line.Split(m_leafSplit, StringSplitOptions.RemoveEmptyEntries)[1]; var valueString = valueLine.Split(',')[0]; var value = FloatingPointConversion.ToF64(valueString); return(value); }
/// <summary> /// Parses the split value from a line. /// </summary> /// <param name="line"></param> /// <returns></returns> public static double ParseSplitValue(string line) { // Extract feature value from string between [] var valueString = line.Split('[')[1].Split(']')[0].Split('<')[1]; // convert value. var value = FloatingPointConversion.ToF64(valueString); return(value); }
public void FloatingPointConversion_ToF64_to_from_custom_infinity() { var text = new string[] { "Inf", "beyond", "0.0", "75357.987" }; var nfi = new NumberFormatInfo() { PositiveInfinitySymbol = text[0], NegativeInfinitySymbol = text[1], }; var actual = text.Select(x => FloatingPointConversion.ToF64(x, converter: t => double.Parse(t, nfi))).ToArray(); var expected = new double[] { double.PositiveInfinity, double.NegativeInfinity, 0.0, 75357.987 }; CollectionAssert.AreEqual(expected, actual); }
/// <summary> /// Converts am array of string to an array of floats /// </summary> /// <param name="v"></param> /// <returns></returns> public static double[] AsF64(this string[] v) { return(v.Select(s => FloatingPointConversion.ToF64(s)).ToArray()); }
static double ToF64(string value) { return(FloatingPointConversion.ToF64(value)); }
public void FloatingPointConversion_ToF64_Unable_To_Parse() { var value = "infinity12"; FloatingPointConversion.ToF64(value); }