Skip to content

Instantly share code, notes, and snippets.

@jniechcial
Created February 20, 2017 12:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jniechcial/7640c45f9c43615d79a29f2fa77bb5ff to your computer and use it in GitHub Desktop.
require 'csv'
class ClusterUsers
def initialize(filename)
@filename = filename
end
def call
cluster_users
end
private
attr_reader :data, :labels
def cluster_users
@data = []
@labels = []
CSV.foreach(@filename, headers: true) do |row|
labels << row[1]
data << row[2..(row.length - 1)].map { |rate| rate.to_f }
end
k = labels.size / 8
kmeans = KMeansClusterer.run k, data, labels: labels, runs: 5
kmeans.clusters
end
def render_clusters(clusters)
clusters.each_with_index do |cluster, index|
puts "Team #{index}: "
cluster.sorted_points.map(&:label).each do |name|
puts "\t#{name}"
end
puts "\n"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment