[PYTHON] Data Scientist Training Course Chapter 4 Day 1

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.

Study of probability

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.

Exercise 1

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)

Exercise 2

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

Data Scientist Training Course Chapter 3 Day 3
Data Scientist Training Course Chapter 4 Day 1
Data Scientist Training Course Chapter 3 Day 1 + 2
Solving Exercises in GCI Data Scientist Training Course Chapter6
Solving Exercises in GCI Data Scientist Training Course Chapter7
Solving Exercises in GCI Data Scientist Training Course Chapter8
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
Creating training data
Training data by CNN
Python for Data Analysis Chapter 4
<Course> Deep Learning: Day2 CNN
<Course> Deep Learning: Day1 NN
Infra_ Data Science Course Output
Python for Data Analysis Chapter 2
Python for Data Analysis Chapter 3