[go: up one dir, main page]

0% found this document useful (0 votes)
5 views6 pages

Practical 1

Uploaded by

Om Bachhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Practical 1

Uploaded by

Om Bachhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

4/18/24, 11:25 AM Practical 1

MGV's Loknete Vyankatrao Hiray Arts, Science


and Commerce College Nashik

Department of Mathematics

M. Sc. 1 Data Science

Practical 1. Probability Theory

Probability

1. Example. Data will be generated by flipping a coin 10 times and


counting how many times we get heads.

In [1]: import random


def coin_trial():
heads = 0 # Declare heads inside the function
for i in range(10):
if random.random() <= 0.5:
heads += 1
return heads
def simulate(n):
trials = []
for i in range(n):
trials.append(coin_trial())
return(sum(trials)/n)
simulate(10)

Out[1]: 5.7

In [2]: simulate(100)

Out[2]: 4.83

localhost:8888/notebooks/Desktop/LVH Academic/Data Science/practical exercis/Practical 1.ipynb 1/6


4/18/24, 11:25 AM Practical 1

2. Example of choosing one number out of a set of


numbers. We can find different probabilities for this
particular problem; for instance:

selecting an even number

selecting a number divisible by 3 and 5

selecting a prime number


In [3]: set_of_numbers=[]
n=int(input("enter the number of elements: "))
print("enter the elements: ")
for i in range(0,n):
j=int(input())
set_of_numbers.append(j)
#print the set of numbers
print(" the set of numbers are: ",set_of_numbers)

enter the number of elements: 4


enter the elements:
1
2
3
4
the set of numbers are: [1, 2, 3, 4]

In [4]: def probability_of_even(a):


c=0
# here the set of numbers is the list a
total=len(a)
for i in a:
#checks whether the number is even or not
if(i%2==0):
c+=1;
#returns the probability of total even numbers/total outcomes
return(c/total)

localhost:8888/notebooks/Desktop/LVH Academic/Data Science/practical exercis/Practical 1.ipynb 2/6


4/18/24, 11:25 AM Practical 1

In [5]: def probability_of_prime(a):


c=0
# here the set of numbers is the list a
total=len(a)
for i in a:
#funciton to see if its a prime or not
if(i>1):
c2=0
for j in range(2,i):
if(i%j==0):
c2+=1
if(c2==0):
c+=1
#probability value:
return(c/total)

In [6]: def probability_divisible(a):


c=0
#here the set of numbers is list a
total=len(a)
for i in a:
#check divisibility
if(i %3==0 and i%5==0):
c+=1
# probability value:
return(c/total)

In [7]: set_of_numbers = [1,2,3,4,5,6,7,8,12,13,15]

In [8]: probability_of_even(set_of_numbers)

Out[8]: 0.45454545454545453

In [9]: probability_of_prime(set_of_numbers)

Out[9]: 0.45454545454545453

In [10]: probability_divisible(set_of_numbers)

Out[10]: 0.09090909090909091

localhost:8888/notebooks/Desktop/LVH Academic/Data Science/practical exercis/Practical 1.ipynb 3/6


4/18/24, 11:25 AM Practical 1

Conditional Probability

1. Example Suppose you have a fair six-sided die, and


you want to find out the probability on rolling a
number greater than 4, given that you rolled an odd
number.
In [11]: # define the sample space
sample_space = {1, 2, 3, 4, 5, 6}
# define the event A (roll a number greater than 4)
event_A = {5, 6}
# define the event B (roll an odd number)
event_B = {1, 3, 5}

In [12]: # calculate P(A)


prob_A = len(event_A) / len(sample_space)
# calculate P(B)
prob_B = len(event_B) / len(sample_space)

In [13]: # calculate P(A ∩ B)


event_A_intersect_B = event_A.intersection(event_B)
prob_A_intersect_B = len(event_A_intersect_B) / len(sample_space)

In [14]: # calculate P(A|B)


prob_A_given_B = prob_A_intersect_B / prob_B
print(prob_A_given_B)

0.3333333333333333

2. Example : In a family with two children, what is the


probability that , if at least one of the children is a girl,
both children are girls?
In [3]: import random
sample_size = 5
num_families_at_least_one_girl = 0
num_families_two_girls = 0

In [4]: for i in range(sample_size):


first_child = random.choice(["boy", "girl"])
second_child = random.choice(["boy", "girl"])
if first_child == "girl" or second_child == "girl":
num_families_at_least_one_girl += 1
if first_child == "girl" and second_child == "girl":
num_families_two_girls += 1
result = round(num_families_two_girls / num_families_at_least_one_girl, 2)

localhost:8888/notebooks/Desktop/LVH Academic/Data Science/practical exercis/Practical 1.ipynb 4/6


4/18/24, 11:25 AM Practical 1

In [5]: print(f"Out of {sample_size} families sampled,\


{num_families_at_least_one_girl}have at least one girl.")
print(f"Of these {num_families_two_girls} have two girls.")
print(f"This gives an experimental probability of {result} to two decimal\
places that,")
print("gives at least one child is a girl, both children are girls.")

Out of 5 families sampled,3have at least one girl.


Of these 1 have two girls.
This gives an experimental probability of 0.33 to two decimalplaces that,
gives at least one child is a girl, both children are girls.

3. Example: Suppose we send out a survey to 300


individuals asking them which sport they like best:
baseball, basketball, football, or soccer.
In [7]: import pandas as pd
import numpy as np

In [8]: df = pd.DataFrame({'gender': np.repeat(np.array(['Male', 'Female']), 150),


'sport': np.repeat(np.array(['Baseball', 'Basketball', 'Football',
'Soccer', 'Baseball', 'Basketball',
'Football', 'Soccer']),
(34, 40, 58, 18, 34, 52, 20, 44))})

In [9]: #produce contingency table to summarize raw data


survey_data = pd.crosstab(index=df['gender'], columns=df['sport'], \
margins=True)

In [10]: #view contingency table


survey_data

Out[10]:
sport Baseball Basketball Football Soccer All

gender

Female 34 52 20 44 150

Male 34 40 58 18 150

All 68 92 78 62 300

In [11]: #extract value in second row and first column


survey_data.iloc[1, 0]

Out[11]: 34

In [12]: #calculate probability of being male, given that individual prefers baseball
survey_data.iloc[1, 0] / survey_data.iloc[2, 0]

Out[12]: 0.5

localhost:8888/notebooks/Desktop/LVH Academic/Data Science/practical exercis/Practical 1.ipynb 5/6


4/18/24, 11:25 AM Practical 1

In [13]: #calculate probability of preferring basketball, given that individual isfema


survey_data.iloc[2,1] / survey_data.iloc[0, 4]

Out[13]: 0.6133333333333333

In [ ]: ​

localhost:8888/notebooks/Desktop/LVH Academic/Data Science/practical exercis/Practical 1.ipynb 6/6

You might also like