回答編集履歴

4

修正

2020/07/19 14:48

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -18,7 +18,9 @@
18
18
 
19
19
 
20
20
 
21
- n_nodes = df[["node_1", "node_2"]].values.ravel().max() + 1
21
+ name_to_node = {name: i for i, name in enumerate(np.unique(df[["node_1", "node_2"]].values))}
22
+
23
+ n_nodes = len(name_to_node)
22
24
 
23
25
  A = np.zeros((n_nodes, n_nodes))
24
26
 
@@ -26,7 +28,13 @@
26
28
 
27
29
  for row in df.itertuples():
28
30
 
31
+ n1 = name_to_node[row.node_1]
32
+
33
+ n2 = name_to_node[row.node_2]
34
+
29
- A[row.node_1, row.node_2] += row.weight
35
+ A[n1, n2] += row.weight
36
+
37
+ A[n2, n1] += row.weight
30
38
 
31
39
 
32
40
 
@@ -38,22 +46,22 @@
38
46
 
39
47
  ```
40
48
 
41
- [[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
42
49
 
43
- [ 0. 0. 0. 0. 35. 30. 30. 0. 0.]
44
50
 
45
- [ 0. 0. 0. 0. 0. 0. 0. 20. 0.]
51
+ [[ 0. 0. 0. 35. 30. 30. 0. 0.]
46
52
 
47
- [ 0. 0. 0. 0. 0. 0. 0. 0. 15.]
53
+ [ 0. 0. 0. 0. 0. 0. 20. 0.]
48
54
 
49
- [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
55
+ [ 0. 0. 0. 0. 0. 0. 0. 15.]
50
56
 
51
- [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
57
+ [35. 0. 0. 0. 0. 0. 0. 0.]
52
58
 
53
- [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
59
+ [30. 0. 0. 0. 0. 0. 0. 0.]
54
60
 
55
- [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
61
+ [30. 0. 0. 0. 0. 0. 0. 0.]
56
62
 
63
+ [ 0. 20. 0. 0. 0. 0. 0. 0.]
64
+
57
- [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
65
+ [ 0. 0. 15. 0. 0. 0. 0. 0.]]
58
66
 
59
67
  ```

3

修正

2020/07/19 14:48

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -28,8 +28,6 @@
28
28
 
29
29
  A[row.node_1, row.node_2] += row.weight
30
30
 
31
- A[row.node_2, row.node_1] += row.weight
32
-
33
31
 
34
32
 
35
33
  print(A)
@@ -48,14 +46,14 @@
48
46
 
49
47
  [ 0. 0. 0. 0. 0. 0. 0. 0. 15.]
50
48
 
51
- [ 0. 35. 0. 0. 0. 0. 0. 0. 0.]
49
+ [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
52
50
 
53
- [ 0. 30. 0. 0. 0. 0. 0. 0. 0.]
51
+ [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
54
52
 
55
- [ 0. 30. 0. 0. 0. 0. 0. 0. 0.]
53
+ [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
56
54
 
57
- [ 0. 0. 20. 0. 0. 0. 0. 0. 0.]
55
+ [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
58
56
 
59
- [ 0. 0. 0. 15. 0. 0. 0. 0. 0.]]
57
+ [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
60
58
 
61
59
  ```

2

修正

2020/07/19 14:27

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -28,26 +28,34 @@
28
28
 
29
29
  A[row.node_1, row.node_2] += row.weight
30
30
 
31
+ A[row.node_2, row.node_1] += row.weight
32
+
31
33
 
32
34
 
33
35
  print(A)
34
36
 
35
- # [[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
37
+ ```
36
38
 
37
- # [ 0. 0. 0. 0. 35. 30. 30. 0. 0.]
38
39
 
39
- # [ 0. 0. 0. 0. 0. 0. 0. 20. 0.]
40
-
41
- # [ 0. 0. 0. 0. 0. 0. 0. 0. 15.]
42
-
43
- # [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
44
-
45
- # [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
46
-
47
- # [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
48
-
49
- # [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
50
-
51
- # [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
52
40
 
53
41
  ```
42
+
43
+ [[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
44
+
45
+ [ 0. 0. 0. 0. 35. 30. 30. 0. 0.]
46
+
47
+ [ 0. 0. 0. 0. 0. 0. 0. 20. 0.]
48
+
49
+ [ 0. 0. 0. 0. 0. 0. 0. 0. 15.]
50
+
51
+ [ 0. 35. 0. 0. 0. 0. 0. 0. 0.]
52
+
53
+ [ 0. 30. 0. 0. 0. 0. 0. 0. 0.]
54
+
55
+ [ 0. 30. 0. 0. 0. 0. 0. 0. 0.]
56
+
57
+ [ 0. 0. 20. 0. 0. 0. 0. 0. 0.]
58
+
59
+ [ 0. 0. 0. 15. 0. 0. 0. 0. 0.]]
60
+
61
+ ```

1

修正

2020/07/19 14:27

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -18,9 +18,9 @@
18
18
 
19
19
 
20
20
 
21
- n_nodes = df[["node_1", "node_2"]].values.ravel().max()
21
+ n_nodes = df[["node_1", "node_2"]].values.ravel().max() + 1
22
22
 
23
- A = np.zeros((n_nodes + 1, n_nodes + 1))
23
+ A = np.zeros((n_nodes, n_nodes))
24
24
 
25
25
 
26
26