Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/node_classification/utils.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 def _propagate(P, F, B): | |
2 """Propagate labels by one step | |
3 | |
4 Parameters | |
5 ---------- | |
6 P : scipy sparse matrix, shape = [n_samples, n_samples] | |
7 Propagation matrix | |
8 F : numpy array, shape = [n_samples, n_classes] | |
9 Label matrix | |
10 B : numpy array, shape = [n_samples, n_classes] | |
11 Base matrix | |
12 | |
13 Returns | |
14 ---------- | |
15 F_new : array, shape = [n_samples, n_classes] | |
16 Label matrix | |
17 """ | |
18 F_new = P.dot(F) + B | |
19 return F_new | |
20 | |
21 | |
22 def _get_label_info(G, label_name): | |
23 """Get and return information of labels from the input graph | |
24 | |
25 Parameters | |
26 ---------- | |
27 G : Network X graph | |
28 label_name : string | |
29 Name of the target label | |
30 | |
31 Returns | |
32 ---------- | |
33 labels : numpy array, shape = [n_labeled_samples, 2] | |
34 Array of pairs of labeled node ID and label ID | |
35 label_dict : numpy array, shape = [n_classes] | |
36 Array of labels | |
37 i-th element contains the label corresponding label ID `i` | |
38 """ | |
39 import numpy as np | |
40 | |
41 labels = [] | |
42 label_to_id = {} | |
43 lid = 0 | |
44 for i, n in enumerate(G.nodes(data=True)): | |
45 if label_name in n[1]: | |
46 label = n[1][label_name] | |
47 if label not in label_to_id: | |
48 label_to_id[label] = lid | |
49 lid += 1 | |
50 labels.append([i, label_to_id[label]]) | |
51 labels = np.array(labels) | |
52 label_dict = np.array( | |
53 [label for label, _ in sorted(label_to_id.items(), key=lambda x: x[1])] | |
54 ) | |
55 return (labels, label_dict) | |
56 | |
57 | |
58 def _init_label_matrix(n_samples, n_classes): | |
59 """Create and return zero matrix | |
60 | |
61 Parameters | |
62 ---------- | |
63 n_samples : integer | |
64 The number of nodes (samples) on the input graph | |
65 n_classes : integer | |
66 The number of classes (distinct labels) on the input graph | |
67 | |
68 Returns | |
69 ---------- | |
70 F : numpy array, shape = [n_samples, n_classes] | |
71 Label matrix | |
72 """ | |
73 import numpy as np | |
74 | |
75 F = np.zeros((n_samples, n_classes)) | |
76 return F | |
77 | |
78 | |
79 def _predict(F, label_dict): | |
80 """Predict labels by learnt label matrix | |
81 | |
82 Parameters | |
83 ---------- | |
84 F : numpy array, shape = [n_samples, n_classes] | |
85 Learnt (resulting) label matrix | |
86 label_dict : numpy array, shape = [n_classes] | |
87 Array of labels | |
88 i-th element contains the label corresponding label ID `i` | |
89 | |
90 Returns | |
91 ---------- | |
92 predicted : numpy array, shape = [n_samples] | |
93 Array of predicted labels | |
94 """ | |
95 import numpy as np | |
96 | |
97 predicted_label_ids = np.argmax(F, axis=1) | |
98 predicted = label_dict[predicted_label_ids].tolist() | |
99 return predicted |