Graph Convolutional Networks を学習するための準備

### 内容
- Graph Convolutional Networks を学ぶための準備。
- 簡単な具体例で NetworkX、PageRank、Poincaré Embeddings、 DeepWalk に慣れる。 


### NetworkX

```python
import matplotlib.pyplot as plt
%matplotlib inline

import networkx as nx

G = nx.karate_club_graph()

# グラフの特徴量の確認
print(G)
print(nx.number_of_nodes(G))
print(G.number_of_edges())
print(nx.nodes(G))
print(G.nodes)
print(G.node[0])
print(G.node[30])
print(G.edges)
print(G.adj)
print(G.adj[0])
print(G.degree[0])
print(list(G.neighbors(0)))

# 可視化
pos = nx.spring_layout(G)

color = []
for node in G.nodes:
    if G.node[node]['club'] == 'Mr. Hi':
        color.append('r')
    elif G.node[node]['club'] == 'Officer':
        color.append('b')
    
plt.figure(figsize=(5, 5))
#nx.draw(G, pos=pos, node_size=200, node_color=color, with_labels=True)
nx.draw_networkx(G, pos=pos, node_size=200, node_color=color, with_labels=True)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/254604/a3ee7b30-3784-6af8-752f-6cc3c755f4f8.png)

```python
# 各行列の確認
nx.adjacency_matrix(G).todense()

nx.laplacian_matrix(G).todense()
```


### PageRangk

```python
pr = nx.pagerank(G)

plt.figure(figsize=(5, 5))
nx.draw_networkx(G, pos, node_size=200, with_labels=True,
                 node_color=list(pr.values()), cmap=plt.cm.Reds)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/254604/2668f6a3-17b7-9007-cdb2-7759a6bee560.png)


### Poincaré Embeddings

```python
import numpy as np

from gensim.models.poincare import PoincareModel, PoincareRelations
from gensim.viz.poincare import poincare_2d_visualization

relations = []
for edge in G.edges:
    n0 = edge[0]
    n1 = edge[1]
    if G.degree[n0] > G.degree[n1]:
        relations.append((str(n1), str(n0)))
    elif G.degree[n1] > G.degree[n0]:
        relations.append((str(n0), str(n1)))
    else:
        relations.append((str(n0), str(n1)))
        relations.append((str(n1), str(n0))) 
    
model = PoincareModel(relations, size=2, negative=2, burn_in=10, seed=1)

#print(model.kv.vocab.keys())

# 学習
model.train(epochs=200)

#print(model.kv.norm('0'))
#print(model.kv.norm('11'))
#print(model.kv.norm('8'))
#print(model.kv.norm('9'))

# 可視化
relations_set = set(relations)
figure_title = ''
vis = poincare_2d_visualization(model, relations_set, figure_title, num_nodes=None)

x = vis['data'][1]['x']
y = vis['data'][1]['y']
text = vis['data'][1]['text']

color = []
for i, _ in enumerate(x):
    if G.node[int(text[i])]['club'] == 'Mr. Hi':
        color.append('r')
    elif G.node[int(text[i])]['club'] == 'Officer':
        color.append('b')

plt.figure(figsize=(5, 5))
plt.scatter(x, y, c=color)
for i, _ in enumerate(x):
    plt.text(x[i], y[i], text[i])
plt.xticks(np.arange(-1.0, 1.2, 0.2))
plt.yticks(np.arange(-1.0, 1.2, 0.2))
plt.grid('on')
plt.show()
```
類似度に加え、階層構造を表現。非ユークリッド空間での機械学習について学習を進めること。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/254604/4be13946-0b47-6d1d-682f-e4041fb50466.png)


### DeepWalk

```python
import random
from gensim.models import word2vec

def make_random_walks(G, num_of_walk, length_of_walk):
    walks = []
    for i in range(num_of_walk):
        walk = []
        now_node = random.choice(list(G.nodes()))
        walk.append(str(now_node))
        for j in range(length_of_walk):
            next_node = random.choice(list(G.neighbors(now_node)))
            walk.append(str(next_node))
            now_node = next_node
        walks.append(walk)
    return walks

walks = make_random_walks(G, 300, 20)

#print(np.array(walks).shape)
#print(np.array(walks)[0])

# 学習
model = word2vec.Word2Vec(walks, size=2, min_count=1, window=1, 
                          seed=1, workers=1, sg=1, hs=0, negative=5, iter=1)

#vocab = model.wv.vocab
index2word = model.wv.index2word

#print(vocab)
print(index2word)

# 可視化
x = []
y = []
text = []
color = []

for node in G.nodes():
    vector = model.wv[str(node)]
    x.append(vector[0])
    y.append(vector[1])
    text.append(str(node))
    
    if G.node[node]['club'] == 'Mr. Hi':
        color.append('r')
    elif G.node[node]['club'] == 'Officer':
        color.append('b')   

plt.figure(figsize=(5, 5))
plt.scatter(x, y, c=color)
for i, _ in enumerate(x):
    plt.text(x[i], y[i], text[i])
plt.grid('on')
plt.show()
```

2次元でエンコーディング。よい結果ではない理由の考察をすること。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/254604/7c545468-ea21-9ad2-2077-5df47ce56725.png)