Skip to content

Instantly share code, notes, and snippets.

@ki-chi
Last active December 12, 2015 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ki-chi/065bfb389751481612d6 to your computer and use it in GitHub Desktop.
Save ki-chi/065bfb389751481612d6 to your computer and use it in GitHub Desktop.
重み無し無向グラフのクラスター係数を隣接行列から求める
# -*- coding: utf-8 -*-
from __future__ import print_function
import numpy as np
# it's for unweighted and undirected graph.
# Adjency matrix of the graph
adjency_mat = np.array([[0, 1, 1, 1, 0, 0],
[1, 0, 1, 0, 0, 1],
[1, 1, 0, 1, 1, 0],
[1, 0, 1, 0, 1, 0],
[0, 0, 1, 1, 0, 1],
[0, 1, 0, 0, 1, 0]])
mask_mat = (adjency_mat == 1)
adj_square = np.dot(adjency_mat, adjency_mat)
# k is the vector of the degree of nodes
k = adj_square.diagonal()
adj_square[~mask_mat] = 0
# m is the vector of the number of
# the neighbourhood's connecting, multiplied by two.
m = adj_square.sum(axis=1)
# coef_list includes all local cluster coefficient.
coef_list = np.divide(m.astype(float), k*(k-1))
global_coef = coef_list.mean()
print(global_coef)
# 0.416666666667, in this case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment