Fix minor typing errors in maths/ (#8959)

* updating DIRECTORY.md

* types(maths): Fix pylance issues in maths

* reset(vsc): Reset settings changes

* Update maths/jaccard_similarity.py

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>

* revert(erosion_operation): Revert erosion_operation

* test(jaccard_similarity): Add doctest to test alternative_union

* types(newton_raphson): Add typehints to func bodies

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
This commit is contained in:
Caeden Perelli-Harris 2023-08-15 22:27:41 +01:00 committed by GitHub
parent 7618a92fee
commit 490e645ed3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 65 additions and 36 deletions

View File

@ -21,6 +21,7 @@ def rgb2gray(rgb: np.array) -> np.array:
def gray2binary(gray: np.array) -> np.array: def gray2binary(gray: np.array) -> np.array:
""" """
Return binary image from gray image Return binary image from gray image
>>> gray2binary(np.array([[127, 255, 0]])) >>> gray2binary(np.array([[127, 255, 0]]))
array([[False, True, False]]) array([[False, True, False]])
>>> gray2binary(np.array([[0]])) >>> gray2binary(np.array([[0]]))

View File

@ -10,12 +10,12 @@ def get_rotation(
) -> np.ndarray: ) -> np.ndarray:
""" """
Get image rotation Get image rotation
:param img: np.array :param img: np.ndarray
:param pt1: 3x2 list :param pt1: 3x2 list
:param pt2: 3x2 list :param pt2: 3x2 list
:param rows: columns image shape :param rows: columns image shape
:param cols: rows image shape :param cols: rows image shape
:return: np.array :return: np.ndarray
""" """
matrix = cv2.getAffineTransform(pt1, pt2) matrix = cv2.getAffineTransform(pt1, pt2)
return cv2.warpAffine(img, matrix, (rows, cols)) return cv2.warpAffine(img, matrix, (rows, cols))

View File

@ -19,7 +19,9 @@ def median(nums: list) -> int | float:
Returns: Returns:
Median. Median.
""" """
sorted_list = sorted(nums) # The sorted function returns list[SupportsRichComparisonT@sorted]
# which does not support `+`
sorted_list: list[int] = sorted(nums)
length = len(sorted_list) length = len(sorted_list)
mid_index = length >> 1 mid_index = length >> 1
return ( return (

View File

@ -5,7 +5,7 @@ import numpy as np
def euler_modified( def euler_modified(
ode_func: Callable, y0: float, x0: float, step_size: float, x_end: float ode_func: Callable, y0: float, x0: float, step_size: float, x_end: float
) -> np.array: ) -> np.ndarray:
""" """
Calculate solution at each step to an ODE using Euler's Modified Method Calculate solution at each step to an ODE using Euler's Modified Method
The Euler Method is straightforward to implement, but can't give accurate solutions. The Euler Method is straightforward to implement, but can't give accurate solutions.

View File

@ -13,7 +13,7 @@ This script is inspired by a corresponding research paper.
import numpy as np import numpy as np
def sigmoid(vector: np.array) -> np.array: def sigmoid(vector: np.ndarray) -> np.ndarray:
""" """
Mathematical function sigmoid takes a vector x of K real numbers as input and Mathematical function sigmoid takes a vector x of K real numbers as input and
returns 1/ (1 + e^-x). returns 1/ (1 + e^-x).
@ -25,7 +25,7 @@ def sigmoid(vector: np.array) -> np.array:
return 1 / (1 + np.exp(-vector)) return 1 / (1 + np.exp(-vector))
def gaussian_error_linear_unit(vector: np.array) -> np.array: def gaussian_error_linear_unit(vector: np.ndarray) -> np.ndarray:
""" """
Implements the Gaussian Error Linear Unit (GELU) function Implements the Gaussian Error Linear Unit (GELU) function

View File

@ -14,7 +14,11 @@ Jaccard similarity is widely used with MinHashing.
""" """
def jaccard_similarity(set_a, set_b, alternative_union=False): def jaccard_similarity(
set_a: set[str] | list[str] | tuple[str],
set_b: set[str] | list[str] | tuple[str],
alternative_union=False,
):
""" """
Finds the jaccard similarity between two sets. Finds the jaccard similarity between two sets.
Essentially, its intersection over union. Essentially, its intersection over union.
@ -37,41 +41,52 @@ def jaccard_similarity(set_a, set_b, alternative_union=False):
>>> set_b = {'c', 'd', 'e', 'f', 'h', 'i'} >>> set_b = {'c', 'd', 'e', 'f', 'h', 'i'}
>>> jaccard_similarity(set_a, set_b) >>> jaccard_similarity(set_a, set_b)
0.375 0.375
>>> jaccard_similarity(set_a, set_a) >>> jaccard_similarity(set_a, set_a)
1.0 1.0
>>> jaccard_similarity(set_a, set_a, True) >>> jaccard_similarity(set_a, set_a, True)
0.5 0.5
>>> set_a = ['a', 'b', 'c', 'd', 'e'] >>> set_a = ['a', 'b', 'c', 'd', 'e']
>>> set_b = ('c', 'd', 'e', 'f', 'h', 'i') >>> set_b = ('c', 'd', 'e', 'f', 'h', 'i')
>>> jaccard_similarity(set_a, set_b) >>> jaccard_similarity(set_a, set_b)
0.375 0.375
>>> set_a = ('c', 'd', 'e', 'f', 'h', 'i')
>>> set_b = ['a', 'b', 'c', 'd', 'e']
>>> jaccard_similarity(set_a, set_b)
0.375
>>> set_a = ('c', 'd', 'e', 'f', 'h', 'i')
>>> set_b = ['a', 'b', 'c', 'd']
>>> jaccard_similarity(set_a, set_b, True)
0.2
>>> set_a = {'a', 'b'}
>>> set_b = ['c', 'd']
>>> jaccard_similarity(set_a, set_b)
Traceback (most recent call last):
...
ValueError: Set a and b must either both be sets or be either a list or a tuple.
""" """
if isinstance(set_a, set) and isinstance(set_b, set): if isinstance(set_a, set) and isinstance(set_b, set):
intersection = len(set_a.intersection(set_b)) intersection_length = len(set_a.intersection(set_b))
if alternative_union: if alternative_union:
union = len(set_a) + len(set_b) union_length = len(set_a) + len(set_b)
else: else:
union = len(set_a.union(set_b)) union_length = len(set_a.union(set_b))
return intersection / union return intersection_length / union_length
if isinstance(set_a, (list, tuple)) and isinstance(set_b, (list, tuple)): elif isinstance(set_a, (list, tuple)) and isinstance(set_b, (list, tuple)):
intersection = [element for element in set_a if element in set_b] intersection = [element for element in set_a if element in set_b]
if alternative_union: if alternative_union:
union = len(set_a) + len(set_b) return len(intersection) / (len(set_a) + len(set_b))
return len(intersection) / union
else: else:
union = set_a + [element for element in set_b if element not in set_a] # Cast set_a to list because tuples cannot be mutated
union = list(set_a) + [element for element in set_b if element not in set_a]
return len(intersection) / len(union) return len(intersection) / len(union)
raise ValueError(
return len(intersection) / len(union) "Set a and b must either both be sets or be either a list or a tuple."
return None )
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -7,10 +7,14 @@
limit is reached or the gradient f'(x[n]) approaches zero. In both cases, exception limit is reached or the gradient f'(x[n]) approaches zero. In both cases, exception
is raised. If iteration limit is reached, try increasing maxiter. is raised. If iteration limit is reached, try increasing maxiter.
""" """
import math as m import math as m
from collections.abc import Callable
DerivativeFunc = Callable[[float], float]
def calc_derivative(f, a, h=0.001): def calc_derivative(f: DerivativeFunc, a: float, h: float = 0.001) -> float:
""" """
Calculates derivative at point a for function f using finite difference Calculates derivative at point a for function f using finite difference
method method
@ -18,7 +22,14 @@ def calc_derivative(f, a, h=0.001):
return (f(a + h) - f(a - h)) / (2 * h) return (f(a + h) - f(a - h)) / (2 * h)
def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6, logsteps=False): def newton_raphson(
f: DerivativeFunc,
x0: float = 0,
maxiter: int = 100,
step: float = 0.0001,
maxerror: float = 1e-6,
logsteps: bool = False,
) -> tuple[float, float, list[float]]:
a = x0 # set the initial guess a = x0 # set the initial guess
steps = [a] steps = [a]
error = abs(f(a)) error = abs(f(a))
@ -36,7 +47,7 @@ def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6, logsteps=Fa
if logsteps: if logsteps:
# If logstep is true, then log intermediate steps # If logstep is true, then log intermediate steps
return a, error, steps return a, error, steps
return a, error return a, error, []
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,7 +1,7 @@
import numpy as np import numpy as np
def qr_householder(a): def qr_householder(a: np.ndarray):
"""Return a QR-decomposition of the matrix A using Householder reflection. """Return a QR-decomposition of the matrix A using Householder reflection.
The QR-decomposition decomposes the matrix A of shape (m, n) into an The QR-decomposition decomposes the matrix A of shape (m, n) into an

View File

@ -11,7 +11,7 @@ https://en.wikipedia.org/wiki/Sigmoid_function
import numpy as np import numpy as np
def sigmoid(vector: np.array) -> np.array: def sigmoid(vector: np.ndarray) -> np.ndarray:
""" """
Implements the sigmoid function Implements the sigmoid function

View File

@ -12,12 +12,12 @@ https://en.wikipedia.org/wiki/Activation_function
import numpy as np import numpy as np
def tangent_hyperbolic(vector: np.array) -> np.array: def tangent_hyperbolic(vector: np.ndarray) -> np.ndarray:
""" """
Implements the tanh function Implements the tanh function
Parameters: Parameters:
vector: np.array vector: np.ndarray
Returns: Returns:
tanh (np.array): The input numpy array after applying tanh. tanh (np.array): The input numpy array after applying tanh.