Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am learning how to cluster large datasets and this maybe a newbie question, but I haven’t found a suitable answer in the Documentation or on this board.

My question is best illustrated by an example. Let’s import some data:

irisData = Import["", "CSV”];
(*Column labels *)
pLabels = {"Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Type”};

How do I construct clusters based on only two columns say “Sepal.Length” and “Sepal.Width” and still retain the rest of the data… as part of the clusters

Ideally I would like a function:


that would cluster the data into n clusters using the data in the columnlist such that output data format of the clusters is the same as the input data.

I can find the clusters and plot them as:

clusters = {cluster1, cluster2, cluster3} = FindClusters[irisData[[All, 1 ;; 2]], 3];
ListPlot[{{#1, #2} & @@@ cluster1, {#1, #2} & @@@ cluster2, {#1, #2} & @@@ cluster3}, AspectRatio -> 1, ImageSize -> 200, FrameLabel -> pLabels[[1 ;; 2]]]

But I’d like to use the clusters that I found using FindClusters above to explore say “Sepal.Length” vs “Petal.Width” or some other data analysis.

share|improve this question
up vote 3 down vote accepted

You should use one of the syntax options for FindClusters involving rules. When clustering your dataset, transform it to {data to cluster} -> {data to return} format at the level of either individual elements or the whole list. The details are explained in the documentation.

For example, to cluster on columns 1 (sepal length) and 4 (petal width):

irisData = Import["", "CSV"];
sample = irisData[[All, {1, 4}]] -> irisData;
sample[[All, 1]]

{5.1, 0.2} -> {5.1, 3.5, 1.4, 0.2, "setosa"}

Then of course

% // Length


%%[[1, 1]]

{5.1, 3.5, 1.4, 0.2, "setosa"}

share|improve this answer
This works well. Thanks for taking time out to post! – Pam Jul 15 '14 at 18:21

This is based on mfvonh’s answer. I thought I would flush out the full answer for posterity.

Let’s cluster the data based on Sepal Length and Sepal Width

 sample = irisData[[All, {1, 2}]] -> irisData;
 clusters = {cluster1, cluster2, cluster3} = FindClusters[sample, 3];

Let’s plot the various characteristics of the clusters:

{ListPlot[({#1, #2} &  @@@ clusters[[#]]) & /@ Range[3], 
FrameLabel -> pLabels[[1 ;; 2]] ]
, ListPlot[({#2, #3} &  @@@ clusters[[#]]) & /@ Range[3], 
FrameLabel -> pLabels[[2 ;; 3]] ]} // List // Grid

enter image description here

share|improve this answer

Is this what you want?

pLabels = {"Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Type"};

IrisPlot[x_, y_] :=
 ListPlot[FindClusters[irisData[[All, {x, y}]], 3],
  AspectRatio -> 1, ImageSize -> 400, Frame -> True, 
  FrameLabel -> pLabels[[{x, y}]]]

IrisPlot[2, 3]

enter image description here

share|improve this answer
This is not what I want. I want to cluster using Petal Length and Petal Width fields, but for the clusters that I’ve computed plot the the Petal Length vs Sepal Width… – Pam Jul 15 '14 at 18:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.