python - Divide column 2 with a particular value that is in the heading -


hello everyone, first of newbie coding , learning now. so, please excuse me doubt!

my data follows:

topic:  1 87187.0  mr 2288.0 's 1633.0 @card@ 1132.0 party 731.0 710.0  topic:  2 97854.0  2170.0 @card@ 1872.0 people 1078.0 police 562.0 

and on.... till topic 100 same format.

here first line topic number , it's weight. following words in topics , weights in topic.

i want find probability of each of word. divide each of word's weight it's respective topic weight. example,

in topic 1, word mr weight 2288.0 , it's topic weight 87187.0. so, probability of word mr in topic 0 2288.0/87187.0. likewise know probability of words.   output should like:  topic:  1 87187.0  mr 0.02624  's 0.01872 @card@ 0.0129 

and on... these values results of word's weight/the topic weight.

if normal column division then, have used col2/col1 technique quite challenging. so, please guide me. in advance!

you don't @ want output format like, or give example of such, should @ least point in right direction...

suggested python starting point, edit seems indicate desired output, aside floating point rounding concerns:

divisor = 1.0 open("input.txt") fd:     line in fd:         fields = line.strip().split()         if len(fields) > 0:             if fields[0] == 'topic:':                 divisor = float(fields[-1])             if len(fields) == 2:                 fields[-1] = str(float(fields[-1]) / divisor)         print ' '.join(fields) 

with above sample input, code produces:

topic: 1 87187.0  mr 0.0262424444011 's 0.0187298565153 @card@ 0.0129835870026 party 0.00838427747256 0.00814341587622  topic: 2 97854.0  0.0221758947003 @card@ 0.0191305414188 people 0.0110164122059 police 0.00574325014818 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -