title: What's the right way to use a sparse matrix in scikit?
body: <p>I'm trying to accomplish a classification task trying to use a sparse matrix. When I pass my training set to the logistic regression function, in scikit, it returns this error:</p>
  
  <pre><code>ValueError: Array contains NaN or infinity.
  </code></pre>
  
  <p>The type of the matrix I'm passing as argument to the logistic regression is <code>scipy.sparse.csr.csr_matrix</code>.</p>
  
  <p>This is how I create the sparse matrix, from a numpy array with NaNs:</p>
  
  <pre><code>X=sparse.csr_matrix(X_hat)
  </code></pre>
  
  <p>What am I doing wrong? I get the same error with <code>train_test_split</code>, and these functions are supposed to accept sparse matrices as argument according to the documentation.</p>
  
  <p>I'm using the version 0.14 of scikit-learn.</p>
  
  <p>UPDATE:
  This is the full TraceBack.</p>
  
  <pre><code>---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-65-acdaac419166&gt; in &lt;module&gt;()
        1 
  ----&gt; 2 model2.fit(X_train, y_train)
  
  C:\Anaconda\lib\site-packages\sklearn\svm\base.pyc in fit(self, X, y)
      666                              " one.")
      667 
  --&gt; 668         X = atleast2d_or_csr(X, dtype=np.float64, order="C")
      669 
      670         self.class_weight_ = compute_class_weight(self.class_weight,
  
  C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in atleast2d_or_csr(X, dtype, order, copy, force_all_finite)
      132     """
      133     return _atleast2d_or_sparse(X, dtype, order, copy, sparse.csr_matrix,
  --&gt; 134                                 "tocsr", force_all_finite)
      135 
      136 
  
  C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in _atleast2d_or_sparse(X, dtype, order, copy, sparse_class, convmethod, force_all_finite)
      105             X = sparse_class(X, dtype=dtype)
      106         if force_all_finite:
  --&gt; 107             _assert_all_finite(X.data)
      108         X.data = np.array(X.data, copy=False, order=order)
      109     else:
  
  C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in _assert_all_finite(X)
       25     if (X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum())
       26             and not np.isfinite(X).all()):
  ---&gt; 27         raise ValueError("Array contains NaN or infinity.")
       28 
       29 
  
  ValueError: Array contains NaN or infinity.
  </code></pre>