Pandas sparse dataFrame to sparse matrix, without generating a dense matrix in memory

Pandas 0.20.0+: As of pandas version 0.20.0, released May 5, 2017, there is a one-liner for this: from scipy import sparse def sparse_df_to_csr(df): return sparse.csr_matrix(df.to_coo()) This uses the new to_coo() method. Earlier Versions: Building on Victor May’s answer, here’s a slightly faster implementation, but it only works if the entire SparseDataFrame is sparse with all … Read more

Populate a Pandas SparseDataFrame from a SciPy Sparse Matrix

A direct conversion is not supported ATM. Contributions are welcome! Try this, should be ok on memory as the SpareSeries is much like a csc_matrix (for 1 column) and pretty space efficient In [37]: col = np.array([0,0,1,2,2,2]) In [38]: data = np.array([1,2,3,4,5,6],dtype=”float64″) In [39]: m = csc_matrix( (data,(row,col)), shape=(3,3) ) In [40]: m Out[40]: <3×3 … Read more

Sparse matrix slicing using list of int

I think I’ve recreated the csr row indexing with: def extractor(indices, N): indptr=np.arange(len(indices)+1) data=np.ones(len(indices)) shape=(len(indices),N) return sparse.csr_matrix((data,indices,indptr), shape=shape) Testing on a csr I had hanging around: In [185]: M Out[185]: <30×40 sparse matrix of type ‘<class ‘numpy.float64′>’ with 76 stored elements in Compressed Sparse Row format> In [186]: indices=np.r_[0:20] In [187]: M[indices,:] Out[187]: <20×40 sparse … Read more

Are Javascript arrays sparse?

Yes, they are. They are actually hash tables internally, so you can use not only large integers but also strings, floats, or other objects. All keys get converted to strings via toString() before being added to the hash. You can confirm this with some test code: <script> var array = []; array[0] = “zero”; array[new … Read more