I am currently using the following code to normalize data:
Public Function NormalizeData(values As Double()) As Double()
Dim min = values.Min
Dim max = values.Max
Dim outList(values.Count - 1) As Double
For i = 0 To values.Count - 1
outList(i) = (values(i) - min) / (max - min)
Next
Return outList
End Function
This returns values from 0 to 1 accordingly. I am confused on how to make this normalize between [1, -1] instead of [1, 0].
You already know how to normalise to a range of 0 to 1. If you want the range to be -1 to 1, you just need to multiply the normalised data by 2 and subtract 1.
As I said in my comment, you could just modify the statement inside your For loop to adjust the data:
outList(i) = 2 * (values(i) - min) / (max - min) - 1
The equivalent statement to denormalise data would be:
outList(i)= (values(i) + 1) * (max - min) / 2 + min
If you need to normalise and denormalise several arrays of data, I suggest creating a NormalData
class that can store the normalised data as well as the maximum and minimum values and has properties that will return either the normalised or denormalised values. Here is an example of such a class. You can create it using either
The NormalValues
and Values
properties return the normalised and denormalised data respectively.
Public Class NormalData
''' <summary>Create a NormalData object from and unnormalised array of values</summary>
''' <param name="values">Unnormalised values</param>
Sub New(values() As Double)
Minimum = values.Min
Maximum = values.Max
NormalValues = values.Select(Function(val) 2 * (val - Minimum) / (Maximum - Minimum) - 1).ToArray
End Sub
''' <summary>Create a NormalData object from and normalised array of values</summary>
''' <param name="normal">Normalised values</param>
''' <param name="min">Minimum value of unnormalised data</param>
''' <param name="max">Maximum value of unnormalised data</param>
Sub New(normal() As Double, min As Double, max As Double)
Minimum = min
Maximum = max
NormalValues = normal
End Sub
ReadOnly Property Minimum As Double
ReadOnly Property Maximum As Double
ReadOnly Property NormalValues As Double()
ReadOnly Property Values As Double()
Get
Return NormalValues.Select(Function(norm) (norm + 1) * (Maximum - Minimum) / 2 + Minimum).ToArray
End Get
End Property
End Class