Skip to content

IQR function is added #8851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 53 commits into from
Aug 7, 2023
Merged
Changes from 52 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
60235f4
tanh function been added
TMGA-WAY Apr 23, 2023
dbea59d
tanh function been added
TMGA-WAY Apr 23, 2023
6e3f28d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 23, 2023
d42af7b
tanh function is added
TMGA-WAY Apr 23, 2023
84ea4c1
tanh function is added
TMGA-WAY Apr 23, 2023
fa675a4
tanh function is added
TMGA-WAY Apr 23, 2023
a6b5d05
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 23, 2023
2827b15
tanh function added
TMGA-WAY Apr 23, 2023
8092744
tanh function added
TMGA-WAY Apr 23, 2023
9114b52
tanh function added
TMGA-WAY Apr 23, 2023
bbf4067
Merge branch 'TheAlgorithms:master' into master
TMGA-WAY Apr 24, 2023
a4ca897
tanh function is added
TMGA-WAY Apr 24, 2023
e049502
Apply suggestions from code review
cclauss Apr 25, 2023
20639a6
Merge branch 'TheAlgorithms:master' into master
TMGA-WAY Apr 25, 2023
66cc81a
ELU activation function is added
TMGA-WAY Apr 27, 2023
d40c74b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 27, 2023
5453434
elu activation is added
TMGA-WAY Apr 28, 2023
0d1546e
ELU activation is added
TMGA-WAY Apr 28, 2023
112611a
Update maths/elu_activation.py
TMGA-WAY Apr 28, 2023
dad8cf1
Exponential_linear_unit activation is added
TMGA-WAY Apr 28, 2023
34be18c
Exponential_linear_unit activation is added
TMGA-WAY Apr 28, 2023
1ceb47a
Merge branch 'TheAlgorithms:master' into master
TMGA-WAY May 2, 2023
aae8727
SiLU activation is added
TMGA-WAY May 2, 2023
28da38a
SiLU activation is added
TMGA-WAY May 2, 2023
ba0f68b
Merge branch 'TheAlgorithms:master' into master
TMGA-WAY May 14, 2023
fcab387
mish added
TMGA-WAY May 14, 2023
6e7ed74
Merge branch 'TheAlgorithms:master' into master
TMGA-WAY May 16, 2023
b1731ec
mish activation is added
TMGA-WAY May 16, 2023
ae10683
Merge branch 'TheAlgorithms:master' into master
TMGA-WAY May 20, 2023
9a5cb30
inter_quartile_range function is added
TMGA-WAY May 20, 2023
3296963
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 20, 2023
95caae2
Mish activation function is added
TMGA-WAY May 20, 2023
784199a
Mish action is added
TMGA-WAY May 20, 2023
c91bc2a
mish activation added
TMGA-WAY Jun 20, 2023
44a5ff8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 20, 2023
d6095e6
Merge branch 'TheAlgorithms:master' into master
TMGA-WAY Jun 20, 2023
88f05ec
mish activation added
TMGA-WAY Jun 20, 2023
30bd9c2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 20, 2023
9776f93
Merge branch 'TheAlgorithms:master' into master
TMGA-WAY Jul 2, 2023
1355f9d
inter quartile range (IQR) function is added
TMGA-WAY Jul 2, 2023
483d59a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 2, 2023
aa5dccb
IQR function is added
TMGA-WAY Jul 2, 2023
2b5d7c6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 2, 2023
ee3247b
code optimized in IQR function
TMGA-WAY Jul 3, 2023
436a483
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 3, 2023
a3fcc64
Merge branch 'TheAlgorithms:master' into master
TMGA-WAY Aug 1, 2023
ae37fc4
interquartile_range function is added
TMGA-WAY Aug 1, 2023
14a3fb4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 1, 2023
da1c063
Update maths/interquartile_range.py
TMGA-WAY Aug 5, 2023
05644a0
Changes on interquartile_range
TMGA-WAY Aug 6, 2023
0e72de6
numpy removed from interquartile_range
TMGA-WAY Aug 6, 2023
9c8c26f
Fixes from code review
cclauss Aug 7, 2023
90748c8
Update interquartile_range.py
cclauss Aug 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions maths/interquartile_range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
An implementation of interquartile range (IQR) which is a measure of statistical
dispersion, which is the spread of the data.

The function takes the list of numeric values as input and returns the IQR.

Script inspired by this Wikipedia article:
https://en.wikipedia.org/wiki/Interquartile_range
"""
from __future__ import annotations


def find_median(nums: list[int | float]) -> float:
"""
This is the implementation of the median.
:param nums: The list of numeric nums
:return: Median of the list
>>> find_median(nums=([1, 2, 2, 3, 4]))
2
>>> find_median(nums=([1, 2, 2, 3, 4, 4]))
2.5
>>> find_median(nums=([-1, 2, 0, 3, 4, -4]))
1.5
>>> find_median(nums=([1.1, 2.2, 2, 3.3, 4.4, 4]))
2.65
"""
div, mod = divmod(len(nums), 2)
if mod:
return nums[div]
return float((nums[div] + nums[(div) - 1]) / 2)


def interquartile_range(nums: list[int | float]) -> float:
"""
Return the interquartile range for a list of numeric values.
:param nums: The list of numeric values.
:return: interquartile range

>>> interquartile_range(nums=[4, 1, 2, 3, 2])
2.0
>>> interquartile_range(nums = [-2, -7, -10, 9, 8, 4, -67, 45])
17.0
>>> interquartile_range(nums = [-2.1, -7.1, -10.1, 9.1, 8.1, 4.1, -67.1, 45.1])
17.2
>>> interquartile_range(nums = [0, 0, 0, 0, 0])
0.0
>>> interquartile_range(nums=[])
Traceback (most recent call last):
...
ValueError: The list is empty. Provide a non-empty list.
"""
if not nums:
raise ValueError("The list is empty. Provide a non-empty list.")
nums.sort()
length = len(nums)
div, mod = divmod(length, 2)
q1 = find_median(nums[:div])
half_length = sum((div, mod))
q3 = find_median(nums[half_length:length])
return q3 - q1


if __name__ == "__main__":
import doctest

doctest.testmod()