If you take a little care, the gap will open at once. I have to get back to normal operation somehow! So, it's time to enter Chapter 4. It took a long time because there was a part of the exercise in Chapter 3 that I had a headache.
Knowledge about probabilities and sets is required, but honestly, I am not confident at all. A ∩ B or A ∪ B What was it? .. It was like that. Someone like U is "or". If you remember it as Union, you'll understand. I don't remember the likelihood at all.
The problem of finding the probability of a coin. This itself is almost the same as the content that came out earlier, so there is no particular problem
coin = np.array([0, 1])
random.seed(0)
tos_count = 1000
coin_result = random.choice(coin, tos_count)
print("Probability of getting 0", len(coin_result[coin_result==0])/tos_count)
print("Probability of 1", len(coin_result[coin_result==1])/tos_count)
Consider the problem of lottery. Suppose there are 100 lots in 1000 lots. Find the probability that A and B will draw lots in order, and both A and B will win. However, assuming that the drawn lottery will not be returned, each will be drawn only once. (This can be calculated manually.)
Hmmm, does not return the lottery mean that the probability is constantly changing? I tried assembling for the time being
np.random.seed(0)
#Create an array with 1 as the deviation from 0
lott = np.array([0]*900+[1]*100)
result = np.array([])
while len(lott) >= 2:
#Lottery
#If it is a hit, delete the element from the back. If it is off, delete the element from the front
result_a = np.random.choice(lott, 1)
if result_a == 1:
lott = np.delete(lott, len(lott)-1)
else:
lott = np.delete(lott, 0)
result_b = np.random.choice(lott, 1)
if result_b == 1:
lott = np.delete(lott, len(lott)-1)
else:
lott = np.delete(lott, 0)
#If both of them are hit, add 1 to result. Add 0 otherwise
if result_a == 1 and result_b == 1:
result = np.append(result, 1)
else:
result = np.append(result, 0)
print("Probability that A and B will hit", len(result[result==1])/len(result))
From the result, it's probably not wrong, but I think it's wrong as a solution. There must be a better and more elegant way to do it, but I've solved it in the sense of getting used to Python for the time being.
I want a model answer. .. ..
Recommended Posts