473,287 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Please explain collections.defaultdict(lambda: 1)

I'm reading http://norvig.com/spell-correct.html

and do not understand the expression listed in the subject which is
part of this function:

def train(features):
model = collections.defaultdict(lambda: 1)
for f in features:
model[f] += 1
return model
Per http://docs.python.org/lib/defaultdict-examples.html

It seems that there is a default factory which initializes each key to
1. So by the end of train(), each member of the dictionary model will
have value >= 1

But why wouldnt he set the value to zero and then increment it each
time a "feature" (actually a word) is encountered? It seems that each
model value would be 1 more than it should be.

Nov 6 '07 #1
2 5803
On Nov 6, 8:54 am, "metaperl.com" <metap...@gmail.comwrote:
I'm readinghttp://norvig.com/spell-correct.html

and do not understand the expression listed in the subject which is
part of this function:

def train(features):
model = collections.defaultdict(lambda: 1)
for f in features:
model[f] += 1
return model

Perhttp://docs.python.org/lib/defaultdict-examples.html

It seems that there is a default factory which initializes each key to
1. So by the end of train(), each member of the dictionary model will
have value >= 1

But why wouldnt he set the value to zero and then increment it each
time a "feature" (actually a word) is encountered? It seems that each
model value would be 1 more than it should be.
The explanation is a little further down on that same page, on the
discussion of "novel" words and avoiding the probablity of them being
0 just because they have not yet been seen in the training text.

-- Paul

Nov 6 '07 #2
"metaperl.com" <me******@gmail.comwrote:
Per http://docs.python.org/lib/defaultdict-examples.html

It seems that there is a default factory which initializes each key to
1. So by the end of train(), each member of the dictionary model will
have value >= 1

But why wouldnt he set the value to zero and then increment it each
time a "feature" (actually a word) is encountered? It seems that each
model value would be 1 more than it should be.
The author explains his reasoning in the article: he wants to treat novel
words (i.e. those which did not appear in the training corpus) as having
been seen once.

Nov 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
8
by: rubbishemail | last post by:
Hello, I need your help understanding lambda (and doing it a better way without). f = lambda x : x*x # this is a list of functions
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
5
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a...
3
by: Raymond Hettinger | last post by:
FWIW, here are three ways of writing constant functions for collections.defaultdict(): d = defaultdict(int) # slowest way; works only for zero d = defaultdict(lambda: 0) # faster way;...
2
by: tutufan | last post by:
It seems like x = defaultdict(defaultdict(list)) should do the obvious, but it doesn't. This seems to work y = defaultdict(lambda: defaultdict(list)) though is a bit uglier.
3
by: fizilla | last post by:
Hello all! I have the following weird problem and since I am new to Python I somehow cannot figure out an elegant solution. The problem reduces to the following question: How to pickle a...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.