Repository: scipy/scipy

Issue:
Regression in hierarchy.dendogram
It seems that https://github.com/scipy/scipy/pull/11753 introduced a regression.
In the past, `labels` would have worked with a NumPy array as well.

`if labels` raises a `ValueError` because this is ambiguous in the case of NumPy array.
I don't see anything in the documentation preventing to not use a NumPy array indeed.

Sorry for not catching this before the release. We should run our examples in
`scikit-learn` on the nightly wheels and not only our test suite.


#### Reproducing code example:
<!-- 
If you place your code between the triple backticks below, 
it will be rendered as a code block. 
-->

```python
from sklearn.datasets import load_breast_cancer
from scipy.cluster import hierarchy
from scipy.stats import spearmanr

data = load_breast_cancer()
X, y = data.data, data.target

corr = spearmanr(X).correlation
corr_linkage = hierarchy.ward(corr)
dendro = hierarchy.dendrogram(
    corr_linkage, labels=data.feature_names,
)
```

#### Error message:
<!-- If any, paste the *full* error message inside a code block
as above (starting from line Traceback)
-->

```pytb
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-8fc3e844631c> in <module>
      9 corr_linkage = hierarchy.ward(corr)
     10 dendro = hierarchy.dendrogram(
---> 11     corr_linkage, labels=data.feature_names,
     12 )

~/miniconda3/envs/dev/lib/python3.7/site-packages/scipy/cluster/hierarchy.py in dendrogram(Z, p, truncate_mode, color_threshold, get_leaves, orientation, labels, count_sort, distance_sort, show_leaf_counts, no_plot, no_labels, leaf_font_size, leaf_rotation, leaf_label_func, show_contracted, link_color_func, ax, above_threshold_color)
   3275                          "'bottom', or 'right'")
   3276 
-> 3277     if labels and Z.shape[0] + 1 != len(labels):
   3278         raise ValueError("Dimensions of Z and labels must be consistent.")
   3279 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
```

#### Scipy/Numpy/Python version information:
<!-- You can simply run the following and paste the result in a code block
```
import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)
```
-->
```
In [7]: import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)                                                                                                 
1.5.0 1.18.5 sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)
```


Discussion:

Resolving patch:
diff --git a/scipy/cluster/hierarchy.py b/scipy/cluster/hierarchy.py
--- a/scipy/cluster/hierarchy.py
+++ b/scipy/cluster/hierarchy.py
@@ -3274,7 +3274,7 @@ def llf(id):
         raise ValueError("orientation must be one of 'top', 'left', "
                          "'bottom', or 'right'")
 
-    if labels and Z.shape[0] + 1 != len(labels):
+    if labels is not None and Z.shape[0] + 1 != len(labels):
         raise ValueError("Dimensions of Z and labels must be consistent.")
 
     is_valid_linkage(Z, throw=True, name='Z')


Test patch:
diff --git a/scipy/cluster/tests/test_hierarchy.py b/scipy/cluster/tests/test_hierarchy.py
--- a/scipy/cluster/tests/test_hierarchy.py
+++ b/scipy/cluster/tests/test_hierarchy.py
@@ -812,6 +812,14 @@ def test_valid_orientation(self):
         Z = linkage(hierarchy_test_data.ytdist, 'single')
         assert_raises(ValueError, dendrogram, Z, orientation="foo")
 
+    def test_labels_as_array_or_list(self):
+        # test for gh-12418
+        Z = linkage(hierarchy_test_data.ytdist, 'single')
+        labels = np.array([1, 3, 2, 6, 4, 5])
+        result1 = dendrogram(Z, labels=labels, no_plot=True)
+        result2 = dendrogram(Z, labels=labels.tolist(), no_plot=True)
+        assert result1 == result2
+
     @pytest.mark.skipif(not have_matplotlib, reason="no matplotlib")
     def test_valid_label_size(self):
         link = np.array([
@@ -824,6 +832,12 @@ def test_valid_label_size(self):
             dendrogram(link, labels=list(range(100)))
         assert "Dimensions of Z and labels must be consistent."\
                in str(exc_info.value)
+
+        with pytest.raises(
+                ValueError,
+                match="Dimensions of Z and labels must be consistent."):
+            dendrogram(link, labels=[])
+
         plt.close()
 
     @pytest.mark.skipif(not have_matplotlib, reason="no matplotlib")