w3resource

Pandas Series: sparse.from_coo() function

Series-sparse.from_coo() function

The from_coo() function is used to create a SparseSeries from a scipy.sparse.coo_matrix.

Syntax:

classmethod sparse.from_coo(A, dense_index=False)
Pandas Series: sparse.from_coo() function

Parameters:

Name Description Type/Default Value Required / Optional
dense_index If False (default), the SparseSeries index consists of only the coords of the non-null entries of the original coo_matrix. If True, the SparseSeries index consists of the full sorted (row, col) coordinates of the coo_matrix. bool, default False Required

Returns: s : SparseSeries

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
from scipy import sparse
X = sparse.coo_matrix(([4.0, 2.0, 3.0], ([1, 0, 1], [0, 2, 3])),
                       shape=(3, 4))
X

Output:

<3x4 sparse matrix of type '<class 'numpy.float64'>'
	with 3 stored elements in COOrdinate format>

Python-Pandas Code:

import numpy as np
import pandas as pd
from scipy import sparse
X = sparse.coo_matrix(([4.0, 2.0, 3.0], ([1, 0, 1], [0, 2, 3])),
                       shape=(3, 4))
X.todense()

Output:

matrix([[0., 0., 2., 0.],
        [4., 0., 0., 3.],
        [0., 0., 0., 0.]])
Pandas Series: sparse.from_coo() function

Python-Pandas Code:

import numpy as np
import pandas as pd
from scipy import sparse
X = sparse.coo_matrix(([4.0, 2.0, 3.0], ([1, 0, 1], [0, 2, 3])),
                       shape=(3, 4))
ss = pd.SparseSeries.from_coo(X)
ss

Output:

0  2    2.0
1  0    4.0
   3    3.0
dtype: Sparse[float64, nan]
BlockIndex
Block locations: array([0])
Block lengths: array([3])

Previous: Series-sparse.sp_values() function
Next: Series-sparse.to_coo() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-sparse-from_coo.php