/// <summary> /// Normalizes features within the specified range (min, max) /// </summary> /// <param name="matrix"></param> /// <param name="output"></param> public void Transform(F64Matrix matrix, F64Matrix output) { var rows = matrix.RowCount; var cols = matrix.ColumnCount; if (m_featureMinMax.Count == 0) { for (int i = 0; i < cols; i++) { m_featureMinMax.Add(i, new FeatureMinMax { Min = double.MaxValue, Max = double.MinValue }); } } CreateFeatureMinMax(matrix); for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { var value = matrix[j, i]; var minMax = m_featureMinMax[i]; var newValue = m_normalizer.Normalize(m_min, m_max, minMax.Min, minMax.Max, value); output[j, i] = newValue; } } }
public void LinearNormalizer_Normalize() { var oldMin = -213.0; var oldMax = 2345.0; var newMin = -1.0; var newMax = 1.0; var sut = new LinearNormalizer(); Assert.AreEqual(1.0, sut.Normalize(newMin, newMax, oldMin, oldMax, oldMax), 0.000001); Assert.AreEqual(-1.0, sut.Normalize(newMin, newMax, oldMin, oldMax, oldMin), 0.000001); Assert.AreEqual(-0.833463643471462, sut.Normalize(newMin, newMax, oldMin, oldMax, 0.0), 0.000001); Assert.AreEqual(0.730258014073495, sut.Normalize(newMin, newMax, oldMin, oldMax, 2000.0), 0.000001); }