递归地选择numpy数组中的元素

时间:2014-08-26 21:35:59

标签: python-2.7 numpy

我有一个文本文件,其中包含全球各点的纬度和温度值。我想取一个指定纬度区间(即每个度数,从南极到北极)之间的所有温度点的平均值。这是我到目前为止的代码:

data_in = genfromtxt('temperatures.txt', usecols = (0,1)) 
lat = data_in[:,0]
temp = data_in[:,1]

in_1N = np.where((lat>=0) & (lat<=1)) # outputs an array of indexes for all latitudes between 0°-1° North
temp_1N = temp[in_1N] # outputs an array of temperature values between 0°-1° North
avg_1N = np.nanmean(temp_1N) # works out the average of temperatures between 0°-1° North

plt.scatter(1, avg_1N) # plots the average temperature against the latitude interval
plt.show()

我如何改进此代码,因此它可以实现180次以覆盖-90°S和90°N之间的地球? 感谢

1 个答案:

答案 0 :(得分:1)

您可以使用np.histogram将纬度放入分档。通常,np.histogram仅计算每个箱中的纬度数。但是如果你用相关的temp值来加权纬度,那么你得到的是临时值的总和而不是计数。如果将临时值之和除以箱数,则得到每个箱中的平均温度:

import numpy as np
import matplotlib.pyplot as plt

# N = 100
# lat = np.linspace(-90, 90, N)
# temp = 50*(1-np.cos(np.linspace(0, 2*np.pi, N)))
# temp[::5] = np.nan
# np.savetxt(filename, np.column_stack([lat, temp]))

lat, temp = np.genfromtxt('temperatures.txt', usecols = (0,1), unpack=True) 
valid = np.isfinite(temp)
lat = lat[valid]
temp = temp[valid]

grid = np.linspace(-90, 90, 40)

count, bin_edges = np.histogram(lat, bins=grid)
temp_sum, bin_edges = np.histogram(lat, bins=grid, weights=temp)
temp_avg = temp_sum / count

plt.plot(bin_edges[1:], temp_avg, 'o')
plt.show()

enter image description here


请注意,如果您安装了scipy,则可以替换 两次调用np.histogram

count, bin_edges = np.histogram(lat, bins=grid)
temp_sum, bin_edges = np.histogram(lat, bins=grid, weights=temp)

拨打stats.binned_statistic

import scipy.stats as stats
temp_avg, bin_edges, binnumber = stats.binned_statistic(
    x=lat, values=temp, statistic='mean', bins=grid)