t_learn 0.1.1.5 → 0.1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a5ec34f34ed37b9d565989b377f3d73def58395
4
- data.tar.gz: 96fb03c78080151b6c3dd171cfbdda4218a3c916
3
+ metadata.gz: 498f21fc8a9db10db42ac7cad564f372eb5e9b0c
4
+ data.tar.gz: 9b1c1a70545014cc35dde9191660cdb502948e1c
5
5
  SHA512:
6
- metadata.gz: 110a637a57bb8ecbe136c5b674fdfe77eb9faf4491373f9afc2c66a8d19ef976da93f3acb1c18527813dfeb12fab701a7ea6af35194eb9500211a17406c3f08a
7
- data.tar.gz: 0d8ff3858f5e65c3f20f8e54f88a3d4b3bc4a69579212fc1d76f37a00cdde4511861a69c952635dc59aa4e0bbac4f1ae6c3452e5223416b7189a6d608a6ab990
6
+ metadata.gz: 6d91f5751e8fda7d1d99e513dab12bd281b572b8beaf6f2eef99c78418a50583ed39a27538925aeaa90062c45adb7361e94b03756aa76002d5f519bd681bf227
7
+ data.tar.gz: 947dc97fdd84c07bf3f395f339b16f93a72719a66ed47203f092843871500ebf0dbc11b5ddf1c5569cb999568dead4f6b6422dd9126689f5ca6446f81e6d2ae0
data/Gemfile CHANGED
@@ -5,5 +5,4 @@ gemspec
5
5
 
6
6
  group :development, :test do
7
7
  gem 'pry-rails'
8
- gem 'matrix'
9
8
  end
data/README.md CHANGED
@@ -90,7 +90,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
90
90
 
91
91
  ## Contributing
92
92
 
93
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/t_learn. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Tcom242242/t_learn. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
94
94
 
95
95
 
96
96
  ## License
data/lib/t_learn/em.rb CHANGED
@@ -4,6 +4,7 @@
4
4
  require "yaml"
5
5
  require "json"
6
6
  require "pry"
7
+ require "matrix"
7
8
 
8
9
  module TLearn
9
10
 
@@ -11,11 +12,11 @@ module TLearn
11
12
  attr_accessor :mu_list, :conv_list, :pi_list, :log_likelihood, :k_num,:data_list, :real_data_list
12
13
 
13
14
  def init(data_list, k_num)
14
- @k_num = k_num # ガウス分布の数
15
+ @k_num = k_num
15
16
  @data_list = data_list
16
17
  @dim = @data_list[0].size
17
18
  # @data_list = scale(@data_list)
18
- data_ave_std =calc_first_ave_std(@data_list)
19
+ data_ave_std = calc_first_ave_std(@data_list)
19
20
  @real_data_list = Marshal.load(Marshal.dump(@data_list))
20
21
  @mu_list = Array.new(@k_num).map{ini_ave(data_ave_std[:ave_list])}
21
22
  @conv_list = Array.new(@k_num).map{ini_conv(data_ave_std[:std_list])}
@@ -51,8 +52,9 @@ module TLearn
51
52
  return array
52
53
  end
53
54
 
54
- def create_log(cycle)
55
+ def create_log(cycle, likelihood)
55
56
  log = {:cycle => cycle,
57
+ :likelihood => likelihood,
56
58
  :mu => @mu_list.clone,
57
59
  :conv => @conv_list.clone,
58
60
  :pi_list => @pi_list.clone}
@@ -72,7 +74,7 @@ module TLearn
72
74
  diff = (likelihood - last_likelihood).abs
73
75
  last_likelihood = likelihood
74
76
  puts "likelihood: #{likelihood}"
75
- result.push(create_log(cycle))
77
+ result.push(create_log(cycle, likelihood))
76
78
  cycle += 1
77
79
  break if diff < 0.000001
78
80
  end
@@ -120,7 +122,6 @@ module TLearn
120
122
  return mu
121
123
  end
122
124
 
123
-
124
125
  def calc_conv(k, nk)
125
126
  conv = Array.new(@dim).map{Array.new(@dim, 0)}
126
127
  @dim.times{|i|
@@ -130,7 +131,11 @@ module TLearn
130
131
  }
131
132
  }
132
133
  }
133
- conv = conv.map{|arr| arr.map{|v| v/nk}}
134
+ conv = conv.map{|arr|
135
+ arr.map{|v|
136
+ (v/nk) != 0.0 ? (v/nk) : 0.1
137
+ }
138
+ }
134
139
  return conv
135
140
  end
136
141
 
@@ -146,23 +151,6 @@ module TLearn
146
151
  return log_likelihood
147
152
  end
148
153
 
149
-
150
- #
151
- # === 混合正規分布を生成する
152
- #
153
- def gaussian_mix()
154
- r1_x = normal_rand()
155
- r1_y = normal_rand()
156
- r2_x = normal_rand(10.0, 50.0)
157
- r2_y = normal_rand(10.0, 50.0)
158
-
159
- if (rand() < 0.15)
160
- return [r1_x, r1_y]
161
- else
162
- return [r2_x, r2_y]
163
- end
164
- end
165
-
166
154
  #
167
155
  # === gauusian distribution
168
156
  #
@@ -175,24 +163,20 @@ module TLearn
175
163
  f2 = Math.exp(-(((x-mu)**2)/((2.0*sigma))))
176
164
  return f1 * f2
177
165
  else
178
- return gauusian2dim(x, mu, sigma)
166
+ return gauusian_over_2dim(x, mu, sigma)
179
167
  end
180
168
  end
181
169
 
182
170
 
183
171
  #
184
- # === gauusian distribution .2 dim version
172
+ # === gauusian distribution over 2 dim version
185
173
  #
186
- def gauusian2dim(x, mu, conv)
174
+ def gauusian_over_2dim(x, mu, conv)
187
175
  x = Matrix[x]
188
176
  mu = Matrix[mu]
189
177
  conv = Matrix[*conv]
190
- begin
191
- f1 = 1.0/(2.0 * Math::PI * ( conv.det**(0.5) ))
192
- f2 = Math.exp((-1.0/2.0)*((x-mu) * conv.inverse * (x-mu).transpose)[0, 0])
193
- rescue
194
- binding.pry ;
195
- end
178
+ f1 = 1.0/(((2.0 * Math::PI)**(@dim/2.0)) * ( conv.det**(0.5) ))
179
+ f2 = Math.exp((-1.0/2.0)*((x-mu) * conv.inverse * (x-mu).transpose)[0, 0])
196
180
 
197
181
  return (f1 * f2)
198
182
  end
@@ -203,7 +187,7 @@ module TLearn
203
187
  std_list = []
204
188
  x.each{|vec|
205
189
  vec.each_with_index{|data, i|
206
- sum_each_vec[i] = (sum_each_vec[i] == nil) ? data : sum_each_vec[i]+data
190
+ sum_each_vec[i] = (sum_each_vec[i] == nil) ? data : sum_each_vec[i] + data
207
191
  }
208
192
  }
209
193
  x[0].size.times{|i| ave_list.push(sum_each_vec[i]/x.size)}
@@ -214,53 +198,47 @@ module TLearn
214
198
  sum_each_vec[i] = (sum_each_vec[i] == nil) ? (ave_list[i]-data)**2 : (sum_each_vec[i]+(ave_list[i]-data)**2)
215
199
  }
216
200
  }
217
- x[0].size.times{|i| std_list.push(Math.sqrt(sum_each_vec[i]/x.size))}
201
+ x[0].size.times{|i|
202
+ std = Math.sqrt(sum_each_vec[i]/x.size)
203
+ std = 0.1 if std == 0.0
204
+ std_list.push(std)
205
+ }
218
206
 
219
207
  return {:ave_list => ave_list, :std_list => std_list}
220
208
  end
221
209
 
210
+
222
211
  def scale(x)
223
- if x[0].instance_of?(Array) # check whether x's factor is 1dim or over 2dim
224
- sum_each_vec = []
225
- ave_list = []
226
- std_list = []
227
- x.each{|vec|
228
- vec.each_with_index{|data, i|
229
- sum_each_vec[i] = (sum_each_vec[i] == nil) ? data : sum_each_vec[i]+data
230
- }
231
- }
232
- x[0].size.times{|i|
233
- ave_list.push(sum_each_vec[i]/x.size)
212
+ sum_each_vec = []
213
+ ave_list = []
214
+ std_list = []
215
+ x.each{|vec|
216
+ vec.each_with_index{|data, i|
217
+ sum_each_vec[i] = (sum_each_vec[i] == nil) ? data : sum_each_vec[i]+data
234
218
  }
219
+ }
220
+ x[0].size.times{|i|
221
+ ave_list.push(sum_each_vec[i]/x.size)
222
+ }
235
223
 
236
- sum_each_vec = []
237
- x.each{|vec|
238
- vec.each_with_index{|data, i|
239
- sum_each_vec[i] = (sum_each_vec[i] == nil) ? (ave_list[i]-data)**2 : (sum_each_vec[i]+(ave_list[i]-data)**2)
240
- }
241
- }
242
- x[0].size.times{|i|
243
- std_list.push(Math.sqrt(sum_each_vec[i]/x.size))
224
+ sum_each_vec = []
225
+ x.each{|vec|
226
+ vec.each_with_index{|data, i|
227
+ sum_each_vec[i] = (sum_each_vec[i] == nil) ? (ave_list[i]-data)**2 : (sum_each_vec[i]+(ave_list[i]-data)**2)
244
228
  }
229
+ }
230
+ x[0].size.times{|i|
231
+ std_list.push(Math.sqrt(sum_each_vec[i]/x.size))
232
+ }
245
233
 
246
- scaled_x = []
247
- x.each_with_index{|vec, i|
248
- scaled_x[i] ||= []
249
- vec.each_with_index{|data, j|
250
- scaled_x[i][j] ||= (data-ave_list[j])/std_list[j]
251
- }
234
+ scaled_x = []
235
+ x.each_with_index{|vec, i|
236
+ scaled_x[i] ||= []
237
+ vec.each_with_index{|data, j|
238
+ scaled_x[i][j] ||= (data-ave_list[j])/std_list[j]
252
239
  }
253
- return scaled_x
254
- else # if 1dim
255
- mu = x.each.inject(0.0){|sum, data| sum+=data}/x.size
256
- var = (x.each.inject(0.0){|sum, data| sum+=(data-mu)**2}/x.size)
257
- std = Math.sqrt(var)
258
- scaled_x = []
259
- x.each{|sum,data|
260
- scaled_x.push((mu-data)/std)
261
- }
262
- return scaled_x
263
- end
240
+ }
241
+ return scaled_x
264
242
  end
265
243
  end
266
244
  end
@@ -1,3 +1,3 @@
1
1
  module TLearn
2
- VERSION = "0.1.1.5"
2
+ VERSION = "0.1.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: t_learn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.5
4
+ version: 0.1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tcom242242
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-04 00:00:00.000000000 Z
11
+ date: 2017-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,7 +60,6 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
- - ".rake_tasks~"
64
63
  - ".rspec"
65
64
  - ".travis.yml"
66
65
  - CODE_OF_CONDUCT.md
data/.rake_tasks~ DELETED
@@ -1,7 +0,0 @@
1
- build
2
- clean
3
- clobber
4
- install
5
- install:local
6
- release[remote]
7
- spec