DataSharp/Data/Analysis/MachineLearningUtil.cs
/// <summary>
/// Returns an array of Vector that contains all the normalized Vector correspond to original Vector.
/// </summary>
/// <param name="vectors"></param>
/// <returns></returns>
Returns an array of Vector that contains all the normalized Vector correspond to original Vector.
Returns an array of Vector that contains all the normalized Vector correspond to original Vector .
public static (Vector[] normalizedVector, Vector min, Vector ranges) Normalize(Vector[] vectors)
        {
            if (vectors == null)
                throw new ArgumentNullException("vectors", "The input array of Vector is null.");
            for (int i = 0; i < vectors.Length; i++)
            {
                if (vectors[i] == null)
                    throw new ArgumentNullException($"vectors[{i}]", "There is an entry for Vector has null value.");
            }
            int vectorLength = vectors[0].Count;
            Vector min = new Vector(vectorLength);
            Vector max = new Vector(vectorLength);
            for (int i = 0; i < vectorLength; i++)
            {
                min[i] = vectors[0][i];
                max[i] = vectors[0][i];
            }
            for (int i = 1; i < vectors.Length; i++)
            {
                for (int j = 0; j < vectorLength; j++)
                {
                    if (vectors[i][j] < min[j])
                        min[j] = vectors[i][j];
                    else if (vectors[i][j] > max[j])
                        max[j] = vectors[i][j];
                }
            }
            Vector ranges = max - min;
            Vector[] normalizedVector = new Vector[vectors.Length];
            for (int i = 0; i < vectors.Length; i++)
            {
                normalizedVector[i] = new Vector(vectorLength);
                for (int j = 0; j < vectorLength; j++)
                    normalizedVector[i][j] = (vectors[i][j] - min[j]) / ranges[j];
            }
            return (normalizedVector, min, ranges);
        }
public static ( Vector [ ] normalizedVector , Vector min , Vector ranges ) Normalize ( Vector [ ] vectors ) { if ( vectors == null ) throw new ArgumentNullException ( " " , " " ) ; for ( int i = 0 ; i < vectors . Length ; i ++ ) { if ( vectors [ i ] == null ) throw new ArgumentNullException ( $" vectors[ { i } ] " , " " ) ; } int vectorLength = vectors [ 0 ] . Count ; Vector min = new Vector ( vectorLength ) ; Vector max = new Vector ( vectorLength ) ; for ( int i = 0 ; i < vectorLength ; i ++ ) { min [ i ] = vectors [ 0 ] [ i ] ; max [ i ] = vectors [ 0 ] [ i ] ; } for ( int i = 1 ; i < vectors . Length ; i ++ ) { for ( int j = 0 ; j < vectorLength ; j ++ ) { if ( vectors [ i ] [ j ] < min [ j ] ) min [ j ] = vectors [ i ] [ j ] ; else if ( vectors [ i ] [ j ] > max [ j ] ) max [ j ] = vectors [ i ] [ j ] ; } } Vector ranges = max - min ; Vector [ ] normalizedVector = new Vector [ vectors . Length ] ; for ( int i = 0 ; i < vectors . Length ; i ++ ) { normalizedVector [ i ] = new Vector ( vectorLength ) ; for ( int j = 0 ; j < vectorLength ; j ++ ) normalizedVector [ i ] [ j ] = ( vectors [ i ] [ j ] - min [ j ] ) / ranges [ j ] ; } return ( normalizedVector , min , ranges ) ; }
Returns an array of Vector that contains all the normalized Vector correspond to original Vector.
Returns an array of Vector that contains all the normalized Vector correspond to original Vector .
None
vectors
Vector[]
None