from __future__ import division def GetAverage(mat): n=len(mat) m= width(mat) num = [0]*m for j in range(0,m): for i in mat: num[j]=num[j]+i[j] num[j]=num[j]/n return num def width(lst): i=0 for j in lst[0]: i=i+1 return i def GetVar(average,mat): ListMat=[] for i in mat: ListMat.append(list(map(lambda x: x[0]-x[1], zip(average, i)))) n=len(ListMat) m= width(ListMat) num = [0]*m for j in range(0,m): for i in ListMat: num[j]=num[j]+(i[j]*i[j]) num[j]=num[j]/n return num def DenoisMat(mat): average=GetAverage(mat) variance=GetVar(average,mat) section=list(map(lambda x: x[0]+x[1], zip(average, variance))) n=len(mat) m= width(mat) num = [0]*m denoisMat=[] for i in mat: for j in range(0,m): if i[j]>section[j]: i[j]=section[j] denoisMat.append(i) return denoisMat def AutoNorm(mat): n=len(mat) m= width(mat) MinNum=[9999999999]*m MaxNum = [0]*m for i in mat: for j in range(0,m): if i[j]>MaxNum[j]: MaxNum[j]=i[j] for p in mat: for q in range(0,m): if p[q]<=MinNum[q]: MinNum[q]=p[q] section=list(map(lambda x: x[0]-x[1], zip(MaxNum, MinNum))) print section NormMat=[] for k in mat: distance=list(map(lambda x: x[0]-x[1], zip(k, MinNum))) value=list(map(lambda x: x[0]/x[1], zip(distance,section))) NormMat.append(value) return NormMat
GetAverage(mat):返回均值
GetVar(average,mat):返回方差
DenoisMat(mat):去噪
AutoNorm(mat):归一化矩阵
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【机器学习算法-python实现】矩阵去噪以及归一化,布布扣,bubuko.com
原文地址:http://blog.csdn.net/buptgshengod/article/details/37557929