python - 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。
我对分享数据集本身犹豫不决,所以我想知道我的代码中是否有一些东西可能会导致这个问题?
解决方案
推荐阅读
- javascript - 在 Bing Map API Java 上按邮政编码搜索
- vba - 参考部分表格 VBA
- next.js - NextJs css 模块 - 类名中的变量
- java - Spring:GCP Pub / Sub 依赖问题
- javascript - 为什么我得到未定义的 jwt 令牌值,因为令牌存储在 cookie 中?
- c# - 如何解决 EpiServer 的 C# Helper 中的“并非所有代码路径都返回值”
- c++ - 成员函数和成员变量在符号方面有什么区别?
- typescript - 是否可以实现 Y-combinator 的 TypeScript 通用版本?
- angular - 离子应用程序关闭时仅显示标题和消息
- sql - SQL Server 似乎舍入不正确