mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-04 16:57:32 +08:00
Adding doctests into LDA algorithm (#1621)
* Adding doctests into <gaussian_distribution> function * Adding doctests into <y_generator> function * Adding doctests into <calculate_mean> function * Adding doctests into <calculate_probabilities> function * Adding doctests into <calculate_variance> function * Adding doctests into <predict_y_values> function * Adding doctests into <accuracy> function * fixup! Format Python code with psf/black push * Update convex_hull.py * Update convex_hull.py
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
from numbers import Number
|
||||
|
||||
"""
|
||||
The convex hull problem is problem of finding all the vertices of convex polygon, P of
|
||||
a set of points in a plane such that all the points are either on the vertices of P or
|
||||
@ -40,22 +38,11 @@ class Point:
|
||||
>>> Point("pi", "e")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: x and y must be both numeric types but got <class 'str'>, <class 'str'> instead
|
||||
ValueError: could not convert string to float: 'pi'
|
||||
"""
|
||||
|
||||
def __init__(self, x, y):
|
||||
if not (isinstance(x, Number) and isinstance(y, Number)):
|
||||
try:
|
||||
x, y = float(x), float(y)
|
||||
except ValueError as e:
|
||||
e.args = (
|
||||
"x and y must be both numeric types "
|
||||
f"but got {type(x)}, {type(y)} instead"
|
||||
)
|
||||
raise
|
||||
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.x, self.y = float(x), float(y)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.x == other.x and self.y == other.y
|
||||
@ -112,13 +99,7 @@ def _construct_points(list_of_tuples):
|
||||
Examples
|
||||
-------
|
||||
>>> _construct_points([[1, 1], [2, -1], [0.3, 4]])
|
||||
[(1, 1), (2, -1), (0.3, 4)]
|
||||
>>> _construct_points(([1, 1], [2, -1], [0.3, 4]))
|
||||
[(1, 1), (2, -1), (0.3, 4)]
|
||||
>>> _construct_points([(1, 1), (2, -1), (0.3, 4)])
|
||||
[(1, 1), (2, -1), (0.3, 4)]
|
||||
>>> _construct_points([[1, 1], (2, -1), [0.3, 4]])
|
||||
[(1, 1), (2, -1), (0.3, 4)]
|
||||
[(1.0, 1.0), (2.0, -1.0), (0.3, 4.0)]
|
||||
>>> _construct_points([1, 2])
|
||||
Ignoring deformed point 1. All points must have at least 2 coordinates.
|
||||
Ignoring deformed point 2. All points must have at least 2 coordinates.
|
||||
@ -168,11 +149,11 @@ def _validate_input(points):
|
||||
Examples
|
||||
-------
|
||||
>>> _validate_input([[1, 2]])
|
||||
[(1, 2)]
|
||||
[(1.0, 2.0)]
|
||||
>>> _validate_input([(1, 2)])
|
||||
[(1, 2)]
|
||||
[(1.0, 2.0)]
|
||||
>>> _validate_input([Point(2, 1), Point(-1, 2)])
|
||||
[(2, 1), (-1, 2)]
|
||||
[(2.0, 1.0), (-1.0, 2.0)]
|
||||
>>> _validate_input([])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
@ -200,9 +181,9 @@ def _validate_input(points):
|
||||
)
|
||||
elif not hasattr(points, "__iter__"):
|
||||
raise ValueError(
|
||||
"Expecting an iterable object " f"but got an non-iterable type {points}"
|
||||
f"Expecting an iterable object but got an non-iterable type {points}"
|
||||
)
|
||||
except TypeError as e:
|
||||
except TypeError:
|
||||
print("Expecting an iterable of type Point, list or tuple.")
|
||||
raise
|
||||
|
||||
@ -233,11 +214,11 @@ def _det(a, b, c):
|
||||
Examples
|
||||
----------
|
||||
>>> _det(Point(1, 1), Point(1, 2), Point(1, 5))
|
||||
0
|
||||
0.0
|
||||
>>> _det(Point(0, 0), Point(10, 0), Point(0, 10))
|
||||
100
|
||||
100.0
|
||||
>>> _det(Point(0, 0), Point(10, 0), Point(0, -10))
|
||||
-100
|
||||
-100.0
|
||||
"""
|
||||
|
||||
det = (a.x * b.y + b.x * c.y + c.x * a.y) - (a.y * b.x + b.y * c.x + c.y * a.x)
|
||||
@ -271,13 +252,13 @@ def convex_hull_bf(points):
|
||||
Examples
|
||||
---------
|
||||
>>> convex_hull_bf([[0, 0], [1, 0], [10, 1]])
|
||||
[(0, 0), (1, 0), (10, 1)]
|
||||
[(0.0, 0.0), (1.0, 0.0), (10.0, 1.0)]
|
||||
>>> convex_hull_bf([[0, 0], [1, 0], [10, 0]])
|
||||
[(0, 0), (10, 0)]
|
||||
[(0.0, 0.0), (10.0, 0.0)]
|
||||
>>> convex_hull_bf([[-1, 1],[-1, -1], [0, 0], [0.5, 0.5], [1, -1], [1, 1], [-0.75, 1]])
|
||||
[(-1, -1), (-1, 1), (1, -1), (1, 1)]
|
||||
[(-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0), (1.0, 1.0)]
|
||||
>>> convex_hull_bf([(0, 3), (2, 2), (1, 1), (2, 1), (3, 0), (0, 0), (3, 3), (2, -1), (2, -4), (1, -3)])
|
||||
[(0, 0), (0, 3), (1, -3), (2, -4), (3, 0), (3, 3)]
|
||||
[(0.0, 0.0), (0.0, 3.0), (1.0, -3.0), (2.0, -4.0), (3.0, 0.0), (3.0, 3.0)]
|
||||
"""
|
||||
|
||||
points = sorted(_validate_input(points))
|
||||
@ -336,13 +317,13 @@ def convex_hull_recursive(points):
|
||||
Examples
|
||||
---------
|
||||
>>> convex_hull_recursive([[0, 0], [1, 0], [10, 1]])
|
||||
[(0, 0), (1, 0), (10, 1)]
|
||||
[(0.0, 0.0), (1.0, 0.0), (10.0, 1.0)]
|
||||
>>> convex_hull_recursive([[0, 0], [1, 0], [10, 0]])
|
||||
[(0, 0), (10, 0)]
|
||||
[(0.0, 0.0), (10.0, 0.0)]
|
||||
>>> convex_hull_recursive([[-1, 1],[-1, -1], [0, 0], [0.5, 0.5], [1, -1], [1, 1], [-0.75, 1]])
|
||||
[(-1, -1), (-1, 1), (1, -1), (1, 1)]
|
||||
[(-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0), (1.0, 1.0)]
|
||||
>>> convex_hull_recursive([(0, 3), (2, 2), (1, 1), (2, 1), (3, 0), (0, 0), (3, 3), (2, -1), (2, -4), (1, -3)])
|
||||
[(0, 0), (0, 3), (1, -3), (2, -4), (3, 0), (3, 3)]
|
||||
[(0.0, 0.0), (0.0, 3.0), (1.0, -3.0), (2.0, -4.0), (3.0, 0.0), (3.0, 3.0)]
|
||||
|
||||
"""
|
||||
points = sorted(_validate_input(points))
|
||||
|
Reference in New Issue
Block a user