Repository: scipy/scipy

Issue:
BUG: spatial: Bad error message from `hamming` when `w` has the wrong size.
The error message reported by `scipy.spatial.distance.hamming` when the size of the weights parameter `w` does not match the size of the input vectors is not correct.  The following is with the main developement branch and numpy 1.24.0:

```
In [1]: import scipy

In [2]: scipy.__version__
Out[2]: '1.11.0.dev0+1231.71c19cc'

In [3]: import numpy as np

In [4]: np.__version__
Out[4]: '1.24.0'

In [5]: from scipy.spatial.distance import hamming
```

Call `hamming` with a weight array whose size does not match the sizes of `u` and `v`:

```
In [6]: hamming([1, 0, 1, 1], [1, 1, 0, 1], w=[1, 3, 1])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [6], line 1
----> 1 hamming([1, 0, 1, 1], [1, 1, 0, 1], w=[1, 3, 1])

File ~/repos/git/forks/scipy/build-install/lib/python3.10/site-packages/scipy/spatial/distance.py:720, in hamming(u, v, w)
    718 if w is not None:
    719     w = _validate_weights(w)
--> 720 return np.average(u_ne_v, weights=w)

File <__array_function__ internals>:200, in average(*args, **kwargs)

File ~/py3.10.8/lib/python3.10/site-packages/numpy/lib/function_base.py:532, in average(a, axis, weights, returned, keepdims)
    530 if a.shape != wgt.shape:
    531     if axis is None:
--> 532         raise TypeError(
    533             "Axis must be specified when shapes of a and weights "
    534             "differ.")
    535     if wgt.ndim != 1:
    536         raise TypeError(
    537             "1D weights expected when shapes of a and weights differ.")

TypeError: Axis must be specified when shapes of a and weights differ.

```
The error message should just say that the size of `w` does not match the size of `u` and `v`.  The reference to an `axis` parameter is misleading, because that error is actually being raised by a function called by `hamming`; `hamming` does not have an `axis` parameter.  I suspect `hamming` should do some parameter validation earlier than it currently does.



Discussion:
I'd like to give this a go as my first contribution. 
Hi @karan0handa! Feel free - please take some time to read our [Contributing guide](https://docs.scipy.org/doc/scipy/dev/index.html) and if you have any specific questions you can ping me as well. Welcome!

Resolving patch:
diff --git a/scipy/spatial/distance.py b/scipy/spatial/distance.py
--- a/scipy/spatial/distance.py
+++ b/scipy/spatial/distance.py
@@ -737,6 +737,8 @@ def hamming(u, v, w=None):
     u_ne_v = u != v
     if w is not None:
         w = _validate_weights(w)
+        if w.shape != u.shape:
+            raise ValueError("'w' should have the same length as 'u' and 'v'.")
     return np.average(u_ne_v, weights=w)
 
 


Test patch:
diff --git a/scipy/spatial/tests/test_distance.py b/scipy/spatial/tests/test_distance.py
--- a/scipy/spatial/tests/test_distance.py
+++ b/scipy/spatial/tests/test_distance.py
@@ -1935,6 +1935,15 @@ def test_hamming_unequal_length():
     assert_raises(ValueError, whamming, x, y)
 
 
+def test_hamming_unequal_length_with_w():
+    u = [0, 0, 1]
+    v = [0, 0, 1]
+    w = [1, 0, 1, 0]
+    msg = "'w' should have the same length as 'u' and 'v'."
+    with assert_raises(ValueError, match=msg):
+        whamming(u, v, w)
+
+
 def test_hamming_string_array():
     # https://github.com/scikit-learn/scikit-learn/issues/4014
     a = np.array(['eggs', 'spam', 'spam', 'eggs', 'spam', 'spam', 'spam',