# AGAIN CONSIDER USING THE C++ VERSION SINCE IT IS ALOT FASTER
# HERE WE SPEED IT UP USING NUMBA IF THE USER HAS IT INSTALLED AS A MODULE
#
NN,MM=np.shape(dat)
KK,LL=np.shape(cent)
ifnotLL==MM:
print('WARNING DATA FORMAT ERROR. NON COALESCING COORDINATE AXIS')
labels=[int(z)forzinnp.zeros(NN)]
w=labels
counts=np.zeros(KK)
tmp_ce=np.zeros(KK*MM).reshape(KK,MM)
old_error,error,TOL=0.,1.,1.0E-10
whileabs(error-old_error)>TOL:
old_error=error
error=0.
counts=counts*0.
tmp_ce=tmp_ce*0.
# START BC
forhinrange(NN):
min_distance=1.0E30
foriinrange(KK):
distance=np.sum((dat[h]-cent[i])**2)
ifdistance<min_distance:
labels[h]=i
min_distance=distance
tmp_ce[labels[h]]+=dat[h]
counts[labels[h]]+=1.0
error+=min_distance
# END BC
foriinrange(KK):
ifcounts[i]>0:
cent[i]=tmp_ce[i]/counts[i]
centroids=cent
return(labels,centroids)
else:
defseeded_kmeans(dat,cent):
#
# SLOW SLUGGISH KMEANS WITH A DUBBLE FOR LOOP
# IN PYTHON! WOW! SUCH SPEED!
#
NN,MM=np.shape(dat)
KK,LL=np.shape(cent)
ifnotLL==MM:
print('WARNING DATA FORMAT ERROR. NON COALESCING COORDINATE AXIS')
labels=[int(z)forzinnp.zeros(NN)]
w=labels
counts=np.zeros(KK)
tmp_ce=np.zeros(KK*MM).reshape(KK,MM)
old_error,error,TOL=0.,1.,1.0E-10
whileabs(error-old_error)>TOL:
old_error=error
error=0.
counts=counts*0.
tmp_ce=tmp_ce*0.
# START BC
forhinrange(NN):
min_distance=1.0E30
foriinrange(KK):
distance=np.sum((dat[h]-cent[i])**2)
ifdistance<min_distance:
labels[h]=i
min_distance=distance
tmp_ce[labels[h]]+=dat[h]
counts[labels[h]]+=1.0
error+=min_distance
# END BC
foriinrange(KK):
ifcounts[i]>0:
cent[i]=tmp_ce[i]/counts[i]
centroids=cent
return(labels,centroids)
defconnectivity(B,val,bVerbose=True):
description="""
This is a cutoff based clustering algorithm. The intended use is to supply a distance matrix and a cutoff value (then becomes symmetric positive definite). For a small distance cutoff, you should see all the parts of the system and for a large distance cutoff, you should see the entire system. It has been employed for statistical analysis work as well as the original application where it was employed to segment molecular systems.
"""
# PYTHON ADAPTATION OF MY C++ CODE THAT CAN BE FOUND IN