<s> import os , sys <EOL> from time import time <EOL> from multiprocessing import Pool <EOL> import pandas as pd <EOL> import numpy as np <EOL> from sklearn . feature_extraction import DictVectorizer <EOL> import ndl <EOL> os . system ( "<STR_LIT>" % os . getpid ( ) ) <EOL> def simulate ( i ) : <EOL> global data <EOL> W = ndl . rw ( data , M = <NUM_LIT> ) <EOL> return i , W <EOL> def main ( ) : <EOL> global data <EOL> data = pd . read_csv ( '<STR_LIT>' ) <EOL> W0 = ndl . ndl ( data ) <EOL> diff = np . zeros_like ( W0 ) <EOL> W = np . zeros_like ( W0 ) <EOL> R = <NUM_LIT:1000> <EOL> now = time ( ) <EOL> P = Pool ( <NUM_LIT:6> ) <EOL> for i , W1 in P . imap_unordered ( simulate , xrange ( R ) ) : <EOL> diff += abs ( W1 - W0 ) <EOL> W += W1 <EOL> print >> sys . stderr , i , time ( ) - now <EOL> diff = diff / R <EOL> W = W / R <EOL> cues = DictVectorizer ( dtype = int , sparse = False ) <EOL> D = cues . fit_transform ( [ ndl . explode ( c ) for c in data . Cues ] ) <EOL> out = DictVectorizer ( dtype = int , sparse = False ) <EOL> X = out . fit_transform ( [ ndl . explode ( c ) for c in data . Outcomes ] ) * data . Frequency [ : , np . newaxis ] <EOL> O = np . zeros_like ( W0 ) <EOL> for i in xrange ( len ( X ) ) : <EOL> for nz in np . nonzero ( D [ i ] ) : <EOL> O [ nz ] += X [ i ] <EOL> np . savez ( '<STR_LIT>' , diff = diff , W0 = W0 . as_matrix ( ) , O = O , W = W ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
