Basics of Statistics and Probability_FP
Statistical measures
1. import numpy as np
2. from scipy import stats
3. import statistics
4. def measures(arr):
5. #Write your code here
6. '''
7. Input: arr : numpy array
8. Return : mean, median, std_deviation, variance, mode, iqr : float
9. Note:
10. Assign the values to designated variables
11. Round off to 2 decimal places
12. '''
13. q1= np.percentile(arr,25)
14. q3= np.percentile(arr,75)
15. Q=q3 - q1
16. mean=round(np.mean(arr),2)
17. median=round(np.median(arr),2)
18. std_deviation=round(np.std(arr),2)
19. variance=round(np.var(arr),2)
20. mode=round(statistics.mode(arr),2)
21. iqr=round(Q,2)
22. return mean,median,std_deviation,variance,mode,iqr
23. if __name__=='__main__':
24. array1=[]
25. n=int(input())
26. for i in range(n):
27. array1.append(float(input()))
28. narray1=np.array(array1)
29. print(measures(narray1))
Permutations and Combinations
1. from itertools import combinations
2. from itertools import permutations
3. import numpy as np
4. import math
5. def comb_perm(arr):
6. #Write your code here
7. '''
8. Input: arr : numpy array
9. Return : no_of_comb,no_of_perm : Integer
10. '''
11. no_of_comb=len(list(combinations((arr),2)))
12. no_of_perm=len(list(permutations((arr),2)))
13. return no_of_comb,no_of_perm
14. if __name__=='__main__':
15. array1=[]
16. n=int(input())
17. for i in range(n):
18. array1.append(input())
19. narray1=np.array(array1)
20. print(comb_perm(narray1))
Dancers Problem
1. import math
2. def dancers():
3. '''
4. output: ans : Integer
5. '''
6. #Write your code here
7. #Assign your value to variable ans
8. b= math.factorial(6)/(math.factorial(3)*math.factorial(6-3))
9. g=math.factorial(5)/(math.factorial(2)*math.factorial(5-2))
10. ans=g*b
11. return int(ans)
12. if __name__=='__main__':
13. print(dancers())
Binomial distribution
1. from scipy import stats
2. def binomial():
3. '''
4. output: ans : Float
5. '''
6. #Write your code here
7. #Assign the probability value to the variable ans
8. #Round off to 2 decimal places
9. prob = stats.binom.pmf(0,4,0.6)
10. ans = round((1-prob),2)
11. return ans
12. if __name__=='__main__':
13. print(binomial())
Poisson Distribution
1. from scipy import stats
2. def poisson():
3. '''
4. output: ans : Float
5. '''
6. #Write your code here
7. #Assign the probability value to the variable ans
8. #Round off to 2 decimal places
9. avgpass=10
10. ans=round(stats.poisson.pmf(15,avgpass),2)
11. return ans
12. if __name__=='__main__':
13. print(poisson())
Binomial-2
1. from scipy import stats
2. def spinner():
3. '''
4. output: ans : Float
5. '''
6. #Write your code here
7. #Assign the probability value to the variable ans
8. # Round off to 2 decimal places
9. probS= stats.binom.pmf(1,4,0.5)
10. probP= stats.binom.pmf(1,4,0.5)
11. ans=round(((probS+probP)),2)
12. return ans
13. if __name__=='__main__':
14. print(spinner())
Poisson-2
1. from scipy import stats
2. def accident():
3. '''
4. output: ans : Float
5. '''
6. #Write your code here
7. #Assign the probability value to the variable ans. Round off to 2 decimal places
8. Pi=0.32
9. Pa=0.09
10. Pai = 0.15
11. ans= round((Pi+Pa-Pai),2)
12. return ans
13. if __name__=='__main__':
14. print(accident())
Chi Squared Test
1. from scipy.stats import chi2_contingency
2. from scipy.stats import chi2
3. def chi_test():
4. '''
5. Output
6. 1. stat: Float
7. 2. dof : Integer
8. 3. p_val: Float
9. 4. res: String
10. '''
11. #Note: Round off the Float values to 2 decimal places.
12. table=[[18,36,21,9,6],[12,36,45,36,21],[6,9,9,3,3],[3,9,9,6,3]]
13. stat,p_val,dof,res=chi2_contingency(table)
14. prob=0.95
15. critical=chi2.ppf(prob, dof)
16. if abs(stat) >= critical:
17. res = 'Reject the Null Hypothesis'
18. else:
19. res= 'Failed to reject the Null Hypothesis'
20. alpha=1.0-prob
21. if p_val <= alpha:
22. res = 'Reject the Null Hypothesis'
23. else:
24. res = 'Failed to reject the Null Hypothesis'
25. stat = round(stat,2)
26. dof = round(dof,2)
27. p_val = round(p_val,2)
28. return stat,dof,p_val,res
29. if __name__=='__main__':
30. print(chi_test())
R_Basics_FP
Factors and Levels
In prog.R file
v <- factor(c("yes","no","yes","maybe"))
v
Date and Time in R
as.Date("25-12-2016", format = "%d-%m-%Y")
Random Numbers on R
V = round(runif(10,0,1),2)
check<-V<0.50
print(check)
Handling ‘NA’
x <- c(1,4,NA,7,9,NA,2)
x[!(is.na(x)| x%%2 == 1)]
Set operations in R
A <- (11:16)
B <- (13:20)
setdiff(A, B)
Descriptive Statistics in R
A <- (11:17)
median(A)
Conditional Operations – if/else
Marks <- c(78,85,90)
ifelse(Marks >= 80, "Above Average", "Average")
Conditional Operations – which()
M = seq(10,20)
print(which(M>13 & M<16))
for() loop in R
X <- (80:100)
count <- 0
for(val in X){
if(val %%2 == 0) count = count+val
}
print(count)
Writing functions
cube=function(x,n){x^3}
cube(10)
Variables
L <- as.Date("2018-01-01")
year <- as.numeric(substr(L, start = 1, stop = 4))
NL <- ifelse(test = year %% 400 != 0,
yes = ifelse(test = (year %% 4 == 0 | year %% 100 == 0),
yes = ifelse(test = (year + 4) %% 400 != 0,
yes = year + 4,
no = year + 8 + year %% 4),
no = ifelse(test = (year + 4 - year %% 4) %% 400 != 0,
yes = year + 4 - year %% 4,
no = year + 8 - year %% 4)),
no = year + 4)
NL
Variable Operations
L <- 100
LL <- 2
sqrt(L)*LL
Type Conversion
K <- 2.7
class(K)
ifelse((is.integer(K)), K, as.integer(K))
Factors and Levels-2
color <- factor(c("Red", "Green", "Blue"))
nlevels(color)
str(color
Random Numbers
rep(seq(2,10,2), times = 2)
Vector operations
X <- c(1,2,3,4,5)
Y <- c(2,4,6,8,10)
X*Y
Matrix Operations
MM <- matrix(c(2,4,6,8,0,9), nrow = 2)
print(MM)
String and Date
sdate <- "15081947"
ndate <- as.Date(sdate, "%d%m%Y")
ndate
POSIX date
date <-
c(as.POSIXct("15Aug1947", format = "%d%b%Y"),as.POSIXct("15Aug2018", format = "%d%b%Y"))
difftime(date[2], date[1], units = "days")
Conditional Operations-1
temp <- c(103,100,98)
ifelse(temp>100, "hot", "good")
Conditional Operations-2
toppers <- runif(10,80,100)
ifelse(length(toppers[toppers>90]), "best class", "needs improvement")
Iterations -1
factorial <- function(n){
Nfact <- 1
if(n==0 | n==1){
Nfact <- 1
}
else{
while(n>=1){
Nfact <- Nfact * n
n <- n-1
}
}
return (Nfact)
}
factorial(6)
Iterations – 2
x <- 0
sum <- 0
while(x>=0)
{
x <- x+1
sum <- sum + x
if(x==20)
break
}
print(sum)
Funtions which()
Virat_Scored <- c(98,102,120,145,175,169,118,177,101,200)
a <- which(Virat_Scored %% 2 == 0)
print(a)
Function str()
emp <- data.frame(
empno = 11,
name = "Alex",
age = 32,
dept = "sales"
)
str(emp)
Userdefined functions
pyth <- function(a,b)
{hyp <- sqrt(a^2 + b^2)
return (hyp)
}
pyth(3,4)
Vectors in R
V1 <- c(1,2,3,4,5)
V2 <- c(10,20,30,40,50)
V <- V1+V2
V
Vectors and Matrices
V <- c(1:9)
M <- matrix(V, nrow = 3, byrow = TRUE)
M*2
Categorical Numeric variable in R
v1 <- factor(floor((runif(10,min=1,max=101))))
v2 <- as.numeric(levels(v1))[v1]
v3 <- v1 == v2
print(v3)