回答編集履歴
2
numpy\.nan_to_num例を追加
answer
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
`== np.nan`ではなく[numpy.isnan](https://docs.scipy.org/doc/numpy/reference/generated/numpy.isnan.html)で判定してください。
|
2
|
-
さらに[numpy.where](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.where.html)を使えば一括置換できます。
|
2
|
+
さらに[numpy.where](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.where.html)や[numpy.nan_to_num](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.nan_to_num.html)を使えば一括置換できます。
|
3
3
|
```Python
|
4
4
|
import numpy as np
|
5
5
|
G = np.array([[1,2,np.nan],[4,np.nan,6]])
|
@@ -9,6 +9,10 @@
|
|
9
9
|
G2 = np.where( np.isnan(G),0,G)
|
10
10
|
print(G2)
|
11
11
|
|
12
|
+
# numpy.nan_to_numで一括置換
|
13
|
+
G3 = np.nan_to_num(G)
|
14
|
+
print(G3)
|
15
|
+
|
12
16
|
for i in range(G.shape[0]):
|
13
17
|
for j in range(G.shape[1]):
|
14
18
|
if np.isnan(G[i,j]):
|
1
numpy\.where例を追加
answer
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
`== np.nan`ではなく[numpy.isnan](https://docs.scipy.org/doc/numpy/reference/generated/numpy.isnan.html)で判定してください。
|
2
|
-
|
2
|
+
さらに[numpy.where](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.where.html)を使えば一括置換できます。
|
3
3
|
```Python
|
4
4
|
import numpy as np
|
5
5
|
G = np.array([[1,2,np.nan],[4,np.nan,6]])
|
6
6
|
print(G)
|
7
|
+
|
8
|
+
# numpy.whereで一括置換
|
9
|
+
G2 = np.where( np.isnan(G),0,G)
|
10
|
+
print(G2)
|
11
|
+
|
7
12
|
for i in range(G.shape[0]):
|
8
13
|
for j in range(G.shape[1]):
|
9
14
|
if np.isnan(G[i,j]):
|