我目前正在使用以下代码来规范化数据:
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
相应地返回从0到1的值。我对如何在[1,-1]而不是[1,0]之间进行归一化感到困惑。
答案 0 :(得分:1)
您已经知道如何将范围归一化为0到1.如果希望范围为-1到1,则只需将规范化数据乘以2并减去1。
正如我在评论中所说,你可以修改For循环中的语句来调整数据:
outList(i) = 2 * (values(i) - min) / (max - min) - 1
反规范化数据的等效语句是:
outList(i)= (values(i) + 1) * (max - min) / 2 + min
如果你需要对几个数据数组进行规范化和非规范化,我建议创建一个NormalData
类,它可以存储规范化数据以及最大值和最小值,并具有将返回规范化或非规范化的属性。值。这是这样一个类的一个例子。您可以使用
NormalValues
和Values
属性分别返回规范化和非规范化数据。
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