From 60235f49defb07cf43de929f5cf4b45218c3b69f Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sun, 23 Apr 2023 17:01:55 +0530 Subject: [PATCH 01/42] tanh function been added --- maths/tanh.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 maths/tanh.py diff --git a/maths/tanh.py b/maths/tanh.py new file mode 100644 index 000000000000..9a42ae31f235 --- /dev/null +++ b/maths/tanh.py @@ -0,0 +1,32 @@ +""" +This script demonstrates the implementation of the tangent hyperbolic or tanh function. + +The function takes a vector of K real numbers as input and then (e^x - e^(-x))/(e^x + e^(-x)). +After through tanh, the element of the vector mostly -1 between 1. + +Script inspired from its corresponding Wikipedia article +https://en.wikipedia.org/wiki/Activation_function +""" +import numpy +import numpy as np + + +def tanh(vector): + ''' + Implements the tanh function + + Parameters: + vector: np.array, list, tuple consisting real values + + Returns: + tanh (np.array): The input numpy array after applying + tanh. + + mathematically (e^x - e^(-x))/(e^x + e^(-x)) can be written as (2/(1+e^(-2x))-1 + ''' + exp_vector = np.exp(-2 * vector) + return (2 / (1 + exp_vector)) - 1 + + +if __name__ == '__main__': + print(tanh(np.array([1, 5, 6, 113, 13, 16, -5.23]))) From dbea59dfbc7033a675956246c7389c81c705b195 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sun, 23 Apr 2023 17:24:14 +0530 Subject: [PATCH 02/42] tanh function been added --- maths/tanh.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/maths/tanh.py b/maths/tanh.py index 9a42ae31f235..70306c5ee047 100644 --- a/maths/tanh.py +++ b/maths/tanh.py @@ -11,8 +11,8 @@ import numpy as np -def tanh(vector): - ''' +def tanh_func(vector): + """ Implements the tanh function Parameters: @@ -23,10 +23,15 @@ def tanh(vector): tanh. mathematically (e^x - e^(-x))/(e^x + e^(-x)) can be written as (2/(1+e^(-2x))-1 - ''' + + Examples: + >>> tanh_func(np.array([1, 5, 6, 113, 13, 16, -5.23])) + [ 0.76159416 0.9999092 0.99998771 1. 1. 1. -0.99994268] + """ + exp_vector = np.exp(-2 * vector) return (2 / (1 + exp_vector)) - 1 if __name__ == '__main__': - print(tanh(np.array([1, 5, 6, 113, 13, 16, -5.23]))) + print(tanh_func(np.array([0.23]))) From 6e3f28d9d9b5378c6f3aed45f4c95bfc00e1bdcc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Apr 2023 11:58:40 +0000 Subject: [PATCH 03/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/tanh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/tanh.py b/maths/tanh.py index 70306c5ee047..93ef6ec462de 100644 --- a/maths/tanh.py +++ b/maths/tanh.py @@ -33,5 +33,5 @@ def tanh_func(vector): return (2 / (1 + exp_vector)) - 1 -if __name__ == '__main__': +if __name__ == "__main__": print(tanh_func(np.array([0.23]))) From d42af7b70f5315589c687219cb0b781db5b51375 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sun, 23 Apr 2023 17:34:24 +0530 Subject: [PATCH 04/42] tanh function is added --- maths/tanh.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/maths/tanh.py b/maths/tanh.py index 70306c5ee047..a8c1d7cf3dfe 100644 --- a/maths/tanh.py +++ b/maths/tanh.py @@ -9,14 +9,15 @@ """ import numpy import numpy as np +import doctest -def tanh_func(vector): +def tanh_func(vector: np.array) -> np.array: """ Implements the tanh function Parameters: - vector: np.array, list, tuple consisting real values + vector: np.array Returns: tanh (np.array): The input numpy array after applying @@ -26,7 +27,9 @@ def tanh_func(vector): Examples: >>> tanh_func(np.array([1, 5, 6, 113, 13, 16, -5.23])) - [ 0.76159416 0.9999092 0.99998771 1. 1. 1. -0.99994268] + array([ 0.76159416, 0.9999092 , 0.99998771, 1. , 1. , + 1. , -0.99994268]) + """ exp_vector = np.exp(-2 * vector) @@ -34,4 +37,5 @@ def tanh_func(vector): if __name__ == '__main__': - print(tanh_func(np.array([0.23]))) + doctest.testmod() + From fa675a47fda6af87e6f2e03a18b8d84df12e27a0 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sun, 23 Apr 2023 17:42:22 +0530 Subject: [PATCH 05/42] tanh function is added --- maths/tanh.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/tanh.py b/maths/tanh.py index a8c1d7cf3dfe..f07cf2d402e0 100644 --- a/maths/tanh.py +++ b/maths/tanh.py @@ -38,4 +38,3 @@ def tanh_func(vector: np.array) -> np.array: if __name__ == '__main__': doctest.testmod() - From a6b5d0547e4be8337f1ddabc090a60a2c213fa4f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Apr 2023 12:20:11 +0000 Subject: [PATCH 06/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/tanh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/tanh.py b/maths/tanh.py index f07cf2d402e0..2ce383e53642 100644 --- a/maths/tanh.py +++ b/maths/tanh.py @@ -36,5 +36,5 @@ def tanh_func(vector: np.array) -> np.array: return (2 / (1 + exp_vector)) - 1 -if __name__ == '__main__': +if __name__ == "__main__": doctest.testmod() From 2827b159d166306a68823ec7fadfea77b0f11e36 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sun, 23 Apr 2023 18:32:08 +0530 Subject: [PATCH 07/42] tanh function added --- maths/tanh.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/maths/tanh.py b/maths/tanh.py index f07cf2d402e0..cd28a59903fd 100644 --- a/maths/tanh.py +++ b/maths/tanh.py @@ -1,18 +1,18 @@ """ -This script demonstrates the implementation of the tangent hyperbolic or tanh function. +This script demonstrates the implementation of the tangent hyperbolic +or tanh function. -The function takes a vector of K real numbers as input and then (e^x - e^(-x))/(e^x + e^(-x)). -After through tanh, the element of the vector mostly -1 between 1. +The function takes a vector of K real numbers as input and +then (e^x - e^(-x))/(e^x + e^(-x)). After through tanh, the +element of the vector mostly -1 between 1. Script inspired from its corresponding Wikipedia article https://en.wikipedia.org/wiki/Activation_function """ -import numpy import numpy as np -import doctest -def tanh_func(vector: np.array) -> np.array: +def tangent_hyperbolic(vector: np.array) -> np.array: """ Implements the tanh function @@ -26,7 +26,7 @@ def tanh_func(vector: np.array) -> np.array: mathematically (e^x - e^(-x))/(e^x + e^(-x)) can be written as (2/(1+e^(-2x))-1 Examples: - >>> tanh_func(np.array([1, 5, 6, 113, 13, 16, -5.23])) + >>> tangent_hyperbolic(np.array([1,5,6,113,13,16,-5.23])) array([ 0.76159416, 0.9999092 , 0.99998771, 1. , 1. , 1. , -0.99994268]) @@ -36,5 +36,7 @@ def tanh_func(vector: np.array) -> np.array: return (2 / (1 + exp_vector)) - 1 -if __name__ == '__main__': - doctest.testmod() +if __name__ == "__main__": + import doctest + + doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE) From 9114b527a1b530323a80cc33106c15970b968d5e Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sun, 23 Apr 2023 19:17:44 +0530 Subject: [PATCH 08/42] tanh function added --- maths/tanh.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/maths/tanh.py b/maths/tanh.py index cd28a59903fd..7330f378373e 100644 --- a/maths/tanh.py +++ b/maths/tanh.py @@ -26,9 +26,8 @@ def tangent_hyperbolic(vector: np.array) -> np.array: mathematically (e^x - e^(-x))/(e^x + e^(-x)) can be written as (2/(1+e^(-2x))-1 Examples: - >>> tangent_hyperbolic(np.array([1,5,6,113,13,16,-5.23])) - array([ 0.76159416, 0.9999092 , 0.99998771, 1. , 1. , - 1. , -0.99994268]) + >>> tangent_hyperbolic(np.array([1,5,6,-0.67])) + array([ 0.76159416, 0.9999092 , 0.99998771, -0.58497988]) """ @@ -39,4 +38,4 @@ def tangent_hyperbolic(vector: np.array) -> np.array: if __name__ == "__main__": import doctest - doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE) + doctest.testmod() From a4ca89775c25680d5e802aa8c3803e583cde4791 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Mon, 24 Apr 2023 20:30:04 +0530 Subject: [PATCH 09/42] tanh function is added --- maths/tanh.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/maths/tanh.py b/maths/tanh.py index 7330f378373e..d6233da639ec 100644 --- a/maths/tanh.py +++ b/maths/tanh.py @@ -29,6 +29,9 @@ def tangent_hyperbolic(vector: np.array) -> np.array: >>> tangent_hyperbolic(np.array([1,5,6,-0.67])) array([ 0.76159416, 0.9999092 , 0.99998771, -0.58497988]) + >>> tangent_hyperbolic(np.array([8,10,2,-0.98,13])) + array([ 0.99999977, 1. , 0.96402758, -0.7530659 , 1. ]) + """ exp_vector = np.exp(-2 * vector) From e0495023d63bef348bb2774afbf3b54418d55da8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 25 Apr 2023 18:01:07 +0200 Subject: [PATCH 10/42] Apply suggestions from code review --- maths/tanh.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/maths/tanh.py b/maths/tanh.py index d6233da639ec..ddab3e1ab717 100644 --- a/maths/tanh.py +++ b/maths/tanh.py @@ -20,8 +20,7 @@ def tangent_hyperbolic(vector: np.array) -> np.array: vector: np.array Returns: - tanh (np.array): The input numpy array after applying - tanh. + tanh (np.array): The input numpy array after applying tanh. mathematically (e^x - e^(-x))/(e^x + e^(-x)) can be written as (2/(1+e^(-2x))-1 @@ -34,8 +33,7 @@ def tangent_hyperbolic(vector: np.array) -> np.array: """ - exp_vector = np.exp(-2 * vector) - return (2 / (1 + exp_vector)) - 1 + return (2 / (1 + np.exp(-2 * vector))) - 1 if __name__ == "__main__": From 66cc81a30592a5734d9d3d767320d014df933926 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Thu, 27 Apr 2023 12:00:39 +0530 Subject: [PATCH 11/42] ELU activation function is added --- maths/elu_activation.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 maths/elu_activation.py diff --git a/maths/elu_activation.py b/maths/elu_activation.py new file mode 100644 index 000000000000..441c3cf4ee43 --- /dev/null +++ b/maths/elu_activation.py @@ -0,0 +1,39 @@ +""" +Implements the Exponential Linear Unit or ELU function. + +The function takes a vector of K real numbers and a real number alpha as +input and then applies the ELU function to each element of the vector. + +Script inspired from its corresponding Wikipedia article +https://en.wikipedia.org/wiki/Rectifier_(neural_networks) +""" + +import numpy as np + + +def elu_activation(vector: np.array, alpha: float) -> np.array: + """ + Implements the ELU activation function. + Parameters: + vector: np.array + alpha: float + return: + elu (np.array): The input numpy array after applying elu. + + Mathematically, f(x) = x, x>0 else (alpha * (e^x -1)), x<=0, alpha >=0 + + Examples: + >>> elu_activation(vector=np.array([2.3,0.6,-2,-3.8,9]), alpha=0.3) + array([ 2.3 , 0.6 , -0.25939942, -0.29328877, 9. ]) + + >>> elu_activation(vector=np.array([-9.2,-0.3,-2.45,0.45]), alpha=0.067) + array([-0.06699323, -0.01736518, -0.06121833, 0.45 ]) + + """ + return np.where(vector > 0, vector, (alpha * (np.exp(vector) - 1))) + + +if __name__ == '__main__': + import doctest + + doctest.testmod() From d40c74b379098b9d3d280b03c6af2338abbc079b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 27 Apr 2023 06:35:23 +0000 Subject: [PATCH 12/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/elu_activation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/elu_activation.py b/maths/elu_activation.py index 441c3cf4ee43..cc1631551b54 100644 --- a/maths/elu_activation.py +++ b/maths/elu_activation.py @@ -33,7 +33,7 @@ def elu_activation(vector: np.array, alpha: float) -> np.array: return np.where(vector > 0, vector, (alpha * (np.exp(vector) - 1))) -if __name__ == '__main__': +if __name__ == "__main__": import doctest doctest.testmod() From 5453434e967e03ba54a1537d06099683ddddb35f Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Fri, 28 Apr 2023 09:57:32 +0530 Subject: [PATCH 13/42] elu activation is added --- maths/elu_activation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/elu_activation.py b/maths/elu_activation.py index cc1631551b54..2f5ded173f0a 100644 --- a/maths/elu_activation.py +++ b/maths/elu_activation.py @@ -9,9 +9,10 @@ """ import numpy as np +from typing import TYPE_CHECKING -def elu_activation(vector: np.array, alpha: float) -> np.array: +def elu_activation(vector: np.ndarray, alpha: float) -> np.ndarray: """ Implements the ELU activation function. Parameters: From 0d1546eb757d438ab88b386eb64779ff7e897ad2 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Fri, 28 Apr 2023 10:03:31 +0530 Subject: [PATCH 14/42] ELU activation is added --- maths/elu_activation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/elu_activation.py b/maths/elu_activation.py index 2f5ded173f0a..d4b5e71b9368 100644 --- a/maths/elu_activation.py +++ b/maths/elu_activation.py @@ -9,7 +9,6 @@ """ import numpy as np -from typing import TYPE_CHECKING def elu_activation(vector: np.ndarray, alpha: float) -> np.ndarray: From 112611aad41ca676bfabfc0019e21ecd3769ccdd Mon Sep 17 00:00:00 2001 From: Dipankar Mitra <50228537+Mitra-babu@users.noreply.github.com> Date: Fri, 28 Apr 2023 19:37:26 +0530 Subject: [PATCH 15/42] Update maths/elu_activation.py Co-authored-by: Christian Clauss --- maths/elu_activation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/elu_activation.py b/maths/elu_activation.py index d4b5e71b9368..4e9b96f59bec 100644 --- a/maths/elu_activation.py +++ b/maths/elu_activation.py @@ -11,7 +11,7 @@ import numpy as np -def elu_activation(vector: np.ndarray, alpha: float) -> np.ndarray: +def exponential_linear_unit(vector: np.ndarray, alpha: float) -> np.ndarray: """ Implements the ELU activation function. Parameters: From dad8cf18b661d7b7d0e84b3dc623960c75b6628b Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Fri, 28 Apr 2023 19:50:26 +0530 Subject: [PATCH 16/42] Exponential_linear_unit activation is added --- .../activation_functions/exponential_linear_unit.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename maths/elu_activation.py => neural_network/activation_functions/exponential_linear_unit.py (77%) diff --git a/maths/elu_activation.py b/neural_network/activation_functions/exponential_linear_unit.py similarity index 77% rename from maths/elu_activation.py rename to neural_network/activation_functions/exponential_linear_unit.py index 4e9b96f59bec..71ba07cfe773 100644 --- a/maths/elu_activation.py +++ b/neural_network/activation_functions/exponential_linear_unit.py @@ -15,18 +15,18 @@ def exponential_linear_unit(vector: np.ndarray, alpha: float) -> np.ndarray: """ Implements the ELU activation function. Parameters: - vector: np.array - alpha: float + vector: the array containing input of elu activation + alpha: hyper-parameter return: elu (np.array): The input numpy array after applying elu. Mathematically, f(x) = x, x>0 else (alpha * (e^x -1)), x<=0, alpha >=0 Examples: - >>> elu_activation(vector=np.array([2.3,0.6,-2,-3.8,9]), alpha=0.3) + >>> exponential_linear_unit(vector=np.array([2.3,0.6,-2,-3.8,9]), alpha=0.3) array([ 2.3 , 0.6 , -0.25939942, -0.29328877, 9. ]) - >>> elu_activation(vector=np.array([-9.2,-0.3,-2.45,0.45]), alpha=0.067) + >>> exponential_linear_unit(vector=np.array([-9.2,-0.3,-2.45,0.45]), alpha=0.067) array([-0.06699323, -0.01736518, -0.06121833, 0.45 ]) """ From 34be18cadf2193302c2d3bf4c7440f8fa3dbcbd6 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Fri, 28 Apr 2023 19:59:28 +0530 Subject: [PATCH 17/42] Exponential_linear_unit activation is added --- .../activation_functions/exponential_linear_unit.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/neural_network/activation_functions/exponential_linear_unit.py b/neural_network/activation_functions/exponential_linear_unit.py index 71ba07cfe773..7a3cf1d84e71 100644 --- a/neural_network/activation_functions/exponential_linear_unit.py +++ b/neural_network/activation_functions/exponential_linear_unit.py @@ -23,11 +23,12 @@ def exponential_linear_unit(vector: np.ndarray, alpha: float) -> np.ndarray: Mathematically, f(x) = x, x>0 else (alpha * (e^x -1)), x<=0, alpha >=0 Examples: - >>> exponential_linear_unit(vector=np.array([2.3,0.6,-2,-3.8,9]), alpha=0.3) - array([ 2.3 , 0.6 , -0.25939942, -0.29328877, 9. ]) + >>> exponential_linear_unit(vector=np.array([2.3,0.6,-2,-3.8]), alpha=0.3) + array([ 2.3 , 0.6 , -0.25939942, -0.29328877]) + + >>> exponential_linear_unit(vector=np.array([-9.2,-0.3,0.45,-4.56]), alpha=0.067) + array([-0.06699323, -0.01736518, 0.45 , -0.06629904]) - >>> exponential_linear_unit(vector=np.array([-9.2,-0.3,-2.45,0.45]), alpha=0.067) - array([-0.06699323, -0.01736518, -0.06121833, 0.45 ]) """ return np.where(vector > 0, vector, (alpha * (np.exp(vector) - 1))) From aae8727d1cfd568d0d3cfcc5f852df2c0f3250be Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Tue, 2 May 2023 21:23:38 +0530 Subject: [PATCH 18/42] SiLU activation is added --- .../sigmoid_linear_unit.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 neural_network/activation_functions/sigmoid_linear_unit.py diff --git a/neural_network/activation_functions/sigmoid_linear_unit.py b/neural_network/activation_functions/sigmoid_linear_unit.py new file mode 100644 index 000000000000..f69eb11598b8 --- /dev/null +++ b/neural_network/activation_functions/sigmoid_linear_unit.py @@ -0,0 +1,40 @@ +""" +Implements the Sigmoid Linear Unit or SiLU function +also known as Swiss functions. + +The function takes a vector of K real numbers and then +applies the SiLU function to each element of the vector. + +Script inspired from its corresponding Wikipedia article +https://en.wikipedia.org/wiki/Rectifier_(neural_networks) +https://en.wikipedia.org/wiki/Swish_function +""" + +import numpy as np + + +def sigmoid_linear_unit(vector: np.ndarray) -> np.ndarray: + """ + Implements the SiLU activation function. + Parameters: + vector: the array containing input of SiLU activation + return: + The input numpy array after applying SiLU. + + Mathematically, f(x) = x * 1/1+e^(-x) + + Examples: + >>> sigmoid_linear_unit(vector=np.array([2.3,0.6,-2,-3.8])) + array([ 2.09041719, 0.38739378, -0.23840584, -0.08314883]) + + >>> sigmoid_linear_unit(vector=np.array([-9.2,-0.3,0.45,-4.56])) + array([-0.00092947, -0.12766724, 0.27478766, -0.04721304]) + + """ + return vector / (1 + np.exp(-vector)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 28da38a385cceec25d63a52b49c61e2a790b09a9 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Tue, 2 May 2023 21:26:56 +0530 Subject: [PATCH 19/42] SiLU activation is added --- neural_network/activation_functions/sigmoid_linear_unit.py | 1 - 1 file changed, 1 deletion(-) diff --git a/neural_network/activation_functions/sigmoid_linear_unit.py b/neural_network/activation_functions/sigmoid_linear_unit.py index f69eb11598b8..4e6a154fd7f4 100644 --- a/neural_network/activation_functions/sigmoid_linear_unit.py +++ b/neural_network/activation_functions/sigmoid_linear_unit.py @@ -7,7 +7,6 @@ Script inspired from its corresponding Wikipedia article https://en.wikipedia.org/wiki/Rectifier_(neural_networks) -https://en.wikipedia.org/wiki/Swish_function """ import numpy as np From fcab387a8fc2358778769218937576f7f0aed6d0 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Mon, 15 May 2023 01:24:10 +0530 Subject: [PATCH 20/42] mish added --- .../activation_functions/mish_activation.py | 48 +++++++++++++++++++ .../sigmoid_linear_unit.py | 39 --------------- 2 files changed, 48 insertions(+), 39 deletions(-) create mode 100644 neural_network/activation_functions/mish_activation.py delete mode 100644 neural_network/activation_functions/sigmoid_linear_unit.py diff --git a/neural_network/activation_functions/mish_activation.py b/neural_network/activation_functions/mish_activation.py new file mode 100644 index 000000000000..9f1688834be8 --- /dev/null +++ b/neural_network/activation_functions/mish_activation.py @@ -0,0 +1,48 @@ +""" +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 + + +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 + + Examples: + >>> mish_activation(np.array([1,5,6,-0.67])) + array([ 0.86509839, 8.99955208, 10.99992663, -1.93211787]) + + + >>> mish_activation(np.array([8,2,-0.98,13])) + array([14.9999982 , 2.94395896, -2.28214659, 25. ]) + + + """ + soft_plus = np.log(np.exp(vector) + 1) + return vector * (2 / (1 + np.exp(-2 * soft_plus))) - 1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/neural_network/activation_functions/sigmoid_linear_unit.py b/neural_network/activation_functions/sigmoid_linear_unit.py deleted file mode 100644 index 4e6a154fd7f4..000000000000 --- a/neural_network/activation_functions/sigmoid_linear_unit.py +++ /dev/null @@ -1,39 +0,0 @@ -""" -Implements the Sigmoid Linear Unit or SiLU function -also known as Swiss functions. - -The function takes a vector of K real numbers and then -applies the SiLU function to each element of the vector. - -Script inspired from its corresponding Wikipedia article -https://en.wikipedia.org/wiki/Rectifier_(neural_networks) -""" - -import numpy as np - - -def sigmoid_linear_unit(vector: np.ndarray) -> np.ndarray: - """ - Implements the SiLU activation function. - Parameters: - vector: the array containing input of SiLU activation - return: - The input numpy array after applying SiLU. - - Mathematically, f(x) = x * 1/1+e^(-x) - - Examples: - >>> sigmoid_linear_unit(vector=np.array([2.3,0.6,-2,-3.8])) - array([ 2.09041719, 0.38739378, -0.23840584, -0.08314883]) - - >>> sigmoid_linear_unit(vector=np.array([-9.2,-0.3,0.45,-4.56])) - array([-0.00092947, -0.12766724, 0.27478766, -0.04721304]) - - """ - return vector / (1 + np.exp(-vector)) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From b1731ec29a3ceb94e5c972168e1c898e68548847 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Tue, 16 May 2023 21:01:16 +0530 Subject: [PATCH 21/42] mish activation is added --- neural_network/activation_functions/mish_activation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/activation_functions/mish_activation.py b/neural_network/activation_functions/mish_activation.py index 9f1688834be8..8948b43228e5 100644 --- a/neural_network/activation_functions/mish_activation.py +++ b/neural_network/activation_functions/mish_activation.py @@ -2,7 +2,7 @@ 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. +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) From 9a5cb3069460db50550135c7f27ab0f4086af87a Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sat, 20 May 2023 22:43:57 +0530 Subject: [PATCH 22/42] inter_quartile_range function is added --- maths/inter_quartile_range.py | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 maths/inter_quartile_range.py diff --git a/maths/inter_quartile_range.py b/maths/inter_quartile_range.py new file mode 100644 index 000000000000..08a7961bdc28 --- /dev/null +++ b/maths/inter_quartile_range.py @@ -0,0 +1,60 @@ +""" +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() + if length % 2: + q1 = find_median(x[0: length // 2]) + q3 = find_median(x[(length // 2) + 1: length]) + else: + q1 = find_median(x[0: length // 2]) + q3 = find_median(x[length // 2: length]) + return q3 - q1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 3296963721fa7199e3696fdc35824e2c778ff290 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 May 2023 17:15:56 +0000 Subject: [PATCH 23/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/inter_quartile_range.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/inter_quartile_range.py b/maths/inter_quartile_range.py index 08a7961bdc28..fb950613ddd1 100644 --- a/maths/inter_quartile_range.py +++ b/maths/inter_quartile_range.py @@ -46,11 +46,11 @@ def inter_quartile_range(x: List[float]) -> float: raise ValueError x.sort() if length % 2: - q1 = find_median(x[0: length // 2]) - q3 = find_median(x[(length // 2) + 1: length]) + q1 = find_median(x[0 : length // 2]) + q3 = find_median(x[(length // 2) + 1 : length]) else: - q1 = find_median(x[0: length // 2]) - q3 = find_median(x[length // 2: length]) + q1 = find_median(x[0 : length // 2]) + q3 = find_median(x[length // 2 : length]) return q3 - q1 From 95caae23e682d7ad9c640a76c578ccb6fbbc6114 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sat, 20 May 2023 22:51:03 +0530 Subject: [PATCH 24/42] Mish activation function is added --- neural_network/activation_functions/mish_activation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/activation_functions/mish_activation.py b/neural_network/activation_functions/mish_activation.py index 8948b43228e5..648232786dd7 100644 --- a/neural_network/activation_functions/mish_activation.py +++ b/neural_network/activation_functions/mish_activation.py @@ -7,7 +7,7 @@ Script inspired from its corresponding Wikipedia article https://en.wikipedia.org/wiki/Rectifier_(neural_networks) -The proposed paper link is provided below. +The proposed paper link is provided below- https://arxiv.org/abs/1908.08681 """ From 784199a862e933cc67565947e77f5e6d0c7ba7af Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sat, 20 May 2023 22:54:24 +0530 Subject: [PATCH 25/42] Mish action is added --- maths/inter_quartile_range.py | 60 ------------------- .../activation_functions/mish_activation.py | 2 +- 2 files changed, 1 insertion(+), 61 deletions(-) delete mode 100644 maths/inter_quartile_range.py diff --git a/maths/inter_quartile_range.py b/maths/inter_quartile_range.py deleted file mode 100644 index fb950613ddd1..000000000000 --- a/maths/inter_quartile_range.py +++ /dev/null @@ -1,60 +0,0 @@ -""" -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() - if length % 2: - q1 = find_median(x[0 : length // 2]) - q3 = find_median(x[(length // 2) + 1 : length]) - else: - q1 = find_median(x[0 : length // 2]) - q3 = find_median(x[length // 2 : length]) - return q3 - q1 - - -if __name__ == "__main__": - import doctest - - doctest.testmod() diff --git a/neural_network/activation_functions/mish_activation.py b/neural_network/activation_functions/mish_activation.py index 648232786dd7..8948b43228e5 100644 --- a/neural_network/activation_functions/mish_activation.py +++ b/neural_network/activation_functions/mish_activation.py @@ -7,7 +7,7 @@ Script inspired from its corresponding Wikipedia article https://en.wikipedia.org/wiki/Rectifier_(neural_networks) -The proposed paper link is provided below- +The proposed paper link is provided below. https://arxiv.org/abs/1908.08681 """ From c91bc2a5281b32ee67526d02b70b58b435ac8b99 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Tue, 20 Jun 2023 19:38:29 +0530 Subject: [PATCH 26/42] mish activation added --- neural_network/activation_functions/mish_activation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neural_network/activation_functions/mish_activation.py b/neural_network/activation_functions/mish_activation.py index 8948b43228e5..8dfd28f01054 100644 --- a/neural_network/activation_functions/mish_activation.py +++ b/neural_network/activation_functions/mish_activation.py @@ -12,6 +12,7 @@ """ import numpy as np +from maths.tanh import tangent_hyperbolic as tanh def mish_activation(vector: np.ndarray) -> np.ndarray: @@ -39,7 +40,7 @@ def mish_activation(vector: np.ndarray) -> np.ndarray: """ soft_plus = np.log(np.exp(vector) + 1) - return vector * (2 / (1 + np.exp(-2 * soft_plus))) - 1 + return vector * tanh(soft_plus) if __name__ == "__main__": From 44a5ff837a58d95e8ac453f8f5ecd004dcd67b7a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 Jun 2023 14:09:49 +0000 Subject: [PATCH 27/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pyproject.toml | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 48c3fbd4009d..94a773eaa90c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,21 +1,3 @@ -[tool.pytest.ini_options] -markers = [ - "mat_ops: mark a test as utilizing matrix operations.", -] -addopts = [ - "--durations=10", - "--doctest-modules", - "--showlocals", -] - -[tool.coverage.report] -omit = [".env/*"] -sort = "Cover" - -[tool.codespell] -ignore-words-list = "3rt,ans,crate,damon,fo,followings,hist,iff,kwanza,mater,secant,som,sur,tim,zar" -skip = "./.*,*.json,ciphers/prehistoric_men.txt,project_euler/problem_022/p022_names.txt,pyproject.toml,strings/dictionary.txt,strings/words.txt" - [tool.ruff] ignore = [ # `ruff rule S101` for a description of that rule "B904", # B904: Within an `except` clause, raise exceptions with `raise ... from err` @@ -68,3 +50,21 @@ max-args = 10 # default: 5 max-branches = 20 # default: 12 max-returns = 8 # default: 6 max-statements = 88 # default: 50 + +[tool.pytest.ini_options] +markers = [ + "mat_ops: mark a test as utilizing matrix operations.", +] +addopts = [ + "--durations=10", + "--doctest-modules", + "--showlocals", +] + +[tool.coverage.report] +omit = [".env/*"] +sort = "Cover" + +[tool.codespell] +ignore-words-list = "3rt,ans,crate,damon,fo,followings,hist,iff,kwanza,mater,secant,som,sur,tim,zar" +skip = "./.*,*.json,ciphers/prehistoric_men.txt,project_euler/problem_022/p022_names.txt,pyproject.toml,strings/dictionary.txt,strings/words.txt" From 88f05ec5a42787a05eac74b2d86fa7e745d995aa Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Tue, 20 Jun 2023 20:05:39 +0530 Subject: [PATCH 28/42] mish activation added --- neural_network/activation_functions/mish_activation.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/neural_network/activation_functions/mish_activation.py b/neural_network/activation_functions/mish_activation.py index 8dfd28f01054..59a3951635bc 100644 --- a/neural_network/activation_functions/mish_activation.py +++ b/neural_network/activation_functions/mish_activation.py @@ -29,15 +29,6 @@ def mish_activation(vector: np.ndarray) -> np.ndarray: 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 - Examples: - >>> mish_activation(np.array([1,5,6,-0.67])) - array([ 0.86509839, 8.99955208, 10.99992663, -1.93211787]) - - - >>> mish_activation(np.array([8,2,-0.98,13])) - array([14.9999982 , 2.94395896, -2.28214659, 25. ]) - - """ soft_plus = np.log(np.exp(vector) + 1) return vector * tanh(soft_plus) From 30bd9c2c61c5b7728a8d88c712f5b1b184f20363 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 Jun 2023 14:37:16 +0000 Subject: [PATCH 29/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../activation_functions/mish_activation.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/neural_network/activation_functions/mish_activation.py b/neural_network/activation_functions/mish_activation.py index 59a3951635bc..c292f76f1a8b 100644 --- a/neural_network/activation_functions/mish_activation.py +++ b/neural_network/activation_functions/mish_activation.py @@ -17,17 +17,17 @@ def mish_activation(vector: np.ndarray) -> np.ndarray: """ - Implements the Mish function + Implements the Mish function - Parameters: - vector: np.array + Parameters: + vector: np.array - Returns: - Mish (np.array): The input numpy array after applying tanh. + 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 + 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) From 1355f9daa572f64b1f84673fff7dae7a974fe5da Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sun, 2 Jul 2023 20:08:30 +0530 Subject: [PATCH 30/42] inter quartile range (IQR) function is added --- maths/inter_quartile_range.py | 61 +++++++++++++++++++ .../activation_functions/mish_activation.py | 40 ------------ 2 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 maths/inter_quartile_range.py delete mode 100644 neural_network/activation_functions/mish_activation.py diff --git a/maths/inter_quartile_range.py b/maths/inter_quartile_range.py new file mode 100644 index 000000000000..91a5eb330c10 --- /dev/null +++ b/maths/inter_quartile_range.py @@ -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() diff --git a/neural_network/activation_functions/mish_activation.py b/neural_network/activation_functions/mish_activation.py deleted file mode 100644 index c292f76f1a8b..000000000000 --- a/neural_network/activation_functions/mish_activation.py +++ /dev/null @@ -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() From 483d59adec16477b8e0e6d3f952b3d78edde7e32 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 2 Jul 2023 14:41:28 +0000 Subject: [PATCH 31/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/inter_quartile_range.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/inter_quartile_range.py b/maths/inter_quartile_range.py index 91a5eb330c10..89bb449ed6eb 100644 --- a/maths/inter_quartile_range.py +++ b/maths/inter_quartile_range.py @@ -47,11 +47,11 @@ def inter_quartile_range(x: List[float]) -> float: if length == 0: raise ValueError x.sort() - q1 = find_median(x[0: length // 2]) + q1 = find_median(x[0 : length // 2]) if length % 2: - q3 = find_median(x[(length // 2) + 1: length]) + q3 = find_median(x[(length // 2) + 1 : length]) else: - q3 = find_median(x[length // 2: length]) + q3 = find_median(x[length // 2 : length]) return q3 - q1 From aa5dccb6b80dc035b9d7ed26271237bafb2cb35a Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sun, 2 Jul 2023 20:23:58 +0530 Subject: [PATCH 32/42] IQR function is added --- maths/inter_quartile_range.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/maths/inter_quartile_range.py b/maths/inter_quartile_range.py index 89bb449ed6eb..c3be9a8674cb 100644 --- a/maths/inter_quartile_range.py +++ b/maths/inter_quartile_range.py @@ -8,18 +8,18 @@ https://en.wikipedia.org/wiki/Interquartile_range """ -from typing import List +import numpy as np -def find_median(x: List[float]) -> float: +def find_median(x: np.array) -> 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]) + >>> find_median(x=np.array([1,2,2,3,4])) 2 - >>> find_median([1,2,2,3,4,4]) + >>> find_median(np.array([1,2,2,3,4,4])) 2.5 @@ -30,28 +30,28 @@ def find_median(x: List[float]) -> float: return float((x[length // 2] + x[(length // 2) - 1]) / 2) -def inter_quartile_range(x: List[float]) -> float: +def inter_quartile_range(x: np.array) -> 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]) + >>> inter_quartile_range(x=np.array([4,1,2,3,2])) 2.0 - >>> inter_quartile_range([25,32,49,21,37,43,27,45,31]) + >>> inter_quartile_range(x=np.array([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]) + q1 = find_median(x[0: length // 2]) if length % 2: - q3 = find_median(x[(length // 2) + 1 : length]) + q3 = find_median(x[(length // 2) + 1: length]) else: - q3 = find_median(x[length // 2 : length]) + q3 = find_median(x[length // 2: length]) return q3 - q1 From 2b5d7c6095408cc11b62eb8836672852d48f19fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 2 Jul 2023 14:54:55 +0000 Subject: [PATCH 33/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/inter_quartile_range.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/inter_quartile_range.py b/maths/inter_quartile_range.py index c3be9a8674cb..421f0d482cc5 100644 --- a/maths/inter_quartile_range.py +++ b/maths/inter_quartile_range.py @@ -47,11 +47,11 @@ def inter_quartile_range(x: np.array) -> float: if length == 0: raise ValueError x.sort() - q1 = find_median(x[0: length // 2]) + q1 = find_median(x[0 : length // 2]) if length % 2: - q3 = find_median(x[(length // 2) + 1: length]) + q3 = find_median(x[(length // 2) + 1 : length]) else: - q3 = find_median(x[length // 2: length]) + q3 = find_median(x[length // 2 : length]) return q3 - q1 From ee3247bd1ca556cbbe3253283dd24415b421de87 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Mon, 3 Jul 2023 20:39:43 +0530 Subject: [PATCH 34/42] code optimized in IQR function --- maths/inter_quartile_range.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/maths/inter_quartile_range.py b/maths/inter_quartile_range.py index 421f0d482cc5..3ef4a2694021 100644 --- a/maths/inter_quartile_range.py +++ b/maths/inter_quartile_range.py @@ -47,11 +47,9 @@ def inter_quartile_range(x: np.array) -> float: 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]) + q1 = find_median(x[0: length // 2]) + half_length = (length // 2) + 1 if length % 2 else length // 2 + q3 = find_median(x[half_length:length]) return q3 - q1 From 436a48324ec2f96b9216a6e418328a0d0b841e26 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Jul 2023 15:11:41 +0000 Subject: [PATCH 35/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/inter_quartile_range.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/inter_quartile_range.py b/maths/inter_quartile_range.py index 3ef4a2694021..8dd33879dcde 100644 --- a/maths/inter_quartile_range.py +++ b/maths/inter_quartile_range.py @@ -47,7 +47,7 @@ def inter_quartile_range(x: np.array) -> float: if length == 0: raise ValueError x.sort() - q1 = find_median(x[0: length // 2]) + q1 = find_median(x[0 : length // 2]) half_length = (length // 2) + 1 if length % 2 else length // 2 q3 = find_median(x[half_length:length]) return q3 - q1 From ae37fc40be7dbe53901a6c1450d98ce9f2049fdc Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Wed, 2 Aug 2023 00:23:54 +0530 Subject: [PATCH 36/42] interquartile_range function is added --- ...inter_quartile_range.py => interquartile_range.py} | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) rename maths/{inter_quartile_range.py => interquartile_range.py} (79%) diff --git a/maths/inter_quartile_range.py b/maths/interquartile_range.py similarity index 79% rename from maths/inter_quartile_range.py rename to maths/interquartile_range.py index 8dd33879dcde..6f8e0a6ea09a 100644 --- a/maths/inter_quartile_range.py +++ b/maths/interquartile_range.py @@ -30,24 +30,25 @@ def find_median(x: np.array) -> float: return float((x[length // 2] + x[(length // 2) - 1]) / 2) -def inter_quartile_range(x: np.array) -> float: +def interquartile_range(x: np.array) -> 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(x=np.array([4,1,2,3,2])) + >>> interquartile_range(x=np.array([4,1,2,3,2])) 2.0 - >>> inter_quartile_range(x=np.array([25,32,49,21,37,43,27,45,31])) + + >>> interquartile_range(x=np.array([25,32,49,21,37,43,27,45,31])) 18.0 """ length = len(x) if length == 0: - raise ValueError + raise ValueError("The list is empty. Provide a non-empty list.") x.sort() - q1 = find_median(x[0 : length // 2]) + q1 = find_median(x[0: length // 2]) half_length = (length // 2) + 1 if length % 2 else length // 2 q3 = find_median(x[half_length:length]) return q3 - q1 From 14a3fb419d395a254e3f67e2f1bda7d1dbe480c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Aug 2023 18:56:54 +0000 Subject: [PATCH 37/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/interquartile_range.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/interquartile_range.py b/maths/interquartile_range.py index 6f8e0a6ea09a..03af90849943 100644 --- a/maths/interquartile_range.py +++ b/maths/interquartile_range.py @@ -48,7 +48,7 @@ def interquartile_range(x: np.array) -> float: if length == 0: raise ValueError("The list is empty. Provide a non-empty list.") x.sort() - q1 = find_median(x[0: length // 2]) + q1 = find_median(x[0 : length // 2]) half_length = (length // 2) + 1 if length % 2 else length // 2 q3 = find_median(x[half_length:length]) return q3 - q1 From da1c06313ed2d97f39a803667abe4d59ac5da411 Mon Sep 17 00:00:00 2001 From: Dipankar Mitra <50228537+Mitra-babu@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:14:40 +0530 Subject: [PATCH 38/42] Update maths/interquartile_range.py Co-authored-by: Christian Clauss --- maths/interquartile_range.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/maths/interquartile_range.py b/maths/interquartile_range.py index 03af90849943..b54ef3704bc5 100644 --- a/maths/interquartile_range.py +++ b/maths/interquartile_range.py @@ -48,8 +48,9 @@ def interquartile_range(x: np.array) -> float: if length == 0: raise ValueError("The list is empty. Provide a non-empty list.") x.sort() - q1 = find_median(x[0 : length // 2]) - half_length = (length // 2) + 1 if length % 2 else length // 2 + div, mod = divmod(length, 2) + q1 = find_median(x[:div]) + half_length = sum((div, mod)) q3 = find_median(x[half_length:length]) return q3 - q1 From 05644a075040e62932fe18a72692c2d98379417c Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sun, 6 Aug 2023 23:38:17 +0530 Subject: [PATCH 39/42] Changes on interquartile_range --- maths/interquartile_range.py | 44 +++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/maths/interquartile_range.py b/maths/interquartile_range.py index b54ef3704bc5..8ef59b267bf1 100644 --- a/maths/interquartile_range.py +++ b/maths/interquartile_range.py @@ -7,51 +7,63 @@ Script inspired from its corresponding Wikipedia article https://en.wikipedia.org/wiki/Interquartile_range """ +from __future__ import annotations import numpy as np -def find_median(x: np.array) -> float: +def find_median(nums: list[int | float]) -> float: """ This is the implementation of median. - :param x: The list of numeric values + :param nums: The list of numeric nums :return: Median of the list - >>> find_median(x=np.array([1,2,2,3,4])) + >>> find_median(nums=([1,2,2,3,4])) 2 - >>> find_median(np.array([1,2,2,3,4,4])) + >>> find_median(nums=([1,2,2,3,4,4])) 2.5 """ - length = len(x) + length = len(nums) if length % 2: - return x[length // 2] - return float((x[length // 2] + x[(length // 2) - 1]) / 2) + return nums[length // 2] + return float((nums[length // 2] + nums[(length // 2) - 1]) / 2) -def interquartile_range(x: np.array) -> float: +def interquartile_range(nums: list[int | float]) -> float: """ This is the implementation of inter_quartile range for a list of numeric. - :param x: The list of data point + :param nums: The list of data point :return: Inter_quartile range - >>> interquartile_range(x=np.array([4,1,2,3,2])) + >>> interquartile_range(nums=[4,1,2,3,2]) 2.0 - >>> interquartile_range(x=np.array([25,32,49,21,37,43,27,45,31])) - 18.0 + >>> interquartile_range(nums=[]) + Traceback (most recent call last): + ... + ValueError: The list is empty. Provide a non-empty list. + + >>> interquartile_range(nums = [-2,-7,-10,9,8,4, -67, 45]) + 17.0 + + >>> interquartile_range(nums = [0,0,0,0,0]) + 0.0 + + + """ - length = len(x) + length = len(nums) if length == 0: raise ValueError("The list is empty. Provide a non-empty list.") - x.sort() + nums.sort() div, mod = divmod(length, 2) - q1 = find_median(x[:div]) + q1 = find_median(nums[:div]) half_length = sum((div, mod)) - q3 = find_median(x[half_length:length]) + q3 = find_median(nums[half_length:length]) return q3 - q1 From 0e72de66a51b8e629a80c961b29206b317662ef5 Mon Sep 17 00:00:00 2001 From: Mitra-babu Date: Sun, 6 Aug 2023 23:40:35 +0530 Subject: [PATCH 40/42] numpy removed from interquartile_range --- maths/interquartile_range.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/maths/interquartile_range.py b/maths/interquartile_range.py index 8ef59b267bf1..979a58a8d370 100644 --- a/maths/interquartile_range.py +++ b/maths/interquartile_range.py @@ -9,8 +9,6 @@ """ from __future__ import annotations -import numpy as np - def find_median(nums: list[int | float]) -> float: """ From 9c8c26f467cefc7abcab03f85eb053a52c790442 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 7 Aug 2023 06:46:23 -0400 Subject: [PATCH 41/42] Fixes from code review --- maths/interquartile_range.py | 59 +++++++++++++++++------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/maths/interquartile_range.py b/maths/interquartile_range.py index 979a58a8d370..1d77c75b2420 100644 --- a/maths/interquartile_range.py +++ b/maths/interquartile_range.py @@ -1,10 +1,10 @@ """ -This is the implementation of inter_quartile range (IQR). +An implementation of interquartile range (IQR) which is a measure of statistical +dispersion, which is the spread of the data. -function takes the list of numeric values as input -and return the IQR as output. +The function takes the list of numeric values as input and returns the IQR. -Script inspired from its corresponding Wikipedia article +Script inspired by this Wikipedia article: https://en.wikipedia.org/wiki/Interquartile_range """ from __future__ import annotations @@ -12,52 +12,47 @@ def find_median(nums: list[int | float]) -> float: """ - This is the implementation of median. + 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])) + >>> find_median(nums=([1, 2, 2, 3, 4])) 2 - - >>> find_median(nums=([1,2,2,3,4,4])) + >>> 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 """ - length = len(nums) - if length % 2: - return nums[length // 2] - return float((nums[length // 2] + nums[(length // 2) - 1]) / 2) + 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: """ - This is the implementation of inter_quartile - range for a list of numeric. - :param nums: The list of data point - :return: Inter_quartile range + 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]) + >>> 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. - - >>> interquartile_range(nums = [-2,-7,-10,9,8,4, -67, 45]) - 17.0 - - >>> interquartile_range(nums = [0,0,0,0,0]) - 0.0 - - - """ - length = len(nums) - if length == 0: + 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)) From 90748c86aa3dcb9d9292ed5469426fffc0d90ba0 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 7 Aug 2023 06:51:30 -0400 Subject: [PATCH 42/42] Update interquartile_range.py --- maths/interquartile_range.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/interquartile_range.py b/maths/interquartile_range.py index 1d77c75b2420..d4d72e73ef49 100644 --- a/maths/interquartile_range.py +++ b/maths/interquartile_range.py @@ -27,7 +27,7 @@ def find_median(nums: list[int | float]) -> float: div, mod = divmod(len(nums), 2) if mod: return nums[div] - return float((nums[div] + nums[(div) - 1]) / 2) + return (nums[div] + nums[(div) - 1]) / 2 def interquartile_range(nums: list[int | float]) -> float: