def _iterate_sparse_X(X):
 n_samples = X.shape[0]
 X_indices = X.indices
 X_data = X.data
 X_indptr = X.indptr
 for i in xrange(n_samples):
  row = np.zeros(X.shape[1])
  (startptr, endptr) = (X_indptr[i], X_indptr[(i + 1)])
  nonzero_indices = X_indices[startptr:endptr]
  row[nonzero_indices] = X_data[startptr:endptr]
  (yield row)

def _iterate_sparse_X ( X ) : n_samples = X . shape [ 0 ] X_indices = X . indices X_data = X . data X_indptr = X . indptr for i in xrange ( n_samples ) : row = np . zeros ( X . shape [ 1 ] ) ( startptr , endptr ) = ( X_indptr [ i ] , X_indptr [ ( i + 1 ) ] ) nonzero_indices = X_indices [ startptr : endptr ] row [ nonzero_indices ] = X_data [ startptr : endptr ] ( yield row )
this little hack returns a densified row when iterating over a sparse matrix .