Re: FindClusters & BarCharts

• To: mathgroup at smc.vnet.net
• Subject: [mg88542] Re: [mg88488] FindClusters & BarCharts
• From: Darren Glosemeyer <darreng at wolfram.com>
• Date: Thu, 8 May 2008 04:12:01 -0400 (EDT)
• References: <200805061043.GAA23024@smc.vnet.net>

```Namrata Khemka wrote:
> Hi everyone,
> I have the following list:
> list = {{2, 4, 2, 2, 9}, {7, 2, 8, 5, 4}, {2, 1, 4, 5, 3}}
>
>
> I would like to find clusters in the above list:
>
> tmpVal = 3;
>
> temp = FindClusters[Cases[Flatten[Sort[list]], Except[tmpVal]]]
>
>
> This gives me: {{2, 1, 2, 2, 2, 2}, {4, 4, 4}, {5, 5}, {9, 7, 8}} which is
> correct. However, I do want one of the clusters to include the value of 3
> and get something as follows:
>
> {{2, 1, 2, 2, 2, 2}, {4, 4, 4}, {5, 5}, {9, 7, 8}, {3}}
>
>
> If I change tmpVal = 2 I get something that is not desired:
>
> tmpVal=2
>
> temp = FindClusters[Cases[Flatten[list], Except[value]]]
>
> This gives me: {{4, 5, 4, 1, 4, 5, 3}, {9, 7, 8}}.
>
> However, I would like the output to be somewhat as the following (In other
> words
>
> {{1}, {2,2,2,2,2}, {3,4,5}, {7,8,9}}
>
>
> Similarly, if I change the variable value=8, I get:
>
> {{2, 2, 2, 2, 2, 1, 3}, {4, 5, 4, 4, 5}, {9, 7}}
>
> but would like the output to include:
>
> {{2, 2, 2, 2, 2, 1, 3}, {4, 5, 4, 4, 5}, {7}, {8}, {9}}
>
>
> In other words, split the list up at the value and then
>
>
>
> I would like to plot the above in a BarChart:
>
> Needs["BarCharts`"]
>
> BarChart[Map[Length[#] &, temp]]
>
> I would like the bars to have the labels as the minimum and maximum for each
> of the lists. For instance:
>
> {{4, 5, 4, 1, 4, 5, 3}, {9, 7, 8}, {2,2,2,2,2}}
>
> The first bar would be labeled 1-5
>
>

Here's an example that takes the clustering for the entire data set and
breaks up the cluster containing the value of interest.

In[1]:= list = {{2, 4, 2, 2, 9}, {7, 2, 8, 5, 4}, {2, 1, 4, 5, 3}};

In[2]:= clusters = FindClusters[Flatten[Sort[list]]]

Out[2]= {{2, 1, 4, 5, 3, 2, 4, 2, 2, 2, 5, 4}, {9, 7, 8}}

The following will sort each cluster and break the cluster containing
tempval into clusters containing tempval, containing values less than
tempval, and containing values greater than tempval.

In[3]:= f[tempval_] :=
Flatten[Map[
Split[Sort[#], (#1 === #2 ===
tempval || (#1 =!= tempval && #2 =!= tempval)) &] &,
clusters], 1]

Here are the examples for 3, 2, and 8.

In[4]:= f[3]

Out[4]= {{1, 2, 2, 2, 2, 2}, {3}, {4, 4, 4, 5, 5}, {7, 8, 9}}

In[5]:= f[2]

Out[5]= {{1}, {2, 2, 2, 2, 2}, {3, 4, 4, 4, 5, 5}, {7, 8, 9}}

In[6]:= f[8]

Out[6]= {{1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 5, 5}, {7}, {8}, {9}}

In[7]:= Needs["BarCharts`"]

labelfun will be used to create the bar labels. It uses min-max for
clusters containing more than one value, and just the value if there is
only one distinct value in the cluster.

In[8]:= labelfun[xx_] :=
Block[{min,
max},(*use the fact that the data from f will already be sorted*)
min = First[xx];
max = Last[xx];
If[min === max, ToString[min],
ToString[min] <> "-" <> ToString[max]]]

In[9]:= With[{vals = f[3]},
BarChart[Map[Length[#] &, vals], BarLabels -> Map[labelfun, vals]]]

Out[9]= -Graphics-

Darren Glosemeyer
Wolfram Research

```

• Prev by Date: Re: Pattern matching problem
• Next by Date: Re: substitution within a substitution list
• Previous by thread: FindClusters & BarCharts
• Next by thread: Re: Re: smart change of variables?