/cluster_users.rb Secret
Created
February 20, 2017 12:07
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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