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
def cluster_plot_2D(clusters, seq = 1, title = "Random noise", get_x = 'x', get_y = 'y') | |
if defined? Gnuplot | |
Gnuplot.open do |gp| | |
Gnuplot::Plot.new(gp) do |plot| | |
plot.terminal "png" | |
plot.output File.expand_path("../outfiles/clusters-#{seq}.png", __FILE__) | |
plot.title title | |
# Plot each cluster's points | |
clusters.each do |cluster| |
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
def k_means (points, k = 5, delta = 0.001, plot_on = false) | |
k = points.length if points.length < k | |
clusters = [] | |
k.times { clusters << Cluster.new(points.sample) } | |
iterations = 0 | |
while (clusters.any?(&:moved?)) |
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
class Cluster | |
attr_reader :center | |
def initialize(center) | |
@center = center | |
@points = [] | |
@moved = true | |
end | |
def add_point(point) | |
@points << point | |
end |
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
class Point2D < Point | |
def initialize(x, y = nil) | |
y.nil? ? super(x) : super({x: x, y: y}) | |
end | |
end |
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
class Point | |
def initialize(coords) | |
@coords = coords | |
end | |
def distance_to(point) | |
Math.sqrt( @coords.keys.inject(0) { |sum, key| sum + (@coords[key] - point.send(key))**2 } ) | |
end | |
def to_s | |
"#{@coords.values.join(", ")}" | |
end |
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 'benchmark' | |
require 'benchmark/ips' | |
array = [] | |
1000.times { array << rand(1000) } | |
N = 10000 | |
module Enumerable | |
def count_by_function function | |
map = Hash.new(0) |
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' | |
require 'pp' | |
require './names.rb' | |
require './countries.rb' | |
number_of_runs = 10000 | |
number_of_groups = 4 | |
participants_file = "participants.txt" | |
col_sep = "\t" | |
encoding = "UTF-8" |
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
begin | |
require 'gnuplot' | |
rescue LoadError | |
puts "No usable Gnuplot..." | |
end | |
class Point | |
def initialize(coords) | |
@coords = coords | |
end |
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
a = ["test", "tull", "tøys"] | |
=> ["test", "tull", "tøys"] |
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
$ rvm get latest | |
$ rvm pkg install readline --verify-downloads 1 | |
$ rvm reinstall all --force --verify-downloads 1 |