I want to turn the list comprehension below to a 3 dimensional array where it prints out the last index, max , min
of each of the iterations in the array. The (arr[i:] > 0).cumsum()/ np.arange(1, len(arr[i:]) 1) * 100)
function below calculates the the number of positive array values/ length of array
so in the first array [12,12,-3,-1,2,1]
4 out of the 6 values are positive and so it is 66.66
% as the ending result. However if you only intake the first 2 indexes [12,12.....]
it is 100
% so that was the max efficient value and if you take in the first 4 indexes [12,12,-3,-1]
the efficiency will drop to 50
%. The last_inc, maxs , mins
does not work below but the result
does. How would I be able to incorporate the last_inc, maxs , mins
in the code and get the Expected Output below?
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)
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]))
Expected Output:
last_indexes[[66.66666666666666, 60.0, 50.0, 66.66666666666666, 100.0, 100.0], [25.0, 33.33333333333333, 50.0, 100.0], [50.0, 0.0]]
max[[100.0, 100.0, 50.0, 66.66666666666666, 100.0, 100.0],[25.0, 33.33333333333333, 50.0, 100.0],[100.0, 0.0]]
min[[50.0, 33.33333333333333, 0.0, 0.0, 100.0, 100.0]]
CodePudding user response:
numpy looks like a terrible tool for what you're doing. your list comprehension is what prevents you from getting the job done. you'll have to do some filtering on the mins, and maybe wrap the code
def run2(arr):
last_incs=np.zeros_like(arr, float)
maxs=np.zeros_like(arr, float)
mins=np.zeros_like(arr, float)
for i in range(arr.size):
result=np.cumsum(arr[i:]>0)/np.arange(1,arr[i:].size 1)*100
last_incs[i]=result[-1]
maxs[i]=np.max(result)
mins[i]=np.min(result)
print(last_incs,maxs,mins)
for arr in [
np.array([12,12,-3,-1,2,1]),
np.array([-3,-1,-2,1]),
np.array([12,-12])]:
run2(arr)