In my program I have total of 3 steps.
- To find the mean
- Split the given list using the mean value
- If the threshold value > epsilon then update mean value and repeat the step 2 again with updated mean value, else finished.
I have achieved above two steps anyhow. Third step I have achieved only half and struggling in how to repeat the step 2 till the threshold value will be less than epsilon.
My code is below:
import numpy as np
store_list = [1,2,3,1,2,3,1]
# Step 1
mean_value = np.mean(store_list)
print("Mean Value : " + str(mean_value))
# Step 2
small_list = [i for i in store_list if i <= mean_value]
big_list = [i for i in store_list if i > mean_value]
print(small_list)
print(big_list)
toe_1 = np.mean(small_list)
toe_2 = np.mean(big_list)
print("toe value 1 : " + str(toe_1))
print("toe value 2 : " + str(toe_2))
toe_sum = np.sum([toe_1, toe_2])
toe_cap = np.divide(toe_sum, 2)
print("toe cap : " + str(toe_cap))
# Step 3
new_threshold = np.subtract(mean_value, toe_cap)
print(new_threshold)
epsilon_value = 0.1
if new_threshold > epsilon_value:
mean_value = toe_cap
print("new mean : " + str(mean_value))
My output of above is:
Mean Value : 1.8571428571428572 [1, 1, 1] [2, 3, 2, 3] toe value 1 : 1.0 toe value 2 : 2.5 toe cap : 1.75 new threshold : 0.1071428571428572 new mean : 1.75
In this case I have to repeat the step 2 again considering new mean. I am new to Python.
Advertisement
Answer
I’m not able to numpy but i can imagine that all you need is a recursive runner function, i don’t want to break the structure of your code, therefore i did not try to optimize the code. You can try the following code, i tried, it works:
import numpy as np
def step2(store_list, mean_value):
small_list = [i for i in store_list if i <= mean_value]
big_list = [i for i in store_list if i > mean_value]
toe_1 = np.mean(small_list)
toe_2 = np.mean(big_list)
toe_sum = np.sum([toe_1, toe_2])
toe_cap = np.divide(toe_sum, 2)
return small_list, big_list, toe_1, toe_2, toe_cap
def step3(mean_value, toe_cap):
new_threshold = np.subtract(mean_value, toe_cap)
print(new_threshold)
epsilon_value = 0.1
if new_threshold > epsilon_value:
return True, toe_cap
else:
return False, mean_value
def my_run(store_list, mean_value):
small_list, big_list, toe_1, toe_2, toe_cap = step2(store_list, mean_value)
print(small_list)
print(big_list)
print("toe value 1 : " + str(toe_1))
print("toe value 2 : " + str(toe_2))
print("toe cap : " + str(toe_cap))
condition, mean_value = step3(mean_value, toe_cap)
if condition:
print("new mean : " + str(mean_value))
my_run(store_list, mean_value)
def main():
store_list = [1, 2, 3, 1, 2, 3, 1]
mean_value = np.mean(store_list)
print("Mean Value : " + str(mean_value))
my_run(store_list, mean_value)
if __name__ == '__main__':
main()