Repository: pydata/sparse

Issue:
sparse.as_coo does not work for N-by-1 np.matrix
When input a N-by-1 matrix of type np.matrix, sparse.as_coo throws an error. It correctly dispatchs to sparse.COO.from_numpy but can not handle it. Please see the following
```python
In [1]: import scipy.sparse,sparse

In [2]: sp = scipy.sparse.random(10,1,0.5)

In [3]: mat = sp.argmax(1)

In [4]: sparse.as_coo(mat)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-5492fc8fff45> in <module>()
----> 1 sparse.as_coo(mat)

C:\Users\Liyu\Anaconda3\lib\site-packages\sparse\coo\core.py in as_coo(x, shape, fill_value)
   1749
   1750     if isinstance(x, np.ndarray):
-> 1751         return COO.from_numpy(x, fill_value=fill_value)
   1752
   1753     if isinstance(x, scipy.sparse.spmatrix):

C:\Users\Liyu\Anaconda3\lib\site-packages\sparse\coo\core.py in from_numpy(cls, x, fill_value)
    308             data = np.array(x, ndmin=1)
    309         return cls(coords, data, shape=x.shape, has_duplicates=False,
--> 310                    sorted=True, fill_value=fill_value)
    311
    312     def todense(self):

C:\Users\Liyu\Anaconda3\lib\site-packages\sparse\coo\core.py in __init__(self, coords, data, shape, has_duplicates, sorted, cache, fill_value)
    223                 msg = ('The data length does not match the coordinates '
    224                        'given.\nlen(data) = {}, but {} coords specified.')
--> 225                 raise ValueError(msg.format(len(data), self.coords.shape[1]))
    226             if len(self.shape) != self.coords.shape[0]:
    227                 msg = ("Shape specified by `shape` doesn't match the "

ValueError: The data length does not match the coordinates given.
len(data) = 1, but 0 coords specified.
```

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/pydata/sparse/pull/200?src=pr&el=h1) Report
> Merging [#200](https://codecov.io/gh/pydata/sparse/pull/200?src=pr&el=desc) into [master](https://codecov.io/gh/pydata/sparse/commit/76b98bc009c1fafa137f1dcf7d889a56b2f3a47b?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pydata/sparse/pull/200/graphs/tree.svg?width=650&token=H212u0Uxxw&height=150&src=pr)](https://codecov.io/gh/pydata/sparse/pull/200?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #200   +/-   ##
=======================================
  Coverage   97.65%   97.65%           
=======================================
  Files          11       11           
  Lines        1447     1447           
=======================================
  Hits         1413     1413           
  Misses         34       34
```


| [Impacted Files](https://codecov.io/gh/pydata/sparse/pull/200?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [sparse/coo/core.py](https://codecov.io/gh/pydata/sparse/pull/200/diff?src=pr&el=tree#diff-c3BhcnNlL2Nvby9jb3JlLnB5) | `97.14% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pydata/sparse/pull/200?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pydata/sparse/pull/200?src=pr&el=footer). Last update [76b98bc...25d04c8](https://codecov.io/gh/pydata/sparse/pull/200?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/sparse/coo/core.py b/sparse/coo/core.py
index 44943fc..de0524a 100644
--- a/sparse/coo/core.py
+++ b/sparse/coo/core.py
@@ -315,7 +315,7 @@ class COO(SparseArray, NDArrayOperatorsMixin):
         >>> COO.from_numpy(x, fill_value=np.nan)
         <COO: shape=(5, 5), dtype=float64, nnz=5, fill_value=nan>
         """
-        x = np.asanyarray(x)
+        x = np.asanyarray(x).view(type=np.ndarray)
 
         if fill_value is None:
             fill_value = _zero_of_dtype(x.dtype)


Test patch:
diff --git a/sparse/tests/test_coo.py b/sparse/tests/test_coo.py
index 03e0268..e629989 100644
--- a/sparse/tests/test_coo.py
+++ b/sparse/tests/test_coo.py
@@ -1971,3 +1971,10 @@ def test_complex_methods(complex):
     assert_eq(s.imag, x.imag)
     assert_eq(s.real, x.real)
     assert_eq(s.conj(), x.conj())
+
+
+def test_np_matrix():
+    x = np.random.rand(10, 1).view(type=np.matrix)
+    s = sparse.COO.from_numpy(x)
+
+    assert_eq(x, s)