Skip to content

Instantly share code, notes, and snippets.

@yukimya
Created May 29, 2014 14:57
Show Gist options
  • Save yukimya/8ec60bf0d072b60ef98a to your computer and use it in GitHub Desktop.
Save yukimya/8ec60bf0d072b60ef98a to your computer and use it in GitHub Desktop.
The calculation of mean value from probability
# python script: averagin.py
# written by Naoyuki Miyashita
import subprocess
import numpy as np
import numpy.linalg
import string,os,sys,shutil,random
def readfile(filename):
f=open(filename,"r")
box=f.readlines()
f.close()
return (box)
def divide2Line(f):
lines=f.replace("\n","")
lines=lines.strip()
#lines=f[:-1]
return (lines)
def splitData(line):
lines=line.split()
return (lines)
def argv_read():
f=sys.argv[1]
v=int(sys.argv[2])
p=int(sys.argv[3])
argx=(f,v,p)
return (argx)
def main():
argx=argv_read()
(filename,v,p)=argx
box=readfile(filename)
#lines=np.array([])
nline=len(box)
fl=0.0
mean=0.0
for i in range(nline):
line=box[i]
#print nline, line
line2=divide2Line(line)
#print line2
if len(line2) ==0:
continue
lines=splitData(line2)
#print lines
val=lines[v]
pb=lines[p]
mean=mean+float(val)*float(pb)
fl=fl+float(pb)
print val, pb, mean, fl
print "sum=", fl
mean=mean/fl
print "mean=",mean
return ()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment