Search This Blog

Permutations and Combinations - Hands-on- Probability and Statistics - Statistical Measures -1- HackerRank Fresco Play

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

Answer:

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
    '''
    q3, q1 = np.percentile(arr, [75 ,25])
    
    arr = np.array(arr)
    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(statistics.mode(arr),2)
    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))
    
Click on the image to check test cases:


No comments:

Post a Comment

If you have any doubts, Please let us know.