-
大小: 4KB文件類型: .py金幣: 1下載: 0 次發(fā)布日期: 2021-05-09
- 語言: Python
- 標(biāo)簽: Clustering??Python??Methodology??
資源簡介
Rodriguez A, Laio A. Clustering by fast search and find of density peaks[J]. Science, 2014, 344(6191): 1492-1496.基于這篇文章實(shí)現(xiàn)的最基本的密度聚類的算法,具體請看我博客中的相關(guān)文章http://blog.csdn.net/kryolith/article/details/39832573
代碼片段和文件信息
import?numpy?as?np
import?matplotlib.pyplot?as?plt
from?sklearn.datasets?import?make_blobs
def?distanceNorm(NormD_value):
#?initialization
#?Norm?for?distance
if?Norm?==?‘1‘:
counter?=?np.absolute(D_value);
counter?=?np.sum(counter);
elif?Norm?==?‘2‘:
counter?=?np.power(D_value2);
counter?=?np.sum(counter);
counter?=?np.sqrt(counter);
elif?Norm?==?‘Infinity‘:
counter?=?np.absolute(D_value);
counter?=?np.max(counter);
else:
raise?Exception(‘We?will?program?this?later......‘);
return?counter;
def?chi(x):
if?x?0:
return?1;
else:
return?0;
def?fit(featureslabelstdistanceMethod?=?‘2‘):
#?initialization
distance?=?np.zeros((len(labels)len(labels)));
distance_sort?=?list();
density?=?np.zeros(len(labels));
distance_higherDensity?=?np.zeros(len(labels));
#?compute?distance
for?index_i?in?xrange(len(labels)):
for?index_j?in?xrange(index_i+1len(labels)):
D_value?=?features[index_i]?-?features[index_j];
distance[index_iindex_j]?=?distanceNorm(distanceMethodD_value);
distance_sort.append(distance[index_iindex_j]);
distance?+=?distance.T;
#?compute?optimal?cutoff
distance_sort?=?np.array(distance_sort);
cutoff?=?int(np.round(distance_sort[len(distance_sort)?*?t]));
#?computer?density
for?index_i?in?xrange(len(labels)):
distance_cutoff_i?=?distance[index_i]?-?cutoff;
for?index_j?in?xrange(1len(labels)):
density[index_i]?+=?chi(distance_cutoff_i[index_j]);
#?search?for?the?max?density
Max?=?np.max(density);
MaxIndexList?=?list();
for?index_i?in?xrange(len(labels)):
if?density[index_i]?==?Max:
MaxIndexList.extend([index_i]);
#?computer?distance_higherDensity
Min?=?0;
for?index_i?in?xrange(len(labels)):
if?index_i?in?MaxIndexList:
distance_higherDensity[index_i]?=?np.max(distance[index_i]);
continue;
else:
Min?=?np.max(distance[index_i]);
for?index_j?in?xrange(1len(labels)):
if?density[index_i]? Min?=?distance[index_iindex_j];
else:
continue;
distance_high
評論
共有 條評論