If you look at the code, it's more like "write an ARFF file parser in 40 lines" and "do a NB classifier with add-one smoothing in 10 lines".
If you use a better-adapted input format and code things more concisely, you'd probably end up with two functions of 3-4 lines each; conversely, if you wanted to do things properly, you'd separate out ARFF file parsing and the Naive Bayes functionality.
All in all, the blog post wouldn't make me want to recommend their group for prospective (undergrad or graduate) students.
Seriously. I think you could golf NB into 2 lines pretty sensibly once you've got the data in. It's really just compute two histograms, multiply, and maximize.
This is an interesting point - I suppose it shows how sophisticated the language of mathematics is.
I think its not perhaps the 50 lines that matter though (most of this is just effectively defining what the mathematical symbols and grammar mean), but the one line, which can tell you how everything relates together very effectively..... this is what the python version misses to me...
In fact, first three lines could be written in one, although a longer one
logProb = 0
for featureValue in featureVector:
logProb += math.log(self.featureCounts[(label, self.featureNameList[featureVector.index(featureValue)], featureValue)]/self.labelCounts[label])
is better written with a comprehension:
logProb = sum(math.log(self.featureCounts[(label, self.featureNameList[featureVector.index(featureValue)], featureValue)]/self.labelCounts[label]) for featureValue in featureVector)
and split into 2 lines.
In general the code is far from being Pythonic: fv[len(fv)-1] instead of fv[-1], with should be used instead of open/close, variable names are too long etc.
And no fear of namespace collisions, or of introducing additional symbols.
Want to match Math in size when implementing algorithms? Use APL. Want to avoid adding additional symbols all the time? Use J/K/etc (APL descendants). Want to avoid namespace collisions? Welcome to Java.Sun.Com.Math.Oh.For.Effing.Sake.FactoryInterfaceBuilder
13 comments
[ 2.7 ms ] story [ 32.0 ms ] threadIf you use a better-adapted input format and code things more concisely, you'd probably end up with two functions of 3-4 lines each; conversely, if you wanted to do things properly, you'd separate out ARFF file parsing and the Naive Bayes functionality.
All in all, the blog post wouldn't make me want to recommend their group for prospective (undergrad or graduate) students.
I think its not perhaps the 50 lines that matter though (most of this is just effectively defining what the mathematical symbols and grammar mean), but the one line, which can tell you how everything relates together very effectively..... this is what the python version misses to me...
[1] https://gist.github.com/731413/7ad1b4c04bc2d6b5033c5811efcb4... .
In general the code is far from being Pythonic: fv[len(fv)-1] instead of fv[-1], with should be used instead of open/close, variable names are too long etc.
Want to match Math in size when implementing algorithms? Use APL. Want to avoid adding additional symbols all the time? Use J/K/etc (APL descendants). Want to avoid namespace collisions? Welcome to Java.Sun.Com.Math.Oh.For.Effing.Sake.FactoryInterfaceBuilder
ps: I should add some data >>> http://tinlizzie.org/~awarth/
- Raycaster in 1000 lines of Lisp
- Database management system in 20000 lines of C
- Python web framework in 2000 lines of Python
...