Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / node_classification / utils.py @ 5cef0f13

History | View | Annotate | Download (2.59 KB)

1
# -*- coding: utf-8 -*-
2
#
3
# Author: Yuto Yamaguchi <yuto.ymgc@gmail.com>
4

    
5
def _propagate(P, F, B):
6
    """Propagate labels by one step
7

8
    Parameters
9
    ----------
10
    P : scipy sparse matrix, shape = [n_samples, n_samples]
11
        Propagation matrix
12
    F : numpy array, shape = [n_samples, n_classes]
13
        Label matrix
14
    B : numpy array, shape = [n_samples, n_classes]
15
        Base matrix
16

17
    Returns
18
    ----------
19
    F_new : array, shape = [n_samples, n_classes]
20
        Label matrix
21
    """
22
    F_new = P.dot(F) + B
23
    return F_new
24

    
25

    
26
def _get_label_info(G, label_name):
27
    """Get and return information of labels from the input graph
28

29
    Parameters
30
    ----------
31
    G : Network X graph
32
    label_name : string
33
        Name of the target label
34

35
    Returns
36
    ----------
37
    labels : numpy array, shape = [n_labeled_samples, 2]
38
        Array of pairs of labeled node ID and label ID
39
    label_dict : numpy array, shape = [n_classes]
40
        Array of labels
41
        i-th element contains the label corresponding label ID `i`
42
    """
43
    import numpy as np
44

    
45
    labels = []
46
    label_to_id = {}
47
    lid = 0
48
    for i, n in enumerate(G.nodes(data=True)):
49
        if label_name in n[1]:
50
            label = n[1][label_name]
51
            if label not in label_to_id:
52
                label_to_id[label] = lid
53
                lid += 1
54
            labels.append([i, label_to_id[label]])
55
    labels = np.array(labels)
56
    label_dict = np.array([label for label, _ in sorted(
57
        label_to_id.items(), key=lambda x:x[1])])
58
    return (labels, label_dict)
59

    
60

    
61
def _init_label_matrix(n_samples, n_classes):
62
    """Create and return zero matrix
63

64
    Parameters
65
    ----------
66
    n_samples : integer
67
        The number of nodes (samples) on the input graph
68
    n_classes : integer
69
        The number of classes (distinct labels) on the input graph
70

71
    Returns
72
    ----------
73
    F : numpy array, shape = [n_samples, n_classes]
74
        Label matrix
75
    """
76
    import numpy as np
77

    
78
    F = np.zeros((n_samples, n_classes))
79
    return F
80

    
81

    
82
def _predict(F, label_dict):
83
    """Predict labels by learnt label matrix
84

85
    Parameters
86
    ----------
87
    F : numpy array, shape = [n_samples, n_classes]
88
        Learnt (resulting) label matrix
89
    label_dict : numpy array, shape = [n_classes]
90
        Array of labels
91
        i-th element contains the label corresponding label ID `i`
92

93
    Returns
94
    ----------
95
    predicted : numpy array, shape = [n_samples]
96
        Array of predicted labels
97
    """
98
    import numpy as np
99

    
100
    predicted_label_ids = np.argmax(F, axis=1)
101
    predicted = label_dict[predicted_label_ids].tolist()
102
    return predicted