class KMean(object):
def __init__(self,k):
self._data=[]
self._normalizeData=[]
self._memeberOfClusters=[]
self._maxIteration=None
self._clusterCenters=[]
self._pointChangedNum=0
self._k=k
self._comment=[]
def _getColumMeanAndStd(self, column):
sumx=0.0
sumx2=0.0
for x in column:
sumx+=x
sumx2+=x**2
n=len(column)
mean=sum/n
d=sumx2/n-mean**2
std=pow(d,0.5)
return (round(mean,3),round(std,3))
def _getColumnMedian(self,column):
columncopy=list(column)
columncopy.sort()
clen=len(columncopy)
if clen%2==0:
return (columncopy[clen/2]+columncopy[clen/2-1])/2
else:
return columncopy[(clen-1)/2]
def normalizeOneColumn(self,column):
cmedian=self._getColumnMedian(column)
csum=sum([abs(x-cmedian) for x in column])*1.0
asd=round(csum/len(column),3)
result=[round((x-cmedian)/asd,3) for x in column]
return result