Hi Guys,
Today's target is get the averages and variances of a given data in input file. It's common thing which is done with huge amount of data. Here is the program, which shows how to do that in Python. Here we go :
import numpy as np
#defining lists
x=[]
y=[]
#opening data file in reading mode
with open ("data.dat","r") as f:
for line in f: # reading data in lines
row=line.split() # splitting lines to separated columns
x.append(float(row[0])) # appending columns in arrays
y.append(float(row[1]))
#conversion of lists in numpy arrays
x=np.asarray(x)
y=np.asarray(y)
#calculating averages
x_avg=np.mean(x)
y_avg=np.mean(y)
#calculating variances
x_var=np.var(x)
y_var=np.var(y)
print("\nX_average : %f,Y_average : %f" %(x_avg,y_avg))
print("X_var : %f,Y_var : %f\n" %(x_var,y_var))
Input :
1 3
2 4
5 7
6 4
8 5
1 3
1 5
1 4
Output :
No comments:
Post a Comment