LoginSignup
1
1

More than 3 years have passed since last update.

Pythonでクラスター係数を算出

Last updated at Posted at 2019-09-11

Pythonのnetworkxというパッケージを用いて,クラスター係数を出していきます.

もとのデータは,2015.csv から 2018.csv という名前であり,Nodeは「引用者」「被引用者」とします.

事前に,pandas と networkx を install しておきましょう.

cluster.py

import pandas as pd
import networkx as nx

def CENTRALITY(y):
  #pandas の read_csv('',dtype={})
  data=pd.read_csv(f'{y}.csv',dtype={'引用者ID':'str','被引用者ID':'str'})

  print(f'readed file {y}')

  #dataのサイズを確認
  print(len(data))

  #networkx を用いて,dataをグラフとして割り当て(data,edge名,edge名)
  G=nx.from_pandas_edgelist(data2,'引用者ID','被引用者ID')
  print(f'generated graph {y}')

  #クラスター係数と次数の計算 pandas.DataFrame  dict = dictionaly型
  out=pd.DataFrame(dict(
    CLUSTER=nx.clustering(G),
    degree=dict(nx.degree(G))
  ))
  print(f'calculated centralities {y}')

  #ファイルへ出力 pd.DataFrame.to_csv('',)
  out.index.name='ID'
  out.to_csv(f'out/cluster_{y}_v2.csv',float_format='%.10f')
  print(f'generated file {y}')

  pbar.update(1)

def main():

       for i in range(2015, 2018):
                CENTRALITY(i)

if __name__ == "__main__":
  main()

うまくいきましたら,「いいね」お願いしまーす.

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1