在Pandas中高效地创建稀疏透视表?

55ooxyrt  于 2023-03-21  发布在  其他
关注(0)|答案(4)|浏览(141)

我在做一个两栏的记录列表(A和B)转换成矩阵表示。我一直在pandas中使用pivot函数,但结果最终相当大。pandas支持将其旋转成稀疏格式吗?我知道我可以将其旋转,然后将其转换成某种稀疏表示,但并不像我希望的那样优雅。我的最终目标是用它作为预测模型的输入。
或者,在pandas之外是否存在某种稀疏枢轴功能?
编辑:这是一个非稀疏透视的例子

import pandas as pd
frame=pd.DataFrame()
frame['person']=['me','you','him','you','him','me']
frame['thing']=['a','a','b','c','d','d']
frame['count']=[1,1,1,1,1,1]

frame

  person thing  count
0     me     a      1
1    you     a      1
2    him     b      1
3    you     c      1
4    him     d      1
5     me     d      1

frame.pivot('person','thing')

        count            
thing       a   b   c   d
person                   
him       NaN   1 NaN   1
me          1 NaN NaN   1
you         1 NaN   1 NaN

这创建了一个矩阵,可以包含所有可能的人和事物的组合,但它不是稀疏的。
http://docs.scipy.org/doc/scipy/reference/sparse.html
稀疏矩阵占用的空间更少,因为它们可能意味着NaN或0之类的东西。如果我有一个非常大的数据集,这个旋转函数可以生成一个矩阵,由于大量的NaN或0,它应该是稀疏的。我希望我可以通过立即生成一些稀疏的东西来保存大量的空间/内存,而不是创建一个密集矩阵,然后将其转换为稀疏矩阵。

9fkzdhlc

9fkzdhlc1#

下面是一个基于人和事物的数据和索引创建稀疏scipy矩阵的方法。person_uthing_u是表示要创建的pivot的行和列的唯一条目的列表。注意:这假设您的count列中已经有了您想要的值。

from scipy.sparse import csr_matrix

person_u = list(sort(frame.person.unique()))
thing_u = list(sort(frame.thing.unique()))

data = frame['count'].tolist()
row = frame.person.astype('category', categories=person_u).cat.codes
col = frame.thing.astype('category', categories=thing_u).cat.codes
sparse_matrix = csr_matrix((data, (row, col)), shape=(len(person_u), len(thing_u)))

>>> sparse_matrix 
<3x4 sparse matrix of type '<type 'numpy.int64'>'
    with 6 stored elements in Compressed Sparse Row format>

>>> sparse_matrix.todense()

matrix([[0, 1, 0, 1],
        [1, 0, 0, 1],
        [1, 0, 1, 0]])

根据你最初的问题,scipy稀疏矩阵应该足以满足你的需求,但是如果你希望有一个稀疏的 Dataframe ,你可以做以下事情:

dfs=pd.SparseDataFrame([ pd.SparseSeries(sparse_matrix[i].toarray().ravel(), fill_value=0) 
                              for i in np.arange(sparse_matrix.shape[0]) ], index=person_u, columns=thing_u, default_fill_value=0)

>>> dfs
     a  b  c  d
him  0  1  0  1
me   1  0  0  1
you  1  0  1  0

>>> type(dfs)
pandas.sparse.frame.SparseDataFrame
sg24os4d

sg24os4d2#

@khammel之前发布的答案很有用,但不幸的是,由于pandas和Python的变化,答案不再有效。下面应该会产生相同的输出:

from scipy.sparse import csr_matrix
from pandas.api.types import CategoricalDtype

person_c = CategoricalDtype(sorted(frame.person.unique()), ordered=True)
thing_c = CategoricalDtype(sorted(frame.thing.unique()), ordered=True)

row = frame.person.astype(person_c).cat.codes
col = frame.thing.astype(thing_c).cat.codes
sparse_matrix = csr_matrix((frame["count"], (row, col)), \
                           shape=(person_c.categories.size, thing_c.categories.size))

>>> sparse_matrix
<3x4 sparse matrix of type '<class 'numpy.int64'>'
     with 6 stored elements in Compressed Sparse Row format>

>>> sparse_matrix.todense()
matrix([[0, 1, 0, 1],
        [1, 0, 0, 1],
        [1, 0, 1, 0]], dtype=int64)

dfs = pd.SparseDataFrame(sparse_matrix, \
                         index=person_c.categories, \
                         columns=thing_c.categories, \
                         default_fill_value=0)
>>> dfs
        a   b   c   d
 him    0   1   0   1
  me    1   0   0   1
 you    1   0   1   0

主要变化如下:

  • .astype()不再接受“categorical”。您必须创建CategoricalDtype对象。
  • sort()不再工作

其他的变化则比较肤浅:

  • 使用类别大小而不是唯一Series对象的长度,只是因为我不想不必要地创建另一个对象
  • csr_matrixframe["count"])的数据输入不需要是列表对象
  • PandasSparseDataFrame现在直接接受scipy.sparse对象
u5rb5r59

u5rb5r593#

我也遇到了类似的问题,我偶然发现了这篇文章。唯一的区别是,我在DataFrame中有两列定义了输出矩阵的“行维度”(i)。我想这可能是一个有趣的概括,我使用了grouper

# function
import pandas as pd

from scipy.sparse import csr_matrix

def df_to_sm(data, vars_i, vars_j):
    grpr_i = data.groupby(vars_i).grouper

    idx_i = grpr_i.group_info[0]

    grpr_j = data.groupby(vars_j).grouper

    idx_j = grpr_j.group_info[0]

    data_sm = csr_matrix((data['val'].values, (idx_i, idx_j)),
                         shape=(grpr_i.ngroups, grpr_j.ngroups))

    return data_sm, grpr_i, grpr_j

# example
data = pd.DataFrame({'var_i_1' : ['a1', 'a1', 'a1', 'a2', 'a2', 'a3'],
                     'var_i_2' : ['b2', 'b1', 'b1', 'b1', 'b1', 'b4'],
                     'var_j_1' : ['c2', 'c3', 'c2', 'c1', 'c2', 'c3'],
                     'val' : [1, 2, 3, 4, 5, 6]})

data_sm, _, _ = df_to_sm(data, ['var_i_1', 'var_i_2'], ['var_j_1'])

data_sm.todense()
nimxete2

nimxete24#

这里是一个答案,它更新了@Alnilam的答案中的方法,使用最新的pandas库,不再包含该答案中的所有函数。

from scipy.sparse import csr_matrix
from pandas.api.types import CategoricalDtype

rcLabel, vLabel = ('person', 'thing'), 'count'
rcCat = [CategoricalDtype(sorted(frame[col].unique()), ordered=True) for col in rcLabel]
rc = [frame[column].astype(aType).cat.codes for column, aType in zip(rcLabel, rcCat)]
mat = csr_matrix((frame[vLabel], rc), shape=tuple(cat.categories.size for cat in rcCat))
dfPivot = ( pd.DataFrame.sparse.from_spmatrix(
    mat, index=rcCat[0].categories, columns=rcCat[1].categories) )

相关问题