回答編集履歴
2
誤字修正
answer
CHANGED
@@ -1,49 +1,49 @@
|
|
1
|
-
そ
|
1
|
+
そもそもなのですが、散布図を描くためには4つの特徴量のうち2つを選ぶ必要があるのではないしょうか?
|
2
|
-
同様にガウス混合分類器も4つではなく2つの特徴量で訓練する必要があるかと思います。
|
2
|
+
同様にガウス混合分類器も4つではなく2つの特徴量で訓練する必要があるかと思います。
|
3
|
-
|
3
|
+
|
4
|
-
以下`sepal length (cm)`と`sepal width (cm)`で散布図と等高線図を描く例です。
|
4
|
+
以下`sepal length (cm)`と`sepal width (cm)`で散布図と等高線図を描く例です。
|
5
|
-
```Python
|
5
|
+
```Python
|
6
|
-
|
6
|
+
|
7
|
-
from sklearn.mixture import GaussianMixture
|
7
|
+
from sklearn.mixture import GaussianMixture
|
8
|
-
from sklearn.datasets import load_iris
|
8
|
+
from sklearn.datasets import load_iris
|
9
|
-
import numpy as np
|
9
|
+
import numpy as np
|
10
|
-
import matplotlib.pyplot as plt
|
10
|
+
import matplotlib.pyplot as plt
|
11
|
-
|
11
|
+
|
12
|
-
data = load_iris()
|
12
|
+
data = load_iris()
|
13
|
-
N_CLASS = len(data.target_names) # 3
|
13
|
+
N_CLASS = len(data.target_names) # 3
|
14
|
-
LEN_CLASS = len(data.target) // N_CLASS # 50
|
14
|
+
LEN_CLASS = len(data.target) // N_CLASS # 50
|
15
|
-
|
15
|
+
|
16
|
-
vx,vy = 0,1 # 対象の2変数
|
16
|
+
vx,vy = 0,1 # 対象の2変数
|
17
|
-
x_min, x_max = min(data.data[:,vx]), max(data.data[:,vx])
|
17
|
+
x_min, x_max = min(data.data[:,vx]), max(data.data[:,vx])
|
18
|
-
y_min, y_max = min(data.data[:,vy]), max(data.data[:,vy])
|
18
|
+
y_min, y_max = min(data.data[:,vy]), max(data.data[:,vy])
|
19
|
-
|
19
|
+
|
20
|
-
# 2変数で散布図を描画
|
20
|
+
# 2変数で散布図を描画
|
21
|
-
cs = []
|
21
|
+
cs = []
|
22
|
-
for c in 'rgb':
|
22
|
+
for c in 'rgb':
|
23
|
-
cs += list(c*LEN_CLASS)
|
23
|
+
cs += list(c*LEN_CLASS)
|
24
|
-
plt.scatter(data.data[:,vx], data.data[:,vy], c=cs)
|
24
|
+
plt.scatter(data.data[:,vx], data.data[:,vy], c=cs)
|
25
|
-
plt.xlabel(data.feature_names[vx])
|
25
|
+
plt.xlabel(data.feature_names[vx])
|
26
|
-
plt.ylabel(data.feature_names[vy])
|
26
|
+
plt.ylabel(data.feature_names[vy])
|
27
|
-
plt.xlim(x_min, x_max)
|
27
|
+
plt.xlim(x_min, x_max)
|
28
|
-
plt.ylim(y_min, y_max)
|
28
|
+
plt.ylim(y_min, y_max)
|
29
|
-
|
29
|
+
|
30
|
-
# 散布図に合わせた2変数で訓練
|
30
|
+
# 散布図に合わせた2変数で訓練
|
31
|
-
model = GaussianMixture(n_components=N_CLASS, random_state=3)
|
31
|
+
model = GaussianMixture(n_components=N_CLASS, random_state=3)
|
32
|
-
model.fit(data.data[:,[vx,vy]])
|
32
|
+
model.fit(data.data[:,[vx,vy]])
|
33
|
-
|
33
|
+
|
34
|
-
# 存在する範囲内で点群を生成
|
34
|
+
# 存在する範囲内で点群を生成
|
35
|
-
N = 100
|
35
|
+
N = 100
|
36
|
-
x, y = np.meshgrid(np.linspace(x_min,x_max,N), np.linspace(y_min,y_max,N))
|
36
|
+
x, y = np.meshgrid(np.linspace(x_min,x_max,N), np.linspace(y_min,y_max,N))
|
37
|
-
X = np.array([x, y]).reshape(2, -1).T
|
37
|
+
X = np.array([x, y]).reshape(2, -1).T
|
38
|
-
probs = model.predict_proba(X)
|
38
|
+
probs = model.predict_proba(X)
|
39
|
-
|
39
|
+
|
40
|
-
# 等高線図を各クラス毎に描画
|
40
|
+
# 等高線図を各クラス毎に描画
|
41
|
-
cs = ['Reds', 'Greens', 'Blues'] # 散布図の色とは必ずしも対応しないことに注意
|
41
|
+
cs = ['Reds', 'Greens', 'Blues'] # 散布図の色とは必ずしも対応しないことに注意
|
42
|
-
assert len(cs) == N_CLASS
|
42
|
+
assert len(cs) == N_CLASS
|
43
|
-
for i in range(N_CLASS):
|
43
|
+
for i in range(N_CLASS):
|
44
|
-
plt.contourf(x, y, probs[:,i].reshape(N,N), cmap=cs[i], alpha=0.2)
|
44
|
+
plt.contourf(x, y, probs[:,i].reshape(N,N), cmap=cs[i], alpha=0.2)
|
45
|
-
plt.xlim(x_min, x_max)
|
45
|
+
plt.xlim(x_min, x_max)
|
46
|
-
plt.ylim(y_min, y_max)
|
46
|
+
plt.ylim(y_min, y_max)
|
47
|
-
plt.show()
|
47
|
+
plt.show()
|
48
|
-
```
|
48
|
+
```
|
49
49
|

|
1
コード修正
answer
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
|
4
4
|
以下`sepal length (cm)`と`sepal width (cm)`で散布図と等高線図を描く例です。
|
5
5
|
```Python
|
6
|
+
|
6
7
|
from sklearn.mixture import GaussianMixture
|
7
8
|
from sklearn.datasets import load_iris
|
8
9
|
import numpy as np
|
@@ -12,23 +13,27 @@
|
|
12
13
|
N_CLASS = len(data.target_names) # 3
|
13
14
|
LEN_CLASS = len(data.target) // N_CLASS # 50
|
14
15
|
|
16
|
+
vx,vy = 0,1 # 対象の2変数
|
17
|
+
x_min, x_max = min(data.data[:,vx]), max(data.data[:,vx])
|
18
|
+
y_min, y_max = min(data.data[:,vy]), max(data.data[:,vy])
|
19
|
+
|
15
20
|
# 2変数で散布図を描画
|
16
21
|
cs = []
|
17
22
|
for c in 'rgb':
|
18
23
|
cs += list(c*LEN_CLASS)
|
19
|
-
plt.scatter(data.data[:,
|
24
|
+
plt.scatter(data.data[:,vx], data.data[:,vy], c=cs)
|
20
|
-
plt.xlabel(data.feature_names[
|
25
|
+
plt.xlabel(data.feature_names[vx])
|
21
|
-
plt.ylabel(data.feature_names[
|
26
|
+
plt.ylabel(data.feature_names[vy])
|
22
|
-
plt.xlim(
|
27
|
+
plt.xlim(x_min, x_max)
|
23
|
-
plt.ylim(
|
28
|
+
plt.ylim(y_min, y_max)
|
24
29
|
|
25
30
|
# 散布図に合わせた2変数で訓練
|
26
31
|
model = GaussianMixture(n_components=N_CLASS, random_state=3)
|
27
|
-
model.fit(data.data[:,
|
32
|
+
model.fit(data.data[:,[vx,vy]])
|
28
33
|
|
29
34
|
# 存在する範囲内で点群を生成
|
30
35
|
N = 100
|
31
|
-
x, y = np.meshgrid(np.linspace(
|
36
|
+
x, y = np.meshgrid(np.linspace(x_min,x_max,N), np.linspace(y_min,y_max,N))
|
32
37
|
X = np.array([x, y]).reshape(2, -1).T
|
33
38
|
probs = model.predict_proba(X)
|
34
39
|
|
@@ -37,8 +42,8 @@
|
|
37
42
|
assert len(cs) == N_CLASS
|
38
43
|
for i in range(N_CLASS):
|
39
44
|
plt.contourf(x, y, probs[:,i].reshape(N,N), cmap=cs[i], alpha=0.2)
|
40
|
-
plt.xlim(
|
45
|
+
plt.xlim(x_min, x_max)
|
41
|
-
plt.ylim(
|
46
|
+
plt.ylim(y_min, y_max)
|
42
47
|
plt.show()
|
43
48
|
```
|
44
|
-

|