i have following code works fine , wondering how implement same logic using list comprehension.
def get_features(document, feature_space): features = {} w in feature_space: features[w] = (w in document) return features
also going improvements in performance using list comprehension?
the thing both feature_space
, document
relatively big , many iterations run.
edit: sorry not making clear @ first, both feature_space
, document
lists.
document
list of words (a word may exist more once!)feature_space
list of labels (features)
like this, dict comprehension:
def get_features(document, feature_space): return {w: (w in document) w in feature_space}
the features[key] = value
expression becomes key: value
part @ start, , rest of for
loop(s) , if
statements follow in nesting order.
yes, give performance boost, because you've removed features
local name lookups , dict.__setitem__
calls.
note need make sure document
data structure has fast membership tests. if list, convert set()
first, example, ensure membership tests take o(1) (constant) time, not o(n) linear time of list:
def get_features(document, feature_space): document = set(document) return {w: (w in document) w in feature_space}
with set
, o(k) loop instead of o(kn) loop (where n size of document
, k
size of feature_space)
.
Comments
Post a Comment