10/14/07

First 'About Me' post, and thoughts on AP computer science.

Permalink 02:32:43 pm, Categories: Background, About Me  

I would like to take this opportunity to introduce myself to some of my new readers who have added my blog to their RSS readers. I feel it is important that readers can get some background on me when they come here from digg, reddit and others. Don’t worry, I don’t plan to digg this post, in fact, I am actually tentative to digg any of my articles because I wonder if they are ‘advanced’ or innovative enough to justify grabbing someones attention for a few seconds. Of course I end up submitting them because I have to start somewhere, but I think the first few have come out pretty well.
I am new at this, and also pretty new to the real ‘official’ study of computer science although I have been using linux and experimenting with open source software since I was 12 (late 90s). Throughout that time I had a love/hate relationship with programming. I had always thought programming was really cool, but I was never really mature enough mentally to grasp the real concepts that I needed to grasp. Now, don’t get me wrong, I was light years ahead with my interest in math when I was very young, and I know there are many kids, younger and younger who are becoming excellent programmers, but the chain of events did not lead me to an early mastery of programming. The reality was that I was driven away from programming, I was motivated to do many advanced and wonderful things with computers and linux, but I always viewed programming as the brick wall that I wouldn’t be able to get past. This line of thought became surprisingly hard to break out of, and I will explain how I ended up that way.
In high school, I entered into an AP computer science class, knowing that it would be pretty hard, and the last exposure I had to programming was QuickBASIC in 8th grade, which was a joke to me because I learned the basics of BASIC years earlier. Well, the AP class was based on C++ and it was quite a bit different. A lot of the classes were dumbed down and I remember them being pretty boring and dry (except that the teacher sold candy all day long, and I wonder why I need tooth implants already). Then, some time in the second week or so, I discovered that the entire year was being dedicated to somehow moving a fish around the screen. No, we were not doing gui programming! Fish here = <>< :crazy: Anyway, I ended up slacking off hardcore and also not taking the AP exam at all. In retrospect, the AP exam didn’t even matter to me, I kind of knew I didn’t want to to to college the September after I graduated, but the C++ that I could have learned in that room really did matter, and i wish I got more out of it than just remembering certain concepts that i had forced myself to stay awake through.
Writing this, I can see how different my outlook is now about not only programming, but learning in general. I now view learning as a lifelong task, and I have learned to relax and enjoy the ride, because, for me, there is no ‘light at the end of the tunnel’ so to speak. Ironically, the year after I took that class, I was a senior and some of the members of my class took the course that year, but it had been changed to Java! After taking a semester of Java in college I am almost convinced that I would have stuck it out and took the AP exam if I were learning Java, as a matter of fact, if the future gives any indication, it would have gotten me very excited to learn C++ after I finished and had a solid understanding of Java. This all ties into my views on being a newbie and how important and empowering that ‘newbie’ stage is. When you are a newbie, the way you expose yourself to a subject that you are interested in will pave the way for a solid understanding of the important concepts. I don’t want to get too off topic here, so I will save the newbie philosophy for another post
One last thing I would like to mention about the site for this first ‘about me’ post is that I would like to thank the few people who bought Toby’s book through the links in my posts. I will be recommending a lot of books here, and I honestly didn’t expect to sell a single copy, so even if you guys are just doing it for charity it is much appreciated. It isn’t easy getting started, as many of you know because you are bloggers yourself. There is a lot to do, and that is why i haven’t gotten around to finishing my Ruby on Rails blogging engine and I will also have to change hosts soon because I found a much better deal that actually supports RoR (Hostgator lied, they want you to run your rails app as a CGI application!!! ). Despite the details, I have been feeling for quite a while that i need a place to write because I have too much that goes unwritten, and too many programming experiments that end up gone when their respective hard drive bites the dust, this blog is the result of my internal struggle as a perfectionist between wanting to get my ideas online, and wanting them to be presented perfectly with a blog engine and design custom written by myself. Soon enough, I will have the first version up and running.
To wrap this up, I just want to again thank all my readers, and encourage you to link here from any of your sites, or even digg any of the articles you want (even this one, hey I just said I wouldn’t digg it but I wouldn’t mind some extra exposure). I will trade links with anyone who would like to, just email me at gmail (username:louis.calderone) because I don’t want to give out the loucal.net one until I have a more permanent host. Until next time, have a great weekend everyone.

10/13/07

Part 2 - [Trying it out in irb] Programming Collective Intelligence w/ Ruby

Permalink 11:35:10 pm, Categories: Ruby, Collective Intelligence  

Originally posted 9/11/07

Ok, thanks for checking out part 2 of this series where I will show those of you without much background in ruby how to run the code interactively from Part 1.
I will use the monospace to show the irb prompt, once it is installed, just type irb in the directory where you saved the code from part one [intelligence.rb]

$ irb
>require ‘intelligence’
This will load the code as is into your interactive session. If you want to reload the code as Toby does in the text, you can use:

>load ‘intelligence.rb’
Now, you can the following array of critics, I am using the example from the text, and I will show you how to convert it for use in ruby without having to change every hash symbol from : to =>

> critics_str = %{ {’Lisa Rose’: {’Lady in the Water’: 2.5, ‘Snakes on a Plane’: 3.5, ‘Just My Luck’: 3.0, ‘Superman Returns’: 3.5, ‘You, Me and Dupree’: 2.5, ‘The Night Listener’: 3.0}, ‘Gene Seymour’: {’Lady in the Water’: 3.0, ‘Snakes on a Plane’: 3.5, ‘Just My Luck’: 1.5, ‘Superman Returns’: 5.0, ‘The Night Listener’: 3.0, ‘You, Me and Dupree’: 3.5}, ‘Michael Phillips’: {’Lady in the Water’: 2.5, ‘Snakes on a Plane’: 3.0, ‘Superman Returns’: 3.5, ‘The Night Listener’: 4.0}, ‘Claudia Puig’: {’Snakes on a Plane’: 3.5, ‘Just My Luck’: 3.0, ‘The Night Listener’: 4.5, ‘Superman Returns’: 4.0, ‘You, Me and Dupree’: 2.5}, ‘Mick LaSalle’: {’Lady in the Water’: 3.0, ‘Snakes on a Plane’: 4.0, ‘Just My Luck’: 2.0, ‘Superman Returns’: 3.0, ‘The Night Listener’: 3.0, ‘You, Me and Dupree’: 2.0}, ‘Jack Matthews’: {’Lady in the Water’: 3.0, ‘Snakes on a Plane’: 4.0, ‘The Night Listener’: 3.0, ‘Superman Returns’: 5.0, ‘You, Me and Dupree’: 3.5}, ‘Toby’: {’Snakes on a Plane’:4.5,’You, Me and Dupree’:1.0,’Superman Returns’:4.0}} }
> critics = eval(critics_str.gsub(’:', ‘=>’))

>sim_distance(critics,’Lisa Rose’,'Gene Seymour’)
=>0.148148148148

>sim_pearson(critics,’Lisa Rose’,'Gene Seymour’)
=>0.396059017191

>top_matches(critics,’Toby’,n=3)
=>[[0.99124070716192991, ‘Lisa Rose’], [0.92447345164190486, ‘Mick LaSalle’],[0.89340514744156474, ‘Claudia Puig’]]

Whitespace in the critics_str doesn’t matter, and the gsub stands for global substitution, I can also use a regular expression there too.

The eval() method is pretty cool, it takes a string and evaluates it as if you typed it into the prompt. This makes dynamic programming a bit easier
That is all I have time for without risking being late for class, so I hope you have enjoyed this, and I will be back with more for the loyal readers this afternoon
P.S. Please excuse any spelling mistakes until this afternoon, I didn’t want to be late, and this school only has IE on most desktops

Programming Collective Intelligence with Ruby

Permalink 11:16:11 pm, Categories: Ruby, Collective Intelligence  

*Originally posted 9/9/07

Recently, Toby Segaran wrote a book called Programming Collective Intelligence. I am only a few chapters in, but am finding it to be a great read.
The problem for me, was that the code is written in Python. Python is a pretty cool language, but trying to learn from it made me very grateful for the way Ruby code reads. Realizing a learning opportunity, I got started writing the first two recommendation metrics in Ruby (Euclidean Distance and Pearson Correlation Score).
Now, I don’t claim to be a guru, and I have at best a basic understanding of Python. Understand that these were written in my free time, and I am sure that in certain places there are better ways to do this (feel free to email lou@loucal.net with suggestions) but for now, I was just trying to get the first two to run correctly.
I will first post my source code, and then I will demonstrate the proper way to run the book’s examples in irb the interactive ruby interpreter.
ALSO: Special thanks to David A. Black, author of Ruby for Rails, for extreme patience in helping me to understand the block form of Hash.new – Check out the book here

Code:

Code:

def square(n)
  n*n
end
module Summable
  def sum
    inject{|v,n|v+n}
  end
end
class Array
  include Summable
end
def sim_distance(prefs,p1,p2)
  si = {}
  #get shared items
  prefs[p1].each do |key, value|
    si[key] = 1 if prefs[p2].has_key?(key)
  end
  return 0 if (si.length == 0) #if empty
  #add the squares of all differences
  sum = 0.0
  prefs[p1].each do |key, value|
    if prefs[p2].has_key?(key)
      sum += square(prefs[p1][key] - prefs[p2][key])
    end
  end
  1/(1+sum)
end
def sim_pearson(prefs,p1,p2)
  si = {}
  #get shared items
  prefs[p1].each do |key, value|
    si[key] = 1 if prefs[p2].has_key?(key)
  end
  n = si.length
  return 0 if (n == 0) #if empty
  #add up all preferences
  sum1 = prefs[p1].values_at(*si.keys).sum
  sum2 = prefs[p2].values_at(*si.keys).sum
  #add up squares
  sum1Sq = prefs[p1].values_at(*si.keys).collect{|val|val*val}.sum
  sum2Sq = prefs[p2].values_at(*si.keys).collect{|val|val*val}.sum
  #sum up the products
  prod_array = [] #I know the below can be done better
  si.each do |key, val|
    prod_array << prefs[p1][key] * prefs[p2][key]
  end
  pSum = prod_array.sum
  #calculate Pearson score
  num = pSum-(sum1*sum2 / n)
  den = Math.sqrt((sum1Sq - square(sum1) / n) * (sum2Sq - square(sum2) / n))
  return 0 if den==0
  r = num / den
  return r
end
def top_matches(prefs, person, n=5, similarity='sim_pearson')
  scores = []
  prefs.each do |other, hash|
    scores << [eval("#{similarity}(prefs,person,other)"),other] if other != person
  end
  scores.sort!.reverse!
  return scores[0..n-1]
end
def get_recommendations(prefs, person, similarity='sim_pearson')
  totals = Hash.new(0)
  sim_sums = Hash.new(0.0)
  rankings = []
  prefs.each do |other, val|
    unless other == person
      sim = eval("#{similarity}(prefs,person,other)")
      unless sim <= 0
        prefs[other].each do |item, val2|
          if !prefs[person].has_key?(item) || prefs[person][item] == 0
            totals[item] += val2 * sim
            sim_sums[item] += sim
          end
        end
      end
    end
  end
  totals.each_pair do |item, total|
    rankings << [total/sim_sums[item], item]
  end
  rankings.sort!.reverse!
end
def transform_prefs(prefs)
  result = Hash.new{|hash,key| hash[key] = {} }
  prefs.each do |person, person_hash|
    person_hash.each do |item, val|
      result[item][person] = val
    end
  end
  result
end

July 2008
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Lou's Blog

This is the long description for the blog named 'Blog A'.

Search

Archives

Misc

XML Feeds

What is this?

powered by b2evolution free blog software