首页 > 解决方案 > numpy输出一些不正确值的邻接矩阵

问题描述

我有一个 6000 行的数据框,如下所示:

    index name  title       appearance
    0     John  Article 1   1.0
    1     John  Article 3   1.0
    2     Jane  Article 1   1.0
    3     Jane  Article 2   1.0
    4     Sarah Article 2   1.0

我通过获取数据框的叉积创建了一个邻接矩阵:

covar_df = pd.DataFrame(columns = df.name.unique(), index = df.title.unique())
covar_df = covar_df.fillna(0)

for index, row in df.iterrows():
    person = df.loc[index, 'name']
    appearance = df.loc[index, 'appearance']
    covar_df.loc[df.loc[index, 'title'], person] += appearance

adjacency_df = pd.DataFrame(np.dot(covar_df.T, covar_df), index = df.name.unique(), columns = df.name.unique())

邻接矩阵中的大多数节点都是正确的,但不是。例如,使用真实数据,如果我输入:

[In]: covar_df['John'].sum()
[Out]: 626

但是在邻接矩阵中 John 与 John 相交的节点是 630。

我对分享数据集本身犹豫不决,所以我想知道我的代码中是否有一些东西可能会导致这个问题?

标签: pythonpandasnumpy

解决方案


推荐阅读