#NEWLINE# def danks( data ) : #NEWLINE# #INDENT# feats = DictVectorizer ( dtype = int , sparse = False ) #NEWLINE# marginals = data . groupby ( 'Cues' , as_index = False ) . Frequency . sum ( ) #NEWLINE# marginals = marginals . rename ( columns = { #NEWLINE# #INDENT# 'Frequency' : 'Total' , #NEWLINE# #UNINDENT# } ) #NEWLINE# data = pd . merge ( data , marginals , on = 'Cues' ) #NEWLINE# result = pd . DataFrame ( ) #NEWLINE# for outcome in data . Outcomes . unique ( ) : #NEWLINE# #INDENT# yes = data [ ( data . Outcomes == outcome ) ] #NEWLINE# M = feats . fit_transform ( [ explode ( c ) for c in yes . Cues ] ) #NEWLINE# P = np . diag ( ( yes . Total / sum ( yes . Total ) ) ) #NEWLINE# MTP = M . T . dot ( P ) #NEWLINE# O = ( yes . Frequency / yes . Total ) #NEWLINE# left = MTP . dot ( M ) #NEWLINE# right = MTP . dot ( O ) #NEWLINE# V = np . linalg . solve ( left , right ) #NEWLINE# result [ outcome ] = V #NEWLINE# #UNINDENT# result . index = feats . get_feature_names ( ) #NEWLINE# return result