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
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 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
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
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
# parameters | |
k = 7 | |
number_of_points = 10000 | |
delta = 0.001 | |
plot_on = false | |
require './k-means.rb' | |
points = [] | |
number_of_points.times do |
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/d3.geo.projection.v0.min.js"></script> | |
<script src="http://d3js.org/topojson.v0.min.js"></script> | |
<style> | |
.background { | |
fill: #a4bac7; | |
} |
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/d3.geo.projection.v0.min.js"></script> | |
<style> | |
.background { | |
fill: none; | |
pointer-events: all; | |
} |
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
mkdir $rvm_path/usr/ssl | |
cd $rvm_path/usr/ssl | |
curl -O http://curl.haxx.se/ca/cacert.pem # download certificates | |
mv cacert.pem cert.pem # move them in place | |
touch ~/.bashrc # create .bashrc if it does not exist | |
echo 'export SSL_CERT_FILE=~/.rvm/usr/ssl/cert.pem' >> ~/.bashrc # add key to environment | |
source ~/.bashrc # or close your consoles |
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
function pad2(number){ | |
if (number<10) { | |
return "0" + parseInt(number); | |
} else { | |
return parseInt(number); | |
} | |
} | |
var sectionList = $(".course-item-list-section-list"), | |
headerList = $(".course-item-list-header"), |