結果的輸出是 3 個二維陣列,長度遞減 1。我想撰寫一個代碼來獲取結束索引值last_incs、最大值maxs和最小值mins。它應該遍歷每個二維陣列的所有行,例如is的result輸出。對應于每一個這些子陣列的最大值如下:其被示出在下面。我怎么能做到這一點?[-3,-1,-2,1] array([array([ 0., 0., 0., 25.]),array([ 0,0, 33.33333333]), array([ 0., 50.]),array([100.])], dtype=object)[25.0, 33.33333333333333, 50.0, 100.0]Expected OutputsMax:
import numpy as np
def run(*args):
result = np.array([np.array([((arr[i:] > 0).cumsum()/ np.arange(1, len(arr[i:]) 1) * 100) for i in range(len(arr))],dtype=object) for arr in args], dtype=object)
#print(result)
last_inc = result[-1]
maxs = np.max(result)
mins = np.min(result)
run(np.array([12,12,-3,-1,2,1]), np.array([-3,-1,-2,1]), np.array([12,-12]))
預期輸出:
last incs: [array([66.66666666666666, 60.0, 50.0, 66.66666666666666, 100.0, 100.0],
dtype=object)
array([25.0, 33.33333333333333, 50.0, 100.0], dtype=object)
array([50.0, 0.0], dtype=object)]]
mins: [array([50.0, 33.33333333333333, 0.0, 0.0, 100.0, 100.0], dtype=object)
array([0.0, 0.0, 0.0, 100.0], dtype=object)
array([50.0, 0.0], dtype=object)]
maxs: [array([100.0, 100.0, 50.0, 66.66666666666666, 100.0, 100.0], dtype=object)
array([25.0, 33.33333333333333, 50.0, 100.0], dtype=object)
array([100.0, 0.0], dtype=object)]
uj5u.com熱心網友回復:
以下代碼獲取您需要的輸出,在您的函式中使用它:
size = np.empty(0)
for i in result:
size = np.append(size, np.size(i))
results_arrays = np.empty(0)
for i in np.hstack(result).T:
last_incs = np.float64(np.vstack(i)[-1])
maxs = np.max(np.vstack(i))
mins = np.min(np.vstack(i))
results_arrays = np.append(results_arrays, np.array([last_incs, maxs, mins]))
last_incs = np.array_split(results_arrays[0::3], np.cumsum(size, axis=0).astype(int), axis=0)[:-1]
maxs = np.array_split(results_arrays[1::3], np.cumsum(size, axis=0).astype(int), axis=0)[:-1]
mins = np.array_split(results_arrays[2::3], np.cumsum(size, axis=0).astype(int), axis=0)[:-1]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/322034.html
上一篇:重塑陣列以適應Keras模型
