#NEWLINE# def ndl( data ) : #NEWLINE# #INDENT# vec = DictVectorizer ( dtype = float , sparse = False ) #NEWLINE# D = ( vec . fit_transform ( [ explode ( c ) for c in data . Cues ] ) * data . Frequency [ : , np . newaxis ] ) #NEWLINE# n = len ( vec . get_feature_names ( ) ) #NEWLINE# C = np . zeros ( ( n , n ) ) #NEWLINE# for row in D : #NEWLINE# #INDENT# for nz in np . nonzero ( row ) : #NEWLINE# #INDENT# C [ nz ] += row #NEWLINE# #UNINDENT# #UNINDENT# Z = C . sum ( axis = 1 ) #NEWLINE# C1 = ( C / Z [ : , np . newaxis ] ) #NEWLINE# out = DictVectorizer ( dtype = float , sparse = False ) #NEWLINE# X = ( out . fit_transform ( [ explode ( c ) for c in data . Outcomes ] ) * data . Frequency [ : , np . newaxis ] ) #NEWLINE# O = np . zeros ( ( len ( vec . get_feature_names ( ) ) , len ( out . get_feature_names ( ) ) ) ) #NEWLINE# for i in xrange ( len ( X ) ) : #NEWLINE# #INDENT# for nz in np . nonzero ( D [ i ] ) : #NEWLINE# #INDENT# O [ nz ] += X [ i ] #NEWLINE# #UNINDENT# #UNINDENT# O1 = ( O / Z [ : , np . newaxis ] ) #NEWLINE# W = np . linalg . pinv ( C1 ) . dot ( O1 ) #NEWLINE# return pd . DataFrame ( W , columns = out . get_feature_names ( ) , index = vec . get_feature_names ( ) )