jeudi 10 avril 2014

Python 2.7 - moyenne de nième élément de chaque liste dans un dictionnaire - Stack Overflow


D_values = {}
Average = [ ]
for path in glob("F:\Thermal Motion\*.txt"):
t, x, y = np.loadtxt(path, unpack=True)
D_values[path] = [((x[i]/.115 - x[0]/.115)**2 + (y[i]/.155 - y[0]/.155)**2)**0.5
for i in range(len(x))]
print D_values[path]

When I print D_values it gives a bunch of lists. What I need to do is to find the average of each of the nth elements, for all the arrays. I just don't know how I'll refer to a specific list in the dictionary. Like if D_values[path] gives lists


[1,2,3,4,...]
[23,234,43,...]
[5,6,7,...]

I want to make an array where Average[(1,23,5,...) (2,234,7,...)...]




This has basically been done for you with np.mean. But you want to make your data into an array first, instead of a dict. You can do this by saying:


D_values = []  # an empty list, not dict
Average = []
for path in glob("F:\Thermal Motion\*.txt"):
t, x, y = np.loadtxt(path, unpack=True)
#append to list instead of dict:
D_values.append([((x[i]/.115 - x[0]/.115)**2 + (y[i]/.155 - y[0]/.155)**2)**0.5
for i in range(len(x))])
print D_values

Then you have D_values is a list of lists. Now you can use np.mean on it:


np.mean(D_values, axis=0)

Where the axis=0 argument tell the function to average along the first axis (columns).


So, if D_values is [[1,2,3], [23,234,43], [5,6,7]], then you'll get your mean as: [ 9.66666667, 80.66666667, 17.66666667]


Protip: You don't have to build your D_values with a list comprehension, since t, x, and y are arrays. You can do:


D_values = []  # an empty list, not dict
Average = []
for path in glob("F:\Thermal Motion\*.txt"):
t, x, y = np.loadtxt(path, unpack=True)
#append to list instead of dict:
D_values.append((((x - x[0])/.115)**2 + ((y - y[0])/.155)**2)**0.5)
print D_values


D_values = {}
Average = [ ]
for path in glob("F:\Thermal Motion\*.txt"):
t, x, y = np.loadtxt(path, unpack=True)
D_values[path] = [((x[i]/.115 - x[0]/.115)**2 + (y[i]/.155 - y[0]/.155)**2)**0.5
for i in range(len(x))]
print D_values[path]

When I print D_values it gives a bunch of lists. What I need to do is to find the average of each of the nth elements, for all the arrays. I just don't know how I'll refer to a specific list in the dictionary. Like if D_values[path] gives lists


[1,2,3,4,...]
[23,234,43,...]
[5,6,7,...]

I want to make an array where Average[(1,23,5,...) (2,234,7,...)...]



This has basically been done for you with np.mean. But you want to make your data into an array first, instead of a dict. You can do this by saying:


D_values = []  # an empty list, not dict
Average = []
for path in glob("F:\Thermal Motion\*.txt"):
t, x, y = np.loadtxt(path, unpack=True)
#append to list instead of dict:
D_values.append([((x[i]/.115 - x[0]/.115)**2 + (y[i]/.155 - y[0]/.155)**2)**0.5
for i in range(len(x))])
print D_values

Then you have D_values is a list of lists. Now you can use np.mean on it:


np.mean(D_values, axis=0)

Where the axis=0 argument tell the function to average along the first axis (columns).


So, if D_values is [[1,2,3], [23,234,43], [5,6,7]], then you'll get your mean as: [ 9.66666667, 80.66666667, 17.66666667]


Protip: You don't have to build your D_values with a list comprehension, since t, x, and y are arrays. You can do:


D_values = []  # an empty list, not dict
Average = []
for path in glob("F:\Thermal Motion\*.txt"):
t, x, y = np.loadtxt(path, unpack=True)
#append to list instead of dict:
D_values.append((((x - x[0])/.115)**2 + ((y - y[0])/.155)**2)**0.5)
print D_values

0 commentaires:

Enregistrer un commentaire