kmeans-crystal 0.1.2 → 0.2.0

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/kmeans-crystal.rb +20 -11
  3. metadata +5 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d58aa8e75aff1be703afe7ad5c3d89ff09d17512
4
- data.tar.gz: 51c94d22cee9f8c7f46b5a29d60b324b656bd5ae
3
+ metadata.gz: 57707a16da2fbc32a0b01dbb8d38abfc85e244d9
4
+ data.tar.gz: 33f7ee2bd5e1f41c09bcd69c5dec2bccfd8eb90a
5
5
  SHA512:
6
- metadata.gz: 6aa29a139ed0aff295ea7f17d78f22b40b1130137415cbfa6963bd20f14777df4eec21b960565988bb606b06c29a9695afc341411a4e8019e7ffd4c531ac7271
7
- data.tar.gz: b1be6bc5b49282eb210d7d39c6ccc8f66f8dd113f644fe46ebbe2dc98a3d0b07506635fa0cc99c55cd2e5b4b0ab5ded7c2a19c62d1d07dadfc94067a8e3beb11
6
+ metadata.gz: 1bbce8b354288a43713a77ea5acb5bdd243420af6c93fd4b9e2d73a08a2a69f9212b36c02aaeefc4586288dbe6c632d20668cf347aead59468783b84ac4e6554
7
+ data.tar.gz: 3dc11beedb1fb2e4d9d6716fadbba1af09b43f1895395173ffb964c1f24634dd57144621bef29dcc769cc9ad6d5ca2e424ec33e80c1bfc4478120fb10ed3d7b7
@@ -2,7 +2,7 @@ module KMeansCrystal
2
2
  class Cluster
3
3
  attr_reader :centroid
4
4
  attr_reader :entries
5
- attr_reader :name
5
+ attr_accessor :name
6
6
 
7
7
  def initialize(name, centroid, vector_name)
8
8
  @name = name
@@ -40,11 +40,12 @@ class Model
40
40
  @cluster_num = cluster_num
41
41
  @entries = entries
42
42
  @vector_name = vector_name
43
- end
44
43
 
45
- def train
46
44
  init_centroids = @entries.sample(@cluster_num).map{|x| x[@vector_name]}
47
45
  @clusters = new_clusters(init_centroids)
46
+ end
47
+
48
+ def train
48
49
  i = 0
49
50
  while true do
50
51
  @entries.each do |entry|
@@ -54,8 +55,7 @@ class Model
54
55
 
55
56
  yield(i+=1, @clusters.map{|x| x.output} )
56
57
 
57
- new_centroids = get_new_centroids(@clusters)
58
- @clusters = new_clusters(new_centroids)
58
+ @clusters = new_clusters_from_old(@clusters)
59
59
  end
60
60
  end
61
61
 
@@ -69,15 +69,16 @@ class Model
69
69
  return predicted_cluster.name
70
70
  end
71
71
 
72
- private
73
- def get_new_centroids(clusters)
74
- centroids = Array.new
75
- clusters.each do |cluster|
76
- centroids << cluster.update_centroid
72
+ def rename_clusters
73
+ named_map = Hash.new
74
+ @clusters.each{|x| named_map[x.name] = x.name }
75
+ yield(named_map)
76
+ @clusters.each do |cluster|
77
+ cluster.name = named_map[cluster.name]
77
78
  end
78
- return centroids
79
79
  end
80
80
 
81
+ private
81
82
  def get_min(centroids, entry)
82
83
  min_cluster = centroids[0]
83
84
  last_distance = centroids[0].distance(entry)
@@ -97,5 +98,13 @@ class Model
97
98
  end
98
99
  return clusters
99
100
  end
101
+
102
+ def new_clusters_from_old(clusters)
103
+ arr = Array.new
104
+ clusters.each do |cluster|
105
+ arr << Cluster.new(cluster.name, cluster.update_centroid, @vector_name)
106
+ end
107
+ return arr
108
+ end
100
109
  end
101
110
  end
metadata CHANGED
@@ -1,21 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kmeans-crystal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ireullin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-29 00:00:00.000000000 Z
11
+ date: 2016-07-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  The library for data clustering is implemented by k-means algorithm.With the library, you can monitor the model’s training processand end the training if the result is converged.
15
-
16
- 這是一個分群用的library。他實作了k-means演算法。透過這個library你可以監看整個model訓練的過程,並且在結果收斂的時候結束訓練。
17
-
18
- https://github.com/ireullin/kmeans-crystal
15
+ <br>這是一個分群用的library。他實作了k-means演算法。透過這個library你可以監看整個model訓練的過程,並且在結果收斂的時候結束訓練。
16
+ <br>https://github.com/ireullin/kmeans-crystal
19
17
  email:
20
18
  - ireullin@gmail.com
21
19
  executables: []
@@ -47,6 +45,6 @@ rubygems_version: 2.4.6
47
45
  signing_key:
48
46
  specification_version: 4
49
47
  summary: With this library, you can monitor the model’s training process and end the
50
- training if the result is converged. https://github.com/ireullin/kmeans-crystal
48
+ training if the result is converged. <br>https://github.com/ireullin/kmeans-crystal
51
49
  test_files: []
52
50
  has_rdoc: