我试图使用pyspark.pandas API在数据库上构造数据的共生矩阵。
我试着用这个方法构造矩阵。Constructing a co-occurrence matrix in python pandas
该代码在熊猫中运行良好,但在使用pyspark.pandas时出现了错误。
coocc = psdf.T.dot(psdf)
coocc
我得到了这个错误
TypeError: Unsupported type DataFrame
pyspark.pandas.DataFrame.dot()
以串联作为输入。
我尝试使用psdf.squeeze()
将dataframe转换为系列化,但它并不将dataframe转换为串联,因为我的dataframe有多个列。
有没有办法将pyspark.pandas.Dataframe
改为pyspark.pandas.Series
?或者在pyspark.pandas中构造共生矩阵的不同方法
发布于 2022-10-14 13:54:07
我用csr_matrix
来解决这个问题,因为数据帧有'1‘和'0’作为值
import scipy.sparse as sp
psdfx = sp.csr_matrix(psdf.astype(int).values)
psdfc = ptdfx.T * psdfx
psdfc.setdiag(0)
coocc = ps.DataFrame(psdfc.todense(), columns=psdf.columns, index=psdf.columns)
coocc
https://stackoverflow.com/questions/74069759
复制相似问题