从 SciPy 稀疏 Coo 矩阵填充 Pandas SparseDataFrame
Populate a Pandas SparseDataFrame from a SciPy Sparse Coo Matrix
(这个问题与 "populate a Pandas SparseDataFrame from a SciPy Sparse Matrix" 有关。我想从 scipy.sparse 填充一个 SparseDataFrame。coo_matrix(具体来说)提到的问题是针对一个不同的 SciPy 稀疏矩阵 (csr)...
所以就这样...)
我注意到 Pandas 现在有 support for Sparse Matrices and Arrays。目前,我创建 DataFrame()
是这样的:
return DataFrame(matrix.toarray(), columns=features, index=observations)
有没有办法用 scipy.sparse.coo_matrix()
或 coo_matrix()
创建 SparseDataFrame()
?转换为密集格式会严重破坏 RAM...!
http://pandas.pydata.org/pandas-docs/stable/sparse.html#interaction-with-scipy-sparse
A convenience method SparseSeries.from_coo() is implemented for creating a SparseSeries from a scipy.sparse.coo_matrix.
在 scipy.sparse
中,有一些方法可以将数据形式相互转换。 .tocoo
、.tocsc
等。因此您可以使用最适合特定操作的形式。
对于另一条路,我已经回答了
您 2013 年的链接答案按行迭代 - 使用 toarray
使行密集。我还没有看过 pandas from_coo
的作用。
最近关于 pandas sparse
的 SO 问题
non-NDFFrame object error using pandas.SparseSeries.from_coo() function
来自https://github.com/pydata/pandas/blob/master/pandas/sparse/scipy_sparse.py
def _coo_to_sparse_series(A, dense_index=False):
""" Convert a scipy.sparse.coo_matrix to a SparseSeries.
Use the defaults given in the SparseSeries constructor. """
s = Series(A.data, MultiIndex.from_arrays((A.row, A.col)))
s = s.sort_index()
s = s.to_sparse() # TODO: specify kind?
# ...
return s
实际上,它采用相同的 data
、i
、j
来构建 coo
矩阵,制作一个序列,对其进行排序,然后将其转换为稀疏系列。
(这个问题与 "populate a Pandas SparseDataFrame from a SciPy Sparse Matrix" 有关。我想从 scipy.sparse 填充一个 SparseDataFrame。coo_matrix(具体来说)提到的问题是针对一个不同的 SciPy 稀疏矩阵 (csr)... 所以就这样...)
我注意到 Pandas 现在有 support for Sparse Matrices and Arrays。目前,我创建 DataFrame()
是这样的:
return DataFrame(matrix.toarray(), columns=features, index=observations)
有没有办法用 scipy.sparse.coo_matrix()
或 coo_matrix()
创建 SparseDataFrame()
?转换为密集格式会严重破坏 RAM...!
http://pandas.pydata.org/pandas-docs/stable/sparse.html#interaction-with-scipy-sparse
A convenience method SparseSeries.from_coo() is implemented for creating a SparseSeries from a scipy.sparse.coo_matrix.
在 scipy.sparse
中,有一些方法可以将数据形式相互转换。 .tocoo
、.tocsc
等。因此您可以使用最适合特定操作的形式。
对于另一条路,我已经回答了
您 2013 年的链接答案按行迭代 - 使用 toarray
使行密集。我还没有看过 pandas from_coo
的作用。
最近关于 pandas sparse
的 SO 问题non-NDFFrame object error using pandas.SparseSeries.from_coo() function
来自https://github.com/pydata/pandas/blob/master/pandas/sparse/scipy_sparse.py
def _coo_to_sparse_series(A, dense_index=False):
""" Convert a scipy.sparse.coo_matrix to a SparseSeries.
Use the defaults given in the SparseSeries constructor. """
s = Series(A.data, MultiIndex.from_arrays((A.row, A.col)))
s = s.sort_index()
s = s.to_sparse() # TODO: specify kind?
# ...
return s
实际上,它采用相同的 data
、i
、j
来构建 coo
矩阵,制作一个序列,对其进行排序,然后将其转换为稀疏系列。