gpt4 book ai didi

python - 从 SciPy 稀疏 Coo 矩阵填充 Pandas SparseDataFrame

转载 作者:行者123 更新时间:2023-11-28 17:31:18 26 4
gpt4 key购买 nike

(这个问题与 "populate a Pandas SparseDataFrame from a SciPy Sparse Matrix" 相关。我想从 scipy.sparse.coo_matrix 填充一个 SparseDataFrame(特别是)提到的问题是针对不同的 SciPy 稀疏矩阵(企业社会责任)...所以就这样......)

我注意到 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 等。因此您可以使用最适合特定操作的任何一种形式。

对于走另一条路,我已经回答了

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

您 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

实际上,它采用相同的 dataij 来构建 coo 矩阵,使得一个系列,对其进行排序,并将其变成一个稀疏系列。

关于python - 从 SciPy 稀疏 Coo 矩阵填充 Pandas SparseDataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34181494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com