# FindClusters with custom DistanceFunction

Let we have an array of integers e.g. Range@365.
Each index has a specific value by which clusterization of integers should be done.
But these values cannot be calculated. It's WeatherData or something like this.
They are presented as a data List. So DistanceFunction is:

df = Abs[data[[#1]]-data[[#2]]]&


I also tried next form:

idf[i_?IntegerQ, j_?IntegerQ]:= Abs[data[[i]]-data[[j]]];


But anyway

FindClusters[Range@365, df]


or

FindClusters[Range@365, (idf[#1,#2])&]


not work, with unclear errors.

Mathematica's FindCluster knows various type of DistanceFunction. So if you want to use 1,2,...,365 as indices of data, try the following syntax:

 FindClusters[data -> Range[365]]


and let Mathematica select the best distance function. If you are not satisfied with the result, then, you can try tuning the distance function.

For example,

data = Most[
Last /@ Normal[
AirTemperatureData[
"KBOS", {{2000, 1, 1}, {2000, 12, 31}, "Day"}]]];

Length[data]
(* 365 *)

data // Short


FindClusters[data -> Range[365]]


Your syntax for the distance function isn't quite right. Define:

df[x_, y_] = Abs[x - y];


Then:

FindClusters[Range@365, 3, DistanceFunction -> df]


Finds 3 clusters using the defined df.