mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
inter quartile range (IQR) function is added
This commit is contained in:
parent
9776f93dc9
commit
1355f9daa5
61
maths/inter_quartile_range.py
Normal file
61
maths/inter_quartile_range.py
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
"""
|
||||||
|
This is the implementation of inter_quartile range (IQR).
|
||||||
|
|
||||||
|
function takes the list of numeric values as input
|
||||||
|
and return the IQR as output.
|
||||||
|
|
||||||
|
Script inspired from its corresponding Wikipedia article
|
||||||
|
https://en.wikipedia.org/wiki/Interquartile_range
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
|
def find_median(x: List[float]) -> float:
|
||||||
|
"""
|
||||||
|
This is the implementation of median.
|
||||||
|
:param x: The list of numeric values
|
||||||
|
:return: Median of the list
|
||||||
|
>>> find_median([1,2,2,3,4])
|
||||||
|
2
|
||||||
|
|
||||||
|
>>> find_median([1,2,2,3,4,4])
|
||||||
|
2.5
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
length = len(x)
|
||||||
|
if length % 2:
|
||||||
|
return x[length // 2]
|
||||||
|
return float((x[length // 2] + x[(length // 2) - 1]) / 2)
|
||||||
|
|
||||||
|
|
||||||
|
def inter_quartile_range(x: List[float]) -> float:
|
||||||
|
"""
|
||||||
|
This is the implementation of inter_quartile
|
||||||
|
range for a list of numeric.
|
||||||
|
:param x: The list of data point
|
||||||
|
:return: Inter_quartile range
|
||||||
|
|
||||||
|
>>> inter_quartile_range([4,1,2,3,2])
|
||||||
|
2.0
|
||||||
|
|
||||||
|
>>> inter_quartile_range([25,32,49,21,37,43,27,45,31])
|
||||||
|
18.0
|
||||||
|
"""
|
||||||
|
length = len(x)
|
||||||
|
if length == 0:
|
||||||
|
raise ValueError
|
||||||
|
x.sort()
|
||||||
|
q1 = find_median(x[0: length // 2])
|
||||||
|
if length % 2:
|
||||||
|
q3 = find_median(x[(length // 2) + 1: length])
|
||||||
|
else:
|
||||||
|
q3 = find_median(x[length // 2: length])
|
||||||
|
return q3 - q1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
@ -1,40 +0,0 @@
|
|||||||
"""
|
|
||||||
Implements the Mish activation functions.
|
|
||||||
|
|
||||||
The function takes a vector of K real numbers input and then
|
|
||||||
applies the mish function, x*tanh(softplus(x) to each element of the vector.
|
|
||||||
|
|
||||||
Script inspired from its corresponding Wikipedia article
|
|
||||||
https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
|
|
||||||
|
|
||||||
The proposed paper link is provided below.
|
|
||||||
https://arxiv.org/abs/1908.08681
|
|
||||||
"""
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
from maths.tanh import tangent_hyperbolic as tanh
|
|
||||||
|
|
||||||
|
|
||||||
def mish_activation(vector: np.ndarray) -> np.ndarray:
|
|
||||||
"""
|
|
||||||
Implements the Mish function
|
|
||||||
|
|
||||||
Parameters:
|
|
||||||
vector: np.array
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Mish (np.array): The input numpy array after applying tanh.
|
|
||||||
|
|
||||||
mathematically, mish = x * tanh(softplus(x) where
|
|
||||||
softplus = ln(1+e^(x)) and tanh = (e^x - e^(-x))/(e^x + e^(-x))
|
|
||||||
so, mish can be written as x * (2/(1+e^(-2 * softplus))-1
|
|
||||||
|
|
||||||
"""
|
|
||||||
soft_plus = np.log(np.exp(vector) + 1)
|
|
||||||
return vector * tanh(soft_plus)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import doctest
|
|
||||||
|
|
||||||
doctest.testmod()
|
|
Loading…
x
Reference in New Issue
Block a user