欢迎光临散文网 会员登陆 & 注册

Gephi实现作者共现网络可视化

2022-12-03 16:09 作者:情报工作站  | 我要投稿

Gephi实现作者共现网络可视化

1.下载安装

    https://gephi.org/  

2 作者共现分析

https://github.com/SparksFly8/Co-occurrence-Matrix

作者共现可视化

2.1 数据

https://raw.githubusercontent.com/SparksFly8/Co-occurrence-Matrix/master/data.csv

数据格式

Xiaohui Bei,Shengyu Zhang
Pin-Yu Chen,Yash Sharma,Huan Zhang,Jinfeng Yi,Cho-Jui Hsieh
Jonathan Chung,Moshe Eizenman,Uros Rakita,Roger McIntyre,Peter Giacobbe
Bolin Ding,Harsha Nori,Paul Li,Joshua Allen

2.2 计算频次与共现次数

def get_Co_authors(filePath):
    '''
    读取csv文件获取作者信息并存储到列表中
    :param filePath: csv文件路径
    :return co_authors_list: 一个包含所有作者的列表
    '''
    # 设置编码为utf-8-sig防止首部\ufeff的出现,它是windows系统自带的BOM,用于区分大端和小端UTF-16编码
    with open(filePath, 'r', encoding='utf-8-sig') as f:
        text = f.read()
        co_authors_list = text.split('\n')  # 分割数据中的换行符'\n'两边的数据
        if '' in co_authors_list:
            co_authors_list.remove('')          # 删除列表结尾的空字符
        return co_authors_list

def str2csv(filePath, s):
    '''
    将字符串写入到本地csv文件中
    :param filePath: csv文件路径
    :param s: 待写入字符串(逗号分隔格式)
    '''
    with open(filePath, 'w', encoding='utf-8') as f:
        f.write(s)
    print('写入文件成功,请在'+filePath+'中查看')

def sortDictValue(dict, is_reverse):
    '''
    将字典按照value排序
    :param dict: 待排序的字典
    :param is_reverse: 是否按照倒序排序
    :return s: 符合csv逗号分隔格式的字符串
    '''
    # 对字典的值进行倒序排序,items()将字典的每个键值对转化为一个元组,key输入的是函数,item[1]表示元组的第二个元素,reverse为真表示倒序
    tups = sorted(dict.items(), key=lambda item: item[1], reverse=is_reverse)
    s = ''
    for tup in tups:  # 合并成csv需要的逗号分隔格式
        s = s + tup[0] + ',' + str(tup[1]) + '\n'
    return s

def build_matrix(co_authors_list, is_reverse):
    '''
    根据共同作者列表,构建共现矩阵(存储到字典中),并将该字典按照权值排序
    :param co_authors_list: 共同作者列表
    :param is_reverse: 排序是否倒序
    :return node_str: 三元组形式的节点字符串(且符合csv逗号分隔格式)
    :return edge_str: 三元组形式的边字符串(且符合csv逗号分隔格式)
    '''
    node_dict = {}  # 节点字典,包含节点名+节点权值(频数)
    edge_dict = {}  # 边字典,包含起点+目标点+边权值(频数)
    # 第1层循环,遍历整表的每行作者信息
    for row_authors in co_authors_list:
        row_authors_list = row_authors.split(',') # 依据','分割每行所有作者,存储到列表中
        # 第2层循环,遍历当前行所有作者中每个作者信息
        for index, pre_au in enumerate(row_authors_list): # 使用enumerate()以获取遍历次数index
            # 统计单个作者出现的频次
            if pre_au not in node_dict:
                node_dict[pre_au] = 1
            else:
                node_dict[pre_au] += 1
            # 若遍历到倒数第一个元素,则无需记录关系,结束循环即可
            if pre_au == row_authors_list[-1]:
                break
            connect_list = row_authors_list[index+1:]
            # 第3层循环,遍历当前行该作者后面所有的合作者,以统计两两作者合作的频次
            for next_au in connect_list:
                A, B = pre_au, next_au
                # 固定两两作者的顺序
                if A > B:
                    A, B = B, A
                key = A+','+B  # 格式化为逗号分隔A,B形式,作为字典的键
                # 若该关系不在字典中,则初始化为1,表示作者间的合作次数
                if key not in edge_dict:
                    edge_dict[key] = 1
                else:
                    edge_dict[key] += 1
    # 对得到的字典按照value进行排序
    node_str = sortDictValue(node_dict, is_reverse)  # 节点
    edge_str = sortDictValue(edge_dict, is_reverse)   # 边
    return node_str, edge_str


if __name__ == '__main__':
    readfilePath = r'data.csv'
    writefilePath1 = r'node.csv'
    writefilePath2 = r'edge.csv'
    # 读取csv文件获取作者信息并存储到列表中
    co_authors_list = get_Co_authors(readfilePath)
    # 根据共同作者列表, 构建共现矩阵(存储到字典中), 并将该字典按照权值排序
    node_str, edge_str = build_matrix(co_authors_list, is_reverse=True)
    # print(edge_str)
    # 将字符串写入到本地csv文件中
    str2csv(writefilePath1, node_str)
    str2csv(writefilePath2, edge_str)

2.3 过滤数据(共现频次)

import pandas as pd
import numpy as np

def main(edge_weight):
    node_df = pd.read_csv('./node.csv',header=None)
    node_df.columns = ['Label','Weight']
    node_df['Id'] = node_df.Label
    node_df = node_df[['Id','Label','Weight']]
    edge_df = pd.read_csv('./edge.csv',header=None)
    edge_df.columns = ['Source','Target','Weight']

    edge_df = edge_df[edge_df.Weight >= edge_weight]
    node_label= list(set(np.array(edge_df[['Source','Target']].values).flatten()))
    node_df['Filter'] = node_df.Label.apply(lambda x:1 if x in node_label else 0)
    node_df = node_df.query("Filter == 1")
    node_df.drop(columns=['Filter'],inplace=True)

    node_df.to_csv('./transform_node.csv',index=0,encoding='utf-8')
    edge_df.to_csv('./transform_edge.csv',index=0,encoding='utf-8')
    return node_df,edge_df

if __name__ == '__main__':
    main(3)

3 Gephi可视化

4 导出至Vosviewer可视化

5 参考资料

python 共现矩阵的实现 - Dragon水魅 - 博客园

【绘制关系网络图】Gephi 入门使用_卖山楂啦prss的博客-CSDN博客_gephi网络图

https://github.com/SparksFly8/Co-occurrence-Matrix


本文使用 文章同步助手 同步

Gephi实现作者共现网络可视化的评论 (共 条)

分享到微博请遵守国家法律