Repository: scipy/scipy

Issue:
gaussian_kde not working with numpy matrix
<!-- 
Thank you for taking the time to report a SciPy issue.
Please describe the issue in detail, and for bug reports
fill in the fields below. You can delete the sections that 
don't apply to your issue. You can view the final output
by clicking the preview button above.
-->

It seems gaussian_kde doesn't work with numpy matrix but only failes when you try to use evalute.

I think it should either work with np.matrix or refuse to accept it when fitting. It took me forever to understand whats wrong :(

### Reproducing code example:
<!-- 
If you place your code between the triple backticks below, 
it will be marked as a code block automatically 
-->
```
from scipy import stats

def measure(n):

    "Measurement model, return two coupled measurements."
    m1 = np.random.normal(size=n)
    m2 = np.random.normal(scale=0.5, size=n)
    return m1+m2, m1-m2

m1, m2 = measure(2000)
xmin = m1.min()
xmax = m1.max()
ymin = m2.min()
ymax = m2.max()

X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(np.matrix(values))
kernel.evaluate(np.matrix(values))

```


### Error message:
<!-- If any, paste the *full* error message inside a code block
as above (starting from line Traceback)
-->



```
Traceback (most recent call last):
  File "/usr/lib/python3.5/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 20, in <module>
  File "/usr/local/lib/python3.5/dist-packages/scipy/stats/kde.py", line 214, in evaluate
    energy = sum(diff*tdiff,axis=0) / 2.0
  File "/home/asaf/.local/lib/python3.5/site-packages/numpy/matrixlib/defmatrix.py", line 215, in __mul__
    return N.dot(self, asmatrix(other))
ValueError: shapes (2,2000) and (2,2000) not aligned: 2000 (dim 1) != 2 (dim 0)
```

### 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)
```
-->



Discussion:
Not really a bug because the docs say "array". However easy to make more user-friendly by adding `asarray` to ``__init__` and `evaluate` methods. Now it uses `atleast_2d`, which is needed as well but doesn't convert `matrix` input to `ndarray`.
Hi, can I work on this?

hi @cldssty, sure. 

I would suggest to not add a test (unusual, I know). We don't want to introduce new `np.matrix` usage in tests. And otherwise you'd have to introduce some wrappers like [sparse.sputils.matrix](https://github.com/scipy/scipy/blob/master/scipy/sparse/sputils.py#L345)

just a minimal patch that makes the example in this issue test is fine I think

Resolving patch:
diff --git a/scipy/stats/kde.py b/scipy/stats/kde.py
--- a/scipy/stats/kde.py
+++ b/scipy/stats/kde.py
@@ -28,8 +28,9 @@
 from scipy.special import logsumexp
 from scipy._lib._numpy_compat import cov
 
-from numpy import (atleast_2d, reshape, zeros, newaxis, dot, exp, pi, sqrt,
-                   ravel, power, atleast_1d, squeeze, sum, transpose, ones)
+from numpy import (asarray, atleast_2d, reshape, zeros, newaxis, dot, exp, pi,
+                   sqrt, ravel, power, atleast_1d, squeeze, sum, transpose,
+                   ones)
 import numpy as np
 from numpy.random import choice, multivariate_normal
 
@@ -190,7 +191,7 @@ class gaussian_kde(object):
 
     """
     def __init__(self, dataset, bw_method=None, weights=None):
-        self.dataset = atleast_2d(dataset)
+        self.dataset = atleast_2d(asarray(dataset))
         if not self.dataset.size > 1:
             raise ValueError("`dataset` input should have multiple elements.")
 
@@ -227,7 +228,7 @@ def evaluate(self, points):
                      the dimensionality of the KDE.
 
         """
-        points = atleast_2d(points)
+        points = atleast_2d(asarray(points))
 
         d, m = points.shape
         if d != self.d:


Test patch: