3 ms·
Good post, but it's a sad reflection of code's failure as a medium of expression that we need 50 lines of Python to express one line of math.
by ced 15y ago
Good post, but it's a sad reflection of code's failure as a medium of expression that we need 50 lines of Python to express one line of math.
- sqrt17 15y agoIf 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.
- tel 15y agoSeriously. 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.
- twelvechairs 15y agoThis 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...
- deleted 15y ago[deleted]
- freyrs3 15y agoThe actual translation of the formula to Python is about 4 lines. Starting on L30 - L34 of [1]. The rest is just IO plumbing. [1] https://gist.github.com/731413/7ad1b4c04bc2d6b5033c5811efcb4e92d017d3cf#L30 https://gist.github.com/731413/7ad1b4c04bc2d6b5033c5811efcb4... .
- deleted 15y ago[deleted]
- g___ 15y agoIn 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.
- abecedarius 15y agohttp://code.google.com/p/aima-python/source/browse/trunk/learning.py http://code.google.com/p/aima-python/source/browse/trunk/lea... is shorter (NaiveBayesLearner around line 200) though that assumes some infrastructure.
- JulianMorrison 15y agoThat's not one line of math. That's one line of math and ten years of textbooks. Math just has a bigger standard library.
- Groxx 15y agoAnd 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
- agumonkey 15y agotell that to sir alan kay ps: I should add some data >>> http://tinlizzie.org/~awarth/ http://tinlizzie.org/~awarth/