scikit-learn/scikit-learn — pull request #2194 by NicolasTr (2013-07-23 12:48:44+00:00)
Title: as_float_array doesn't copy

The function `as_float_array` doesn't copy as expected when the matrix was created with `sparse_random_matrix`. This code should fix that.

```python
import numpy as np
import scipy.sparse as sp

from sklearn.random_projection import sparse_random_matrix
from sklearn.utils import as_float_array

matrices = [
np.matrix(np.arange(5)),
sp.csc_matrix(np.arange(5)).todense(),
sparse_random_matrix(10, 10, density=0.10).todense(),
]

for M in matrices:
print type(M) #

N = as_float_array(M, copy=True)
print type(N) #

print np.isnan(M).any() # False
N[0, 0] = np.nan
print np.isnan(N).any() # True
# False in the first and the second case
# True in the third...
print np.isnan(M).any()
assert np.isnan(M).any() == False
```