Probability and Statistics - Statistical Measures

Rumman Ansari   Software Engineer   2023-02-22   365 Share
☰ Table of Contents

Table of Content:


Probability and Statistics - Statistical Measures
In this scenario, you will be exploring the most commonly used statistical measures
1. Mean
2. Median
3. Standard Deviation
4. Variance
5. Mode
6. Inter Quartile Range

Function Description
Function Name: measures()
1. Input:
arr - Numpy array
2. Output:
mean,median,std_deviation,variance,mode,iqr : tuple of results
Note: Every value must be rounded off to 2 decimal places

 

Sample Input For Custom Testing
4
1
2
4
4
Sample Output
(2.75, 3.0, 1.3, 1.69, 4.0, 2.25)
Explanation
Mean=2.75
Median=3.0
Standard Deviation=1.3
Variance=1.69
Mode=4.0
Inter-quartile Range=2.25


import numpy as np
from scipy import stats
import statistics

def measures(arr):
    #Write your code here
    '''
    Input: arr : numpy array    
    Return : mean,median,std_deviation,variance,mode,iqr  : float
    
    Note: 
    1. Assign the values to designated variables
    2. Round off to 2 decimal places
    '''
    mean = round(np.mean(arr), 2)
    median = round(np.median(arr), 2)
    std_deviation = round(np.std(arr), 2)
    variance = round(np.var(arr), 2)
    mode = round(stats.mode(arr)[0][0], 2)
    q1, q3 = np.percentile(arr, [25, 75])
    iqr = round(q3 - q1, 2)
    
    
    return mean,median,std_deviation,variance,mode,iqr   
if __name__=='__main__':
    array1=[]
    n=int(input())
    for i in range(n):
        array1.append(float(input()))
    narray1=np.array(array1)
    print(measures(narray1))