-5
$\begingroup$

I have a big distance matrix in a file and I want to do clustering and plot dendgoram. I've tried:

Needs["HierarchicalClustering`"]      
      DendrogramPlot[
        DirectAgglomerate[
         Import["C:\\here_path\\distance_matrix.txt"]
        ]
      ]

But unfortunatelly it returns:

DendrogramPlot[DirectAgglomerate["{
  {0.0, 0.051, 0.072243, 2.397, 0.11146, 1.23, 1,21, 0.451, \
0.0901, 1.466, 0.24518, 0.5529},
   next rows of matrix.. }"]]

And I have to copy matrix to DirectAgglomerate and then copy output to DedogramPlot, because I can even combine those two commands.

Edit:

Sample of matrix here at pastebin

$\endgroup$

closed as off-topic by m_goldberg, MarcoB, Sascha, Mr.Wizard Jan 23 '17 at 8:04

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Mr.Wizard
  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, MarcoB, Sascha
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ What is the data format in your text file? It seems that Import is returning a string, rather than an expression. Try wrapping your Import call in ToExpression. $\endgroup$ – MarcoB Jan 22 '17 at 16:08
  • $\begingroup$ Unfortunatelly, the same output (only without splitting "\") $\endgroup$ – adolzi Jan 22 '17 at 16:10
  • 1
    $\begingroup$ Data you posted is not a matrix: the first row has 13 elements and the rest 12. Also check to make sure the matrix is symmetric. $\endgroup$ – kglr Jan 22 '17 at 16:39
  • $\begingroup$ It is a symmetric matrix. Also when I copy it DirectAgglomerate it's ok, but I cannot use DirectAgglomerate and DendgramPlot at the same time. $\endgroup$ – adolzi Jan 22 '17 at 16:43
  • 2
    $\begingroup$ Please post a file that can actually be imported, not one with omissions like "..." and typos like "1,21" instead of "1.21". $\endgroup$ – Rahul Jan 23 '17 at 1:35
4
$\begingroup$

This is a comment with code. There is nothing wrong with daisy-chaining those commands. See e.g.

Needs["HierarchicalClustering`"]
DendrogramPlot@DirectAgglomerate@RandomReal[1, {12, 12}]

Your problem is in the format in which you saved the data. Import returns a string from your text file; DirectAgglomerate doesn't know how to handle it, and returns unevalulated. You need to convert your string into an expression, using e.g. ToExpression.

For instance, let's generate a file similar to yours, but containing random data:

Put[RandomReal[1, {12, 12}], "temp.txt"]

Then let's import it, convert it to an expression, and apply the other operations:

DendrogramPlot@DirectAgglomerate@ToExpression@Import["temp.txt"]

Mathematica graphics

$\endgroup$
  • 1
    $\begingroup$ +1; however, the documentation for DirectAgglomerate states that the "distance matrix m can be any symmetric matrix." The fact that it accepted a non-symmetric matrix without a warning or error message concerning this, seems like a shortfall. $\endgroup$ – Bob Hanlon Jan 23 '17 at 2:59
  • $\begingroup$ @BobHanlon That's a good point. I hadn't really given thought to the actual functionality here. Indeed, DirectAgglomerate only complains about ties when fed a random matrix, not about the structure of the matrix itself, and still produces a result. $\endgroup$ – MarcoB Jan 23 '17 at 3:15

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