teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

出力された図を加えました

2020/01/27 10:03

投稿

ygtygtygt
ygtygtygt

スコア7

title CHANGED
File without changes
body CHANGED
@@ -4,24 +4,42 @@
4
4
 
5
5
  混合ガウスモデルのアルゴリズムは理解しているのですがこのプログラムの出力値が何を示しているか自分の知識不足のせいでわかりません。
6
6
  自分で考えた中では入力データの負担率だと思うのですがあってますか?
7
+ このプログラムにおけるy_trueとy_estimatedは何を表していますか?
8
+
7
9
  負担率とは、あるデータxにおいて、k番目のクラスの正規分布からの観測される確率のことです。別の言い方をすると、あるデータxにおける混合正規分布の割合とも考えられそうです。
8
10
 
9
11
  3つのクラスA,B,Cに分かれると想定した場合1つのデータを入力すると
10
- [クラスAに属する確率,クラスBに属する確率,クラスCに属する確率] = [0.5, 0.3, 0.2]
12
+ [クラスAに属する確率,クラスBに属する確率,クラスCに属する確率] = [0.5, 0.3, 0.2]と出るはずです.
11
13
 
12
14
  加えて負担率はクラスごとに計算されるものだと思うのですがこのプログラムで負担率はクラスごとに計算されますか?
13
15
 
14
- このプログラムでは入力データが100個なのですがいずれは30個で行いたいと思ってます。
15
- ちなみに16個のデータを入力したら重ね合わせる最適なガウス分布の個数は1で出力データは入力データの個数と同じ個数出てきました。
16
+ ちなみに25個のデータを入力したら重ね合わせる最適なガウス分布の個数は1で出力データは入力データの個数と同じ個数出てきました。
16
17
 
17
18
  入力データ:
18
- [1,2,2,1,2,1,2,3,3,1,2,2,3,4,1,1]
19
+ [1. 1. 1. 2. 1. 1. 1. 2. 1. 2. 3. 1. 3. 1. 2. 4. 1. 2. 4. 1. 1. 2. 5. 1. 1. ]
19
20
 
20
21
  出力データ:
22
+ <y_true>
21
- [0.00979, 0.19649, 0.19649, 0.00979, 0.19649, 0.00979, 0.19649, 0.96889, 0.96889, 0.00979, 0.19649, 0.19649, 0.96889, 0.99995, 0.00979, 0.00979]
23
+ [0.41558912 0.57036429 0.52914185 0.48112792 0.60293698 0.46554157
24
+ 0.52676959 0.54020389 0.37723733 0.44722378 0.56863281 0.58951637
25
+ 0.51391066 0.51027414 0.58159079 0.45768141 0.45611032 0.43729612
26
+ 0.55446468 0.48241021 0.45572774 0.55418018 0.51759982 0.44739818
27
+ 0.45398047]
22
28
 
29
+ <y_estimated>
30
+ [0.49164472 0.49164472 0.49164472 0.50693811 0.49164472 0.49164472
31
+ 0.49164472 0.50693811 0.49164472 0.50693811 0.52245777 0.49164472
32
+ 0.52245777 0.49164472 0.50693811 0.5224578 0.49164472 0.50693811
33
+ 0.5224578 0.49164472 0.49164472 0.50693811 0.5224578 0.49164472
34
+ 0.49164472]
23
35
 
36
+ <図1>
37
+ ![イメージ説明](2caaa2a8c81d421942d37dcc1976ab0a.png)
24
38
 
39
+ <図2>
40
+ ![イメージ説明](3f4370f4cb893ba815d3f02b1679a34b.png)
41
+
42
+
25
43
  ``````ここに言語を入力
26
44
  python
27
45
  ```
@@ -44,14 +62,14 @@
44
62
  sigma = np.array([0.05, 0.05, 0.03])
45
63
 
46
64
 
47
- #sample.csvの3列目の値を取得
65
+
48
66
  list = []
49
67
  with open('data/src/sample.csv') as f:#ファイル名は架空のものを記載
50
68
  reader = csv.reader(f)
51
69
  for row in reader:
52
- list.append(row[2])
70
+ list.append(row[3])#sample.csvの4列目の値を取得
53
- float = float(list)#文字列をfloat型に変換
71
+ float = [float(s) for s in list]#文字列をfloat型に変換
54
- x = np.array(list)#numpy型に変換 xは入力したデータ
72
+ x = np.array(float)#numpy型に変換 xは入力したデータ
55
73
 
56
74
  y_true = np.random.normal(0.5, 0.05, len(x))
57
75
  for i in range(num_peak_true):
@@ -99,7 +117,8 @@
99
117
 
100
118
  #出力データ
101
119
  print(y_true)
102
-
120
+ print(y_estimated)
121
+
103
122
  # gaussian fit
104
123
  fig, ax = plt.subplots(figsize=(6, 4))
105
124
  plt.scatter(x, y_true, label='true data')
@@ -110,7 +129,7 @@
110
129
  plt.legend()
111
130
  plt.tight_layout()
112
131
  plt.savefig("mix_gauss_fit.png")
113
- plt.show()
132
+ plt.show()#図1
114
133
  plt.close()
115
134
 
116
135
  # BIC
@@ -122,6 +141,6 @@
122
141
  plt.xticks(x_bic)
123
142
  plt.tight_layout()
124
143
  plt.savefig("bic.png")
125
- plt.show()
144
+ plt.show()#図2
126
145
  plt.close()
127
146
  ```[リンク内容](https://omedstu.jimdofree.com/2018/12/01/scipyによる1次元混合ガウス回帰/)

1

入力データと出力データをコードに反映させました。

2020/01/27 10:03

投稿

ygtygtygt
ygtygtygt

スコア7

title CHANGED
File without changes
body CHANGED
@@ -32,6 +32,7 @@
32
32
  import matplotlib.pyplot as plt
33
33
  from tqdm import tqdm
34
34
  import math
35
+ import csv
35
36
 
36
37
  """Setting up test data"""
37
38
  def gaussian_func(x, A, mu, sigma):
@@ -42,8 +43,15 @@
42
43
  mu = np.array([0.2, 0.4, 0.8])
43
44
  sigma = np.array([0.05, 0.05, 0.03])
44
45
 
46
+
45
- num_sample = 100
47
+ #sample.csvの3列目の値を取得
48
+ list = []
49
+ with open('data/src/sample.csv') as f:#ファイル名は架空のものを記載
50
+ reader = csv.reader(f)
51
+ for row in reader:
52
+ list.append(row[2])
53
+ float = float(list)#文字列をfloat型に変換
46
- x = np.linspace(0, 1, num_sample)
54
+ x = np.array(list)#numpy型に変換 xは入力したデータ
47
55
 
48
56
  y_true = np.random.normal(0.5, 0.05, len(x))
49
57
  for i in range(num_peak_true):
@@ -87,8 +95,10 @@
87
95
  """plot results"""
88
96
  y_estimated = plsq_global_opt[0]
89
97
  for i in range(num_peak_estimated):
90
- y_estimated += gaussian_func(x, plsq_global_opt[3*i+1], plsq_global_opt[3*i+2],
98
+ y_estimated += gaussian_func(x, plsq_global_opt[3*i+1], plsq_global_opt[3*i+2],plsq_global_opt[3*(i+1)])
99
+
100
+ #出力データ
91
- plsq_global_opt[3*(i+1)])
101
+ print(y_true)
92
102
 
93
103
  # gaussian fit
94
104
  fig, ax = plt.subplots(figsize=(6, 4))