Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Size Constraint #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions lib/kmeans-clusterer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def self.run k, data, opts = {}
opts[:k] = k
typecode = TYPECODE[opts[:float_precision]]

data_weights = data.map { |d| d.pop } if k.is_a?(Array)
data = Utils.ensure_matrix data, typecode

if opts[:scale_data]
Expand All @@ -136,6 +137,7 @@ def self.run k, data, opts = {}
end

opts[:data] = data
opts[:data_weights] = data_weights
opts[:row_norms] = Scaler.row_norms(data)

bestrun = nil
Expand All @@ -160,12 +162,16 @@ def self.run k, data, opts = {}


def initialize opts = {}
@k = opts[:k]
@size_constrained = opts[:k].is_a?(Array)
@k = @size_constrained ? opts[:k].size : opts[:k]
@k_constraints = opts[:k] if @size_constrained
@init = opts[:init]
@labels = opts[:labels] || []
@row_norms = opts[:row_norms]

@data = opts[:data]
@data_weights = opts[:data_weights]
@data_weights = NArray.cast(@data_weights) unless @data_weights.is_a?(NArray)
@points_count = @data ? @data.shape[1] : 0
@mean = Utils.ensure_narray(opts[:mean]) if opts[:mean]
@std = Utils.ensure_narray(opts[:std]) if opts[:std]
Expand All @@ -187,12 +193,24 @@ def run

min_distances.fill! Float::INFINITY
@distances = Distance.euclidean(@centroids, @data, @row_norms)

@k.times do |cluster_id|
dist = NArray.ref @distances[true, cluster_id].flatten
mask = dist < min_distances
@cluster_assigns[mask] = cluster_id
min_distances[mask] = dist[mask]

assigned_points = NArray.int(@points_count)
while assigned_points.sum != @points_count
@k.times do |cluster_id|
dist = NArray.ref @distances[true, cluster_id].flatten
mask = dist < min_distances
cluster_weight = 0
@data_weights[mask].each.with_index do |w, i|
if w + cluster_weight <= @k_constraints[cluster_id]
cluster_weight += w
else
mask[i] = 0
end
end if @size_constrained
@cluster_assigns[mask] = cluster_id
assigned_points[mask] = 1
min_distances[mask] = dist[mask]
end
end

max_move = 0
Expand Down