Place 2 values randomly in array with certain probability - python

I wanted to create a random array with the lenght of 24 in python, by assigning the values of a list seq=[30,170]. So I wanted to have an array which looks like a=[30,30,30,170,30,170,...]. The problem is, that the values shouldn't be assigned completely random, but with a certain probability. In this case, if the first value is 30 (or 170) the probability of the next value to be 30 (or 170) as well, should be 90% and the probability of the next value to be 170 (or 30) should be 10%.
Does anyone know how to do this? For the moment I only found how to arrange them randomly. Thanks!!
import random
seq=[30,170]
a = random.choices(seq,k=24)

You can provide weights to random.choices(population, weights=None, *, cum_weights=None, k=1).
You need to do it iteratively to be able to reference the last value when looking up weight you need to use:
import random
seq = [30, 170]
# weight lookup
wgt = {30: (90, 10), 170:(10, 90)}
r = []
for _ in range(24):
if not r:
# need some initial value: 50/50
r.append(random.choice(seq))
else:
# k == 1, using correct weights from lookup, use only single value and add it
r.append(random.choices(seq, weights=wgt[r[-1]], k=1)[0])
print(r)
Output over 8 runs:
[170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170]
[30, 30, 30, 30, 30, 30, 170, 170, 170, 170, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
[30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
[30, 30, 30, 30, 30, 170, 170, 170, 170, 170, 170, 170, 170, 30, 30, 170, 170, 170, 170, 170, 170, 170, 170, 170]
[170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 30, 30, 30, 170, 170, 170, 170]
[170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 30, 30, 30, 30, 30, 30, 30, 30, 30]
[30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
[30, 30, 170, 170, 30, 170, 170, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
You can also provide the weighting yourself:
seq = [30,170]
w = {30: (30,30,30,30,30,30,30,30,30,170), # if 30: use 9 * 30 + 1 * 170
170:(30,170,170,170,170,170,170,170,170,170)} # if 170: use 9 * 170 + 1 * 30
r = []
for _ in range(24):
if not r:
r.append(random.choice(seq))
else:
r.append(random.choice(w[r[-1]]))

Related

how to sum each array in 3d array

i have some array
`
arr_values = [[71, 70, 80, 80, 50, 90, 100, 85, 75], [70, 70, 80, 80, 50, 50, 100, 85, 75], [70, 70, 80, 80, 50, 90, 100, 85, 78]],
[[70, 70, 80, 80, 50, 90, 100, 85, 75], [70, 70, 80, 80, 50, 50, 100, 85, 75], [70, 70, 80, 80, 50, 90, 100, 85, 79]],
[[70, 70, 80, 80, 50, 90, 100, 85, 75], [70, 70, 80, 80, 50, 50, 100, 85, 75], [70, 70, 80, 80, 50, 90, 100, 85, 80]],
[[70, 70, 80, 80, 50, 90, 100, 85, 75], [70, 70, 80, 80, 50, 50, 100, 85, 75], [70, 70, 80, 80, 50, 90, 100, 85, 81]],
[[70, 70, 80, 80, 50, 90, 100, 85, 73], [70, 70, 80, 80, 50, 50, 100, 85, 74], [70, 70, 80, 80, 50, 90, 100, 85, 76], [70, 70, 80, 80, 50, 90, 100, 82, 73]]
`
i want it become like this
`
[[701] [660] [703]]
[[700] [660] [704]]
[[700] [660] [705]]
[[700] [660] [706]]
[[698] [659] [701] [695]]
`
i have try with this way
`
for values in arr_values:
for value in range(len(values)):
a=0
for a in values[value]:
a += a
print(a)
`
the result is not what i want. how to loop correctly?
`
for values in arr_values:
for value in range(len(values)):
a=0
for a in values[value]:
a += a
print(a)
`
the result is not what i want. how to loop correctly?
If you want to return a list of sums, do it by creating an empty list and then appending each sum iteratively.
arraySums = []
for subArray in arr_values:
arrays = []
for array in subArray:
curSum = 0
for elem in array:
curSum += elem
arrays.append([curSum])
arraySums.append(arrays)
Note: arr_values is a tuple of lists
Explanation: Create a list that will hold the result of everything outside of the loop so you can store the results. After that, iterate over the arr_values tuple to get the 2d lists you'll be summing. The inner two for loops are then the sum of a 2 dimensional list, appended to the master list after.

Partition a list on python

The following code partitions a list in spaces of 5.
o_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
def partition(lst, size):
for i in range(0, len(lst), size):
yield lst[i :: size]
# size of each partition
n = 5
p_list = list(partition(o_list, n))
print("Original List: ")
print(o_list)
print("Partitioned List:")
print(p_list)
The following are the results:
Original List:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
Partitioned List:
[[10, 60, 110, 160], [60, 110, 160], [110, 160], [160]]
However I want the second array to be [20, 70, 120, 170] and the third and so on follow suit.
Just replace the comma in the range function to a // operator:
o_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
def partition(lst, size):
for i in range(0, len(lst) // size):
yield lst[i :: size]
# size of each partition
n = 5
p_list = list(partition(o_list, n))
print("Original List: ")
print(o_list)
print("Partitioned List:")
print(p_list)
This prints:
Original List:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
Partitioned List:
[[10, 60, 110, 160], [20, 70, 120, 170], [30, 80, 130, 180], [40, 90, 140, 190]]
Something like this?
o_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
[o_list[x::5] for x in range(len(o_list)//5)]
[[10, 60, 110, 160],
[20, 70, 120, 170],
[30, 80, 130, 180],
[40, 90, 140, 190]]
With division you can figure out how many slices can be produced, then simply produce those slices in a loop.
partition = lambda L, N: [L[n::N] for n in range(len(L)//N)]
print(partition(list(range(10, 201, 10)), 5))
#[[10, 60, 110, 160], [20, 70, 120, 170], [30, 80, 130, 180], [40, 90, 140, 190]]

How do I make a list of every other ten numbers in Python

I need a list of integers that skips every ten numbers. I've tried to do it with brute force:
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
This has been okay so far, but I need to go much farther and can't type in every number.
I've tried this as well, but I guess I don't understand how range works:
x = list(range(10))
y = list(range(20:29))
z = list(range(40:49))
final_list = x + y + z
Any help would be greatly appreciated.
Something like this will probably work . Just change the range to repeat how ever many times you need it. The +20 is 10 for the first that is included and then for the ones skipped. And I increase both limits
my_list = []
lower=0
high=10
for n in range(5):
x = range(lower, high)
for n in x:
my_list.append(n)
lower=lower+20;
high=high+20;
y = list(range(20:29))
z = list(range(40:49))
was invalided.
range(start, end, step).Under your circumstance, you need these numbers: [20, 30), [40, 50).
So just use(like #jizhihaoSAMA said in comment):
x = list(range(10))
y = list(range(20, 30))
z = list(range(40, 50))
final_list = x + y + z
print(final_list)
Result:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
https://i.stack.imgur.com/cGygs.png]
x = list(range(11))
y = list(range(20, 30))
z = list(range(40, 50))
final_list = x + y + z
print(final_list)
You could also do this with a bit of math by adding the appropriate multiple of 10 to sequential numbers:
[ n + 10*(n//10) for n in range(100)]
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189]

Is there a way to label the mean and median in matplotlib boxplot legend?

I have the following box plot which plots some values with different mean and median values for each box; I am wondering if there is any way to label them so that they appear on the graph legend (because the current box plot plots an orange line for the median and a blue dot for the mean and it is not so clear which is which)? Also is there a way to make one legend for these subplots, instead of having a legend for each one, since they are essentially the same objects just different data?
Here's a code example for one of the subplots, the other subplots are the same but have different data:
fig = plt.figure()
xlim = (4, 24)
ylim = (0, 3700)
plt.subplot(1,5,5)
x_5_diff = {5: [200, 200, 291, 200, 291, 200, 291, 200, 291, 200, 291, 200, 291, 200, 291],
7: [161, 161, 179, 161, 179, 161, 179, 161, 179, 161, 179, 161, 179, 161, 179],
9: [205, 205, 109, 205, 109, 205, 109, 205, 109, 205, 109, 205, 109, 205, 109],
11: [169, 169, 95, 169, 95, 169, 95, 169, 95, 169, 95, 169, 95, 169, 95],
13: [43, 43, 70, 43, 70, 43, 70, 43, 70, 43, 70, 43, 70, 43, 70],
15: [33, 33, 39, 33, 39, 33, 39, 33, 39, 33, 39, 33, 39, 33, 39],
17: [23, 23, 126, 23, 126, 23, 126, 23, 126, 23, 126, 23, 126, 23, 126],
19: [17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17],
21: [15, 15, 120, 15, 120, 15, 120, 15, 120, 15, 120, 15, 120, 15, 120],
23: [63, 63, 25, 63, 25, 63, 25, 63, 25, 63, 25, 63, 25, 63, 25]}
keys = sorted(x_5_diff)
plt.boxplot([x_5_diff[k] for k in keys], positions=keys) # box-and-whisker plot
plt.hlines(y = 1600, colors= 'r', xmin = 5, xmax = 23, label = "Level 1 Completed")
plt.title("x = 5 enemies")
plt.ylim(0,3700)
plt.plot(keys, [sum(x_5_diff[k]) / len(x_5_diff[k]) for k in keys], '-o')
plt.legend()
plt.show()
Any help would be appreciated.
Its a bit late, but try this:
bp = plt.boxplot([x_5_diff[k] for k in keys], positions=keys)
# You can access boxplot items using ist dictionary
plt.legend([bp['medians'][0], bp['means'][0]], ['median', 'mean'])
Store the mean as a separate vector. Loop over the vectors to plot.
(Will try to give implementation, as soon as I have my laptop)

how to show over count sequential in matplotlib pyplot? [duplicate]

This question already has answers here:
Changing the tick frequency on the x or y axis
(13 answers)
Closed 3 years ago.
My Code
over = list(range(1,21))
bangladesh = [10, 12, 15, 19, 22, 33, 40, 54, 67, 77, 89, 102, 120, 135, 155, 170, 195, 209, 220, 235]
india = [1, 6, 10, 19, 22, 31, 32, 34, 47, 54, 67, 79, 100, 125, 135, 140, 145, 157, 168, 175]
plt.plot(over, bangladesh, label='Bangladesh')
plt.plot(over, india, label='India')
plt.title('Run Overflow')
plt.xlabel('Over')
plt.ylabel('Run')
plt.legend(loc='best')
plt.show()
This is my output of this code
Here is show the over count 2.5, 5, 7.5 ...
but i want to see the over 1, 2, 3, 4, 5.....
so, how can i do this ?
You need to add plt.xticks:
over = list(range(1,21))
bangladesh = [10, 12, 15, 19, 22, 33, 40, 54, 67, 77, 89, 102, 120, 135, 155, 170, 195, 209, 220, 235]
india = [1, 6, 10, 19, 22, 31, 32, 34, 47, 54, 67, 79, 100, 125, 135, 140, 145, 157, 168, 175]
plt.plot(over, bangladesh, label='Bangladesh')
plt.plot(over, india, label='India')
plt.title('Run Overflow')
plt.xlabel('Over')
plt.ylabel('Run')
plt.xticks(range(1,21))
plt.legend(loc='best')
plt.show()

Categories

Resources