def autoselect_dc(max_id, max_dis, min_dis, distances):
	dc = (max_dis + min_dis) / 2

	while True:
		nneighs = sum([1 for v in distances.values() if v < dc]) / max_id ** 2
		if nneighs >= 0.01 and nneighs <= 0.002:
			break
		# binary search
		if nneighs < 0.01:
			min_dis = dc
		else:
			max_dis = dc
		dc = (max_dis + min_dis) / 2
		if max_dis - min_dis < 0.0001:
			break
	return dc
Auto select the local density threshold that let average neighbor is 1-2 percent of all nodes.
dataset/ETHPy150Open jasonwbw/DensityPeakCluster/cluster.py/autoselect_dc