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:
JavaScript
x
30
30
1
import numpy as np
2
store_list = [1,2,3,1,2,3,1]
3
4
# Step 1
5
mean_value = np.mean(store_list)
6
print("Mean Value : " + str(mean_value))
7
8
# Step 2
9
small_list = [i for i in store_list if i <= mean_value]
10
big_list = [i for i in store_list if i > mean_value]
11
print(small_list)
12
print(big_list)
13
14
toe_1 = np.mean(small_list)
15
toe_2 = np.mean(big_list)
16
print("toe value 1 : " + str(toe_1))
17
print("toe value 2 : " + str(toe_2))
18
19
toe_sum = np.sum([toe_1, toe_2])
20
toe_cap = np.divide(toe_sum, 2)
21
print("toe cap : " + str(toe_cap))
22
23
# Step 3
24
new_threshold = np.subtract(mean_value, toe_cap)
25
print(new_threshold)
26
epsilon_value = 0.1
27
if new_threshold > epsilon_value:
28
mean_value = toe_cap
29
print("new mean : " + str(mean_value))
30
My output of above is:
JavaScript
1
9
1
Mean Value : 1.8571428571428572
2
[1, 1, 1]
3
[2, 3, 2, 3]
4
toe value 1 : 1.0
5
toe value 2 : 2.5
6
toe cap : 1.75
7
new threshold : 0.1071428571428572
8
new mean : 1.75
9
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:
JavaScript
1
47
47
1
import numpy as np
2
3
4
def step2(store_list, mean_value):
5
small_list = [i for i in store_list if i <= mean_value]
6
big_list = [i for i in store_list if i > mean_value]
7
toe_1 = np.mean(small_list)
8
toe_2 = np.mean(big_list)
9
toe_sum = np.sum([toe_1, toe_2])
10
toe_cap = np.divide(toe_sum, 2)
11
return small_list, big_list, toe_1, toe_2, toe_cap
12
13
14
def step3(mean_value, toe_cap):
15
new_threshold = np.subtract(mean_value, toe_cap)
16
print(new_threshold)
17
epsilon_value = 0.1
18
if new_threshold > epsilon_value:
19
return True, toe_cap
20
else:
21
return False, mean_value
22
23
24
def my_run(store_list, mean_value):
25
small_list, big_list, toe_1, toe_2, toe_cap = step2(store_list, mean_value)
26
print(small_list)
27
print(big_list)
28
print("toe value 1 : " + str(toe_1))
29
print("toe value 2 : " + str(toe_2))
30
print("toe cap : " + str(toe_cap))
31
condition, mean_value = step3(mean_value, toe_cap)
32
if condition:
33
print("new mean : " + str(mean_value))
34
my_run(store_list, mean_value)
35
36
37
def main():
38
store_list = [1, 2, 3, 1, 2, 3, 1]
39
mean_value = np.mean(store_list)
40
print("Mean Value : " + str(mean_value))
41
my_run(store_list, mean_value)
42
43
44
if __name__ == '__main__':
45
main()
46
47