Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@benmcredmond
Last active July 28, 2016 08:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmcredmond/0dec520b6ab2ce7c59d5 to your computer and use it in GitHub Desktop.
Save benmcredmond/0dec520b6ab2ce7c59d5 to your computer and use it in GitHub Desktop.
@cluster_centers = [rand_point(), rand_point()]
15.times do
@clusters = [[], []]
@posts.each do |post|
min_distance, min_point = nil, nil
@cluster_centers.each.with_index do |center, i|
if distance(center, post) < min_distance
min_distance = distance(center, post)
min_point = i
end
end
@clusters[min_point] << post
end
@cluster_centers = @clusters.map do |post|
average(posts)
end
end
def similar_posts(post)
title_keywords = post.title.split(' ')
Post.all.to_a.sort |post1, post2|
post1_title_intersection = post1.body.split(' ') & title_keywords
post2_title_intersection = post2.body.split(' ') & title_keywords
post2_title_intersection.length <=> post1_title_intersection.length
end[0..9]
end
@posts = Post.all
@words = @posts.map do |p|
p.body.split(' ')
end.flatten.uniq
@vectors = @posts.map do |p|
@words.map do |w|
p.body.include?(w) ? 1 : 0
end
end
@Gonzih
Copy link

Gonzih commented Nov 21, 2013

I bet you will get error "ArgumentError: comparison of Fixnum with nil failed" on this line https://gist.github.com/benmcredmond/0dec520b6ab2ce7c59d5#file-kmeans-rb-L10

@i-arindam
Copy link

What is rand_point().

The definition of the method is missing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment