我有一个python数组,其中包含一系列事件,以及某些仪表上洪水的相应值。它读起来像这样:
tmp = np.fromfile( Max_File, sep=' ')
nGauges = int(tmp[0]); nEvents = (tmp.size-1)/nGauges;
Max = np.reshape(tmp[1::],[ nEvents, nGauges ])
我现在要做的是,我想要1000个事件,以及该事件的最大洪水值,而不是1000个事件,以及相应的1000个洪水计记录。
所以结果可能如下所示:
1, 3.4
2, 4.5
3, 1.2
4, 3.2
现在事件只是从1到1000排序。我将在需要时使用单独的输入文件重命名。
我知道我不知何故需要使用MAX()函数,但是我一直在玩这个,并且看不到让它工作!
谢谢, 金佰利
答案 0 :(得分:4)
您希望在ndarray
轴(1)上找到1000x1000 ngauges
的最大值:
>>> a = np.arange(25).reshape(5,5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
>>> a.max(axis = 1)
array([ 4, 9, 14, 19, 24])
>>> a.max(axis = 1).reshape(5,1)
array([[ 4],
[ 9],
[14],
[19],
[24]])
>>> np.array([(n, m) for n, m in enumerate(a.max(axis=1))])
array([[ 0, 4],
[ 1, 9],
[ 2, 14],
[ 3, 19],
[ 4, 24]])
>>>
或使用numpy.insert():
>>> np.insert(a.max(axis=1).reshape(5,1),0, values = range(1, len(a)+1), axis = 1)
array([[ 1, 4],
[ 2, 9],
[ 3, 14],
[ 4, 19],
[ 5, 24]])
>>>
答案 1 :(得分:0)
我假设你的重塑Numpy阵列看起来像什么。这是一个例子,假设我的结构正确:
# In each row 1st element is the event, the rest are gage values
l = [[1, 1.1, 2.2, 3.3, 4.4],
[2, 5.4, 3.2, 4.2, 1.1],
[3, 1.0, 2.0, 3.0]]
for line in l:
print line[0], max(line[1::])
结果
1 4.4
2 5.4
3 3.0
同样,使用list comp
[(i[0], max(i[1::])) for i in l]
结果
[(1, 4.4), (2, 5.4), (3, 3.0)]