Re: Custom distance function
- To: mathgroup at smc.vnet.net
- Subject: [mg72108] Re: Custom distance function
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 13 Dec 2006 06:38:21 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <eljbf3$7he$1@smc.vnet.net>
bentheo at gmail.com wrote:
> \!\(wcity[A_, B_]\ := \ â??\+\(i =
> 1\)\%7\((weights[\([i]\)]*\((Abs[A[\([i]\)] \
> - B[\([i]\)]])\))\)\)
>
> I was wondering if anyone could lend me a hand with a problem I'm
> having. I'm trying to use the standard clusteranalysis package with
> Mathematica. Now, I've defined a new distance function, which is
> essentially the standard manhattan distance function, but it uses
> weights. The function works on lists, and the documentation leads me to
> believe it is a legit function, but when I try to use it i get
> DistanceMatrix::optx: "Unknown option DistanceFuntion in \
> \!\(DistanceMatrix[{{226.3382425`, 0.286908795`, \[LeftSkeleton]4\
> \[RightSkeleton], 0.742291389`}, \[LeftSkeleton]9\[RightSkeleton], \
> \[LeftSkeleton]3353\[RightSkeleton]},
> \[LeftSkeleton]1\[RightSkeleton]]\). \!\
> \( \*ButtonBox[\(Moreâ?¦\), ButtonData:>\"General::optx\", \
> ButtonStyle->\"RefGuideLinkText\", ButtonFrame->\"None\"]\)
>
>
> Any ideas what I'm doing wrong?
>
I have just make up an example and your function /wcity/ seems to work
fine. (See below.)
In[1]:=
Needs["Statistics`ClusterAnalysis`"]
In[2]:=
wcity[A_, B_] := Sum[weights[[i]]*
Abs[A[[i]] - B[[i]]], {i, 1, 7}]
In[3]:=
weights = 1/Range[1, 7]
Out[3]=
{1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7}
In[4]:=
data = Table[Random[Integer, {1, 20}], {10}, {7}]
Out[4]=
{{10, 1, 5, 11, 4, 1, 18}, {1, 19, 7, 13, 14, 16, 2},
{20, 12, 20, 15, 5, 3, 13}, {19, 6, 4, 17, 10, 7,
19}, {19, 6, 2, 13, 19, 2, 13},
{5, 17, 17, 11, 20, 14, 11}, {19, 14, 18, 14, 4, 5,
1}, {19, 18, 16, 16, 16, 7, 16},
{9, 3, 8, 17, 13, 19, 7}, {15, 7, 6, 16, 18, 19, 5}}
In[5]:=
FindClusters[data, DistanceFunction -> wcity]
Out[5]=
{{{10, 1, 5, 11, 4, 1, 18}, {9, 3, 8, 17, 13, 19, 7},
{15, 7, 6, 16, 18, 19, 5}},
{{1, 19, 7, 13, 14, 16, 2}, {5, 17, 17, 11, 20, 14,
11}}, {{20, 12, 20, 15, 5, 3, 13},
{19, 6, 4, 17, 10, 7, 19}, {19, 6, 2, 13, 19, 2,
13}, {19, 14, 18, 14, 4, 5, 1},
{19, 18, 16, 16, 16, 7, 16}}}
Regards,
Jean-Marc