ckmeans 0.1.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 72a2d84963628565eb94d962dc73aa1230f7af8c6948dc6713b0f9582d1bb401
4
+ data.tar.gz: ec3cb0c09eaaf38147d1a7dfd1f772b960905f336b801aade402d6f795329d27
5
+ SHA512:
6
+ metadata.gz: ceb63e72327d2f3a00aee2c23c7ba8bd63f835d7b0132ce0785f9636b550a79ea4827eadfeb9a6d95c36e9b046ae5005325adeaf2cdd1689444d8f7af181bbc1
7
+ data.tar.gz: 0d7d7ca2c942ecb238c7d26cc601d64187ae97218bdd59aeb6ca067d11f08472725e6b3e8ca9021be88d63aa4ae45a5d629a5f8966a3731b7941aa6c0a205619
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 3.1
5
+ NewCops: enable
6
+
7
+ Style/StringLiterals:
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ EnforcedStyle: double_quotes
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,62 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2025-03-31 15:04:58 UTC using RuboCop version 1.75.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 7
10
+ # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
11
+ Metrics/AbcSize:
12
+ Max: 95
13
+
14
+ # Offense count: 3
15
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
16
+ # AllowedMethods: refine
17
+ Metrics/BlockLength:
18
+ Max: 112
19
+
20
+ # Offense count: 3
21
+ # Configuration parameters: AllowedMethods, AllowedPatterns.
22
+ Metrics/CyclomaticComplexity:
23
+ Max: 10
24
+
25
+ # Offense count: 5
26
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
27
+ Metrics/MethodLength:
28
+ Max: 48
29
+
30
+ # Offense count: 5
31
+ # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
32
+ Metrics/ParameterLists:
33
+ Max: 9
34
+
35
+ # Offense count: 3
36
+ # Configuration parameters: AllowedMethods, AllowedPatterns.
37
+ Metrics/PerceivedComplexity:
38
+ Max: 13
39
+
40
+ # Offense count: 13
41
+ # Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
42
+ # AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to
43
+ Naming/MethodParameterName:
44
+ Exclude:
45
+ - 'lib/ckmeans/clusterer.rb'
46
+
47
+ # Offense count: 1
48
+ # Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
49
+ # SupportedStyles: snake_case, normalcase, non_integer
50
+ # AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
51
+ Naming/VariableNumber:
52
+ Exclude:
53
+ - 'lib/ckmeans/clusterer.rb'
54
+
55
+ # Offense count: 6
56
+ # This cop supports unsafe autocorrection (--autocorrect-all).
57
+ # Configuration parameters: EnforcedStyle, AllowedMethods, AllowedPatterns.
58
+ # SupportedStyles: predicate, comparison
59
+ Style/NumericPredicate:
60
+ Exclude:
61
+ - 'spec/**/*'
62
+ - 'lib/ckmeans/clusterer.rb'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.2] - 2025-03-31
4
+
5
+ - https://github.com/vlebedeff/rb-ckmeans/pull/3
6
+
7
+ ## [0.1.1] - 2025-03-31
8
+
9
+ - https://github.com/vlebedeff/rb-ckmeans/pull/2
10
+
11
+ ## [0.1.0] - 2025-03-31
12
+
13
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Ckmeans
2
+
3
+ Repeatable unidimensional data clustering inspired by Ckmeans.1d.dp
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```bash
10
+ bundle add ckmeans
11
+ ```
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ ```bash
16
+ gem install ckmeans
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```rb
22
+ Ckmeans::Clusterer(data, kmin).clusters # fixed cluster count
23
+ Ckmeans::Clusterer(data, kmin, kmax).clusters # estimate optimal cluster count within kmin and kmax
24
+ Ckmeans::Clusterer(data, kmin, kmax, :sensitive).clusters # Adjust Bayesian Information Criteria favoring more smaller clusters
25
+ ```
26
+
27
+ ## License
28
+
29
+ The gem is available as open source under the terms of the [LGPL v3 License](https://opensource.org/license/lgpl-3-0).
30
+
31
+ ## References
32
+
33
+ - https://github.com/cran/Ckmeans.1d.dp
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,316 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ckmeans
4
+ class Clusterer # rubocop:disable Style/Documentation, Metrics/ClassLength
5
+ attr_reader :xcount, :xsorted, :kmin, :kmax, :smat, :jmat, :kestimate
6
+
7
+ PI_DOUBLE = Math::PI * 2
8
+
9
+ def initialize(entries, kmin, kmax = kmin, kestimate = :regular)
10
+ @xcount = entries.size
11
+
12
+ raise ArgumentError, "Minimum cluster count is bigger than element count" if kmin > @xcount
13
+ raise ArgumentError, "Maximum cluster count is bigger than element count" if kmax > @xcount
14
+
15
+ @kmin = kmin
16
+ @unique_xcount = entries.uniq.size
17
+ @kmax = [@unique_xcount, kmax].min
18
+ @xsorted = entries.sort
19
+ @kestimate = kestimate
20
+ end
21
+
22
+ def clusters
23
+ @clusters ||=
24
+ if @unique_xcount <= 1
25
+ [xsorted]
26
+ else
27
+ @smat = Array.new(kmax) { Array.new(xcount) { 0.0 } }
28
+ @jmat = Array.new(kmax) { Array.new(xcount) { 0 } }
29
+
30
+ kappa = kmax
31
+ n = xcount
32
+ xsum = Array.new(n)
33
+ xsumsq = Array.new(n)
34
+ shift = xsorted[n / 2]
35
+ xsum[0] = xsorted[0] - shift
36
+ xsumsq[0] = xsum[0]**2
37
+ 1.upto(n - 1) do |i|
38
+ xsum[i] = xsum[i - 1] + xsorted[i] - shift
39
+ xsumsq[i] = xsumsq[i - 1] + ((xsorted[i] - shift) * (xsorted[i] - shift))
40
+ smat[0][i] = dissim(0, i, xsum, xsumsq)
41
+ jmat[0][i] = 0
42
+ end
43
+
44
+ kappa_dec = kappa - 1
45
+ 1.upto(kappa_dec) do |q|
46
+ imin = q < kappa_dec ? [1, q].max : n - 1
47
+ fill_row(q, imin, n - 1, xsum, xsumsq)
48
+ end
49
+
50
+ kopt = koptimal
51
+
52
+ results = []
53
+ backtrack(kopt) do |q, left, right|
54
+ results[q] = xsorted[left..right]
55
+ end
56
+ results
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def koptimal
63
+ kopt = kmin
64
+ n = xcount
65
+ max_bic = 0.0
66
+
67
+ # Deviation from BIC formula to favor smaller clusters
68
+ adjustment = kestimate == :sensitive ? 0.0 : 1.0
69
+
70
+ kmin.upto(kmax) do |k|
71
+ sizes = Array.new(k)
72
+ backtrack(k) { |q, left, right| sizes[q] = right - left + 1 }
73
+ index_left = 0
74
+ index_right = nil
75
+ loglikelihood = 0.0
76
+ bin_left = nil
77
+ bin_right = nil
78
+
79
+ k.times do |kb|
80
+ num_points_in_bin = sizes[kb]
81
+ index_right = index_left + num_points_in_bin - 1
82
+
83
+ if xsorted[index_left] < xsorted[index_right]
84
+ bin_left = xsorted[index_left]
85
+ bin_right = xsorted[index_right]
86
+ elsif xsorted[index_left] == xsorted[index_right]
87
+ bin_left = index_left == 0 ? xsorted[0] : (xsorted[index_left - 1] + xsorted[index_left]) / 2.0
88
+ bin_right = index_right < n - 1 ? (xsorted[index_right] + xsorted[index_right + 1]) / 2.0 : xsorted[n - 1]
89
+ else
90
+ raise "ERROR: binLeft > binRight"
91
+ end
92
+
93
+ bin_width = bin_right - bin_left
94
+
95
+ mean, variance = shifted_data_variance(index_left, index_right)
96
+
97
+ if variance > 0
98
+ (index_left..index_right).each do |i|
99
+ loglikelihood += -(xsorted[i] - mean) * (xsorted[i] - mean) / (2.0 * variance)
100
+ end
101
+ loglikelihood +=
102
+ num_points_in_bin *
103
+ ((Math.log(num_points_in_bin / n.to_f) * adjustment) - (0.5 * Math.log(PI_DOUBLE * variance)))
104
+ else
105
+ loglikelihood += num_points_in_bin * Math.log(1.0 / bin_width / n)
106
+ end
107
+
108
+ index_left = index_right + 1
109
+ end
110
+
111
+ bic = (2.0 * loglikelihood) - (((3 * k) - 1) * Math.log(n.to_f))
112
+
113
+ if k == kmin
114
+ max_bic = bic
115
+ kopt = kmin
116
+ elsif bic > max_bic
117
+ max_bic = bic
118
+ kopt = k
119
+ end
120
+ end
121
+
122
+ kopt
123
+ end
124
+
125
+ def shifted_data_variance(ileft, iright)
126
+ sum = 0.0
127
+ sumsq = 0.0
128
+ mean = 0.0
129
+ variance = 0.0
130
+ n = iright - ileft + 1
131
+
132
+ if iright >= ileft
133
+ median = xsorted[(ileft + iright) / 2]
134
+
135
+ ileft.upto(iright) do |i|
136
+ sumi = xsorted[i] - median
137
+ sum += sumi
138
+ sumsq += sumi**2
139
+ end
140
+
141
+ mean = (sum / n) + median
142
+ variance = (sumsq - (sum * sum / n)) / (n - 1) if n > 1
143
+ end
144
+
145
+ [mean, variance]
146
+ end
147
+
148
+ def backtrack(k)
149
+ n = jmat[0].size
150
+ right = n - 1
151
+ left = nil
152
+
153
+ (k - 1).downto(0) do |q|
154
+ left = jmat[q][right]
155
+
156
+ yield q, left, right
157
+
158
+ right = left - 1 if q > 0
159
+ end
160
+ end
161
+
162
+ def dissim(j, i, xsum, xsumsq)
163
+ return 0.0 if j >= i
164
+
165
+ sji =
166
+ if j > 0
167
+ segment_sum = xsum[i] - xsum[j - 1]
168
+ segment_size = i - j + 1
169
+ xsumsq[i] - xsumsq[j - 1] - (segment_sum * segment_sum / segment_size)
170
+ else
171
+ xsumsq[i] - (xsum[i] * xsum[i] / (i + 1))
172
+ end
173
+
174
+ [0, sji].max
175
+ end
176
+
177
+ def fill_row(q, imin, imax, xsum, xsumsq)
178
+ size = imax - q + 1
179
+
180
+ js = Array.new(size) { |i| q + i }
181
+ smawk(imin, imax, 1, q, js, xsum, xsumsq)
182
+ end
183
+
184
+ def smawk(imin, imax, istep, q, js, xsum, xsumsq)
185
+ if (imax - imin) <= (0 * istep)
186
+ find_min_from_candidates(q, imin, imax, istep, js, xsum, xsumsq)
187
+ else
188
+ js_odd = js_reduced(imin, imax, istep, q, js, xsum, xsumsq)
189
+ istepx2 = istep * 2
190
+ imin_odd = imin + istep
191
+ imax_odd = imin_odd + ((imax - imin_odd) / istepx2 * istepx2)
192
+ smawk(imin_odd, imax_odd, istepx2, q, js_odd, xsum, xsumsq)
193
+ fill_even_positions(imin, imax, istep, q, js, smat, jmat, xsum, xsumsq)
194
+ end
195
+ end
196
+
197
+ def find_min_from_candidates(q, imin, imax, istep, js, xsum, xsumsq)
198
+ rmin_prev = 0
199
+
200
+ (imin..imax).step(istep) do |i|
201
+ rmin = rmin_prev
202
+ smat[q][i] = smat[q - 1][js[rmin] - 1] + dissim(js[rmin], i, xsum, xsumsq)
203
+ jmat[q][i] = js[rmin]
204
+
205
+ ((rmin + 1)...js.size).each do |r|
206
+ jabs = js[r]
207
+
208
+ next if jabs < jmat[q - 1][i]
209
+ break if jabs > i
210
+
211
+ sj = smat[q - 1][jabs - 1] + dissim(jabs, i, xsum, xsumsq)
212
+
213
+ next unless sj <= smat[q][i]
214
+
215
+ smat[q][i] = sj
216
+ jmat[q][i] = js[r]
217
+ rmin_prev = r
218
+ end
219
+ end
220
+ end
221
+
222
+ def js_reduced(imin, imax, istep, q, js, xsum, xsumsq)
223
+ n = ((imax - imin) / istep) + 1
224
+ m = js.size
225
+
226
+ return js if n >= m
227
+
228
+ js_red = js.dup
229
+ left = -1
230
+ right = 0
231
+
232
+ while m > n
233
+ p = left + 1
234
+ i = imin + (p * istep)
235
+ j = js_red[right]
236
+ sl = smat[q - 1][j - 1] + dissim(j, i, xsum, xsumsq)
237
+ jplus1 = js_red[right + 1]
238
+ splus1 = smat[q - 1][jplus1 - 1] + dissim(jplus1, i, xsum, xsumsq)
239
+
240
+ if (sl < splus1) && (p < n - 1)
241
+ left += 1
242
+ js_red[left] = j
243
+ right += 1
244
+ elsif (sl < splus1) && (p == n - 1)
245
+ right += 1
246
+ js_red[right] = j
247
+ m -= 1
248
+ else
249
+ if p > 0
250
+ js_red[right] = js_red[left]
251
+ left -= 1
252
+ else
253
+ right += 1
254
+ end
255
+
256
+ m -= 1
257
+ end
258
+ end
259
+
260
+ ((left + 1)...m).each do |r|
261
+ js_red[r] = js_red[right]
262
+ right += 1
263
+ end
264
+
265
+ js_red.slice!(m..-1) if js_red.size > m
266
+ js_red
267
+ end
268
+
269
+ def fill_even_positions(imin, imax, istep, q, js, smat, jmat, xsum, xsumsq)
270
+ n = js.size
271
+ istepx2 = istep * 2
272
+ jl = js[0]
273
+
274
+ i = imin
275
+ r = 0
276
+ while i <= imax
277
+ r += 1 while js[r] < jl
278
+
279
+ smat[q][i] = smat[q - 1][js[r] - 1] + dissim(js[r], i, xsum, xsumsq)
280
+ jmat[q][i] = js[r]
281
+ jh = ((i + istep) <= imax ? jmat[q][i + istep] : js[n - 1]).to_i
282
+ jmax = [jh, i].min.to_i
283
+ sjimin = dissim(jmax, i, xsum, xsumsq)
284
+
285
+ r += 1
286
+ while r < n && js[r] <= jmax
287
+ jabs = js[r]
288
+
289
+ break if jabs > i
290
+
291
+ if jabs < jmat[q - 1][i]
292
+ r += 1
293
+ next
294
+ end
295
+
296
+ s = dissim(jabs, i, xsum, xsumsq)
297
+ sj = smat[q - 1][jabs - 1] + s
298
+
299
+ if sj <= smat[q][i]
300
+ smat[q][i] = sj
301
+ jmat[q][i] = js[r]
302
+ elsif smat[q - 1][jabs - 1] + sjimin > smat[q][i]
303
+ break
304
+ end
305
+
306
+ r += 1
307
+ end
308
+
309
+ r -= 1
310
+ jl = jh
311
+
312
+ i += istepx2
313
+ end
314
+ end
315
+ end
316
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ckmeans
4
+ VERSION = "0.1.2"
5
+ end
data/lib/ckmeans.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ckmeans/version"
4
+ require_relative "ckmeans/clusterer"
5
+
6
+ module Ckmeans
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
data/sig/ckmeans.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ckmeans
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ckmeans
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Vlad Lebedev
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-03-31 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Repeatable clustering of unidimensional data
13
+ email:
14
+ - vladlebedeff@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".rspec"
20
+ - ".rubocop.yml"
21
+ - ".rubocop_todo.yml"
22
+ - ".ruby-version"
23
+ - CHANGELOG.md
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/ckmeans.rb
28
+ - lib/ckmeans/clusterer.rb
29
+ - lib/ckmeans/version.rb
30
+ - sig/ckmeans.rbs
31
+ homepage: https://github.com/vlebedeff/rb-ckmeans
32
+ licenses:
33
+ - LGPL-3.0-only
34
+ metadata:
35
+ rubygems_mfa_required: 'true'
36
+ homepage_uri: https://github.com/vlebedeff/rb-ckmeans
37
+ source_code_uri: https://github.com/vlebedeff/rb-ckmeans
38
+ changelog_uri: https://github.com/vlebedeff/rb-ckmeans/blob/main/CHANGELOG.md
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.1.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.6.5
54
+ specification_version: 4
55
+ summary: Ruby implementation of Ckmeans.1d.dp
56
+ test_files: []