多维度数据的归一化代码

多维度数据的归一化代码

在神经网络分析时,需要对多维度的信息进行归一化,下面的python代码实现了此功能,并且实现了反向归一化。过程中使用了numpy库。此归一化仅仅将数据全部压缩在(0,1)区间之内。使用者可以根据实际情况修改

import numpy as np 

def normalize(array:np.ndarray,ax:int = 0):
    """ normalize the matrix to (0,1), r.s.t A axis (Default=0)
        return normalized matrix and a record matrix for normalize back
    """
    arraySize=array.shape
    remianDimHors=int(np.prod(arraySize)/arraySize[ax])
    indexTranspose=list(range(len(arraySize)))
    indexTranspose[0],indexTranspose[ax]=indexTranspose[ax],indexTranspose[0]
    arrayTranspose=array.transpose(tuple(indexTranspose))
    sizeTranspose=arrayTranspose.shape
    arrayReshape=arrayTranspose.reshape(arraySize[ax],remianDimHors)
    normalizeFactor=np.zeros((2,remianDimHors))
    for i in range(remianDimHors):
        adjustData=arrayReshape[:,i]
        minValue=min(adjustData)
        maxValue=max(adjustData)

        arrayReshape[:,i]=(adjustData-minValue)/(maxValue-minValue)

        normalizeFactor[0,i]=minValue
        normalizeFactor[1,i]=maxValue
        
    arrayReshapeBack=arrayReshape.reshape(sizeTranspose)
    normalizeBackShape=list(sizeTranspose);normalizeBackShape[0]=2
    normalizeFactor=normalizeFactor.reshape(normalizeBackShape)
    arrayFinal=arrayReshapeBack.transpose(indexTranspose)
    normalizeFactor=normalizeFactor.transpose(indexTranspose)
    return arrayFinal,normalizeFactor

def normalizeBack(arrayNormalized:np.ndarray,normalizeFactor:np.ndarray):
    """ normalize back the normalizedArray to what original is it
        input: arrayNormalized, recordMaxtrix
    """
    sizeArrayNormalized=arrayNormalized.shape
    sizeNormalizeFactor=normalizeFactor.shape

    normalizeAx=0
    for i in range(len(sizeNormalizeFactor)):
        if sizeNormalizeFactor[i] != sizeArrayNormalized[i]:
            normalizeAx=i

    remianDimHors=int(np.prod(sizeArrayNormalized)/sizeArrayNormalized[normalizeAx])
    indexTranspose=list(range(len(sizeArrayNormalized)))
    indexTranspose[0],indexTranspose[normalizeAx]=indexTranspose[normalizeAx],indexTranspose[0]
    arrayTranspose=arrayNormalized.transpose(tuple(indexTranspose))
    normalizeFactorTranspose=normalizeFactor.transpose(tuple(indexTranspose))
    sizeTranspose=arrayTranspose.shape
    arrayReshape=arrayTranspose.reshape(sizeArrayNormalized[normalizeAx],remianDimHors)
    normalizeFactorReshape=normalizeFactorTranspose.reshape(sizeNormalizeFactor[normalizeAx],remianDimHors)

    for i in range(remianDimHors):
        adjustData=arrayReshape[:,i]
        maxValue=normalizeFactorReshape[1,i]
        minValue=normalizeFactorReshape[0,i]
        arrayReshape[:,i]=adjustData*(maxValue-minValue)+minValue

        
    arrayReshapeBack=arrayReshape.reshape(sizeTranspose)
    arrayFinal=arrayReshapeBack.transpose(indexTranspose)
    return arrayFinal

#  testArray=np.linspace(1,20,5)
#  testArray=np.expand_dims(testArray,1).repeat(3,axis=1)
#  testArray[:,1] = np.sqrt(testArray[:,1])
#  testArray[:,2] = np.sqrt(testArray[:,2])+5
#  testArray=np.expand_dims(testArray,2).repeat(3,axis=2)
#  testArray[:,:,1] = np.sqrt(testArray[:,:,1])
#  testArray[:,:,2] = np.sqrt(testArray[:,:,2])+5
#  print(testArray[1,:,:])
#  print("----------------")
#  (array,factor)=normalize(testArray,2)
#  print(array[1,:,:])
#  print("----------------")
#  print(factor[1,:,:])
#  print("----------------")
#  print("Bacccccccck!")
#  or_array=normalizeBack(array,factor)
#  print(or_array[1,:,:])

代码为本人原创,转载需要注明出处

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值