将 Pandas SparseDataframe 转换为 Scipy 稀疏 csc_matrix [英] Convert Pandas SparseDataframe to Scipy sparse csc_matrix
本文介绍了将 Pandas SparseDataframe 转换为 Scipy 稀疏 csc_matrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将熊猫 SparseDataFrame
转换为 scipy.sparse.csc_matrix
.但我不想先把它转换回稠密矩阵.
I want to convert a pandas SparseDataFrame
to a scipy.sparse.csc_matrix
. But I don't want to convert it back to a dense matrix first.
现在我有类似下面的内容.
Right now I have something like the below.
df = pd.get_dummies(df, sparse=True)
基本上我需要的是从 df
进一步得到一个 scipy.sparse.csc_matrix
.有办法吗?
Basically what I need is to further get a scipy.sparse.csc_matrix
from df
. Is there a way to do it?
推荐答案
感谢@hpaulj 的回复.我最终使用了 https://stackoverflow.com/a/38157234/7298911 中的模板.
Thanks to @hpaulj's reply. I ended it up using the template from https://stackoverflow.com/a/38157234/7298911.
这是修改后的实现.
def sparseDfToCsc(df):
columns = df.columns
dat, rows = map(list,zip(*[(df[col].sp_values-df[col].fill_value, df[col].sp_index.to_int_index().indices) for col in columns]))
cols = [np.ones_like(a)*i for (i,a) in enumerate(dat)]
datF, rowsF, colsF = np.concatenate(dat), np.concatenate(rows), np.concatenate(cols)
arr = sparse.coo_matrix((datF, (rowsF, colsF)), df.shape, dtype=np.float64)
return arr.tocsc()
df = pd.get_dummies(df, sparse=True)
cscMatrix = sparseDfToCsc(df)
这篇关于将 Pandas SparseDataframe 转换为 Scipy 稀疏 csc_matrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文