Skip to content
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

Fixes issue #12224: Added Kadane's Algorithm #12270

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1cb79bc
added ridge regression
ankana2113 Oct 23, 2024
b72320b
added ridge regression
ankana2113 Oct 23, 2024
d4fc2bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2024
a84d209
added ridge regression
ankana2113 Oct 23, 2024
6fc134d
added ridge regression
ankana2113 Oct 23, 2024
21fe32f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2024
7484cda
ridge regression
ankana2113 Oct 23, 2024
b1353dd
ridge regression
ankana2113 Oct 23, 2024
2eeb450
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2024
1713cbe
resolved errors
ankana2113 Oct 23, 2024
3876437
resolved conflicts
ankana2113 Oct 23, 2024
c76784e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2024
544a38b
resolved conflicts
ankana2113 Oct 23, 2024
d5963b2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2024
b0255a8
added doctests
ankana2113 Oct 24, 2024
d8c0b7c
Merge branch 'main' of https://github.com/ankana2113/Python
ankana2113 Oct 24, 2024
59d3ceb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2024
83d7252
ruff and minor checks
ankana2113 Oct 24, 2024
1918aac
Merge branch 'main' of https://github.com/ankana2113/Python
ankana2113 Oct 24, 2024
f614b2e
minor chenges
ankana2113 Oct 24, 2024
254b9bf
minor checks
ankana2113 Oct 24, 2024
97eb853
minor checks
ankana2113 Oct 24, 2024
dcf47d4
minor changes
ankana2113 Oct 24, 2024
0ea341a
descriptive names
ankana2113 Oct 24, 2024
1ff7975
Fix ruff check in loss_functions.py
ankana2113 Oct 24, 2024
1459adf
fixed pre-commit issues
ankana2113 Oct 24, 2024
0c04372
Merge pull request #1 from ankana2113/main
ankana2113 Oct 24, 2024
5c2d1fe
added largest rectangle histogram function
ankana2113 Oct 24, 2024
50d5bb1
added largest rectangle histogram function
ankana2113 Oct 24, 2024
d029119
Merge branch 'master' of https://github.com/ankana2113/Python
ankana2113 Oct 24, 2024
b00284f
Merge branch 'largest_rect'
ankana2113 Oct 24, 2024
bfb8167
added kadane's algo
ankana2113 Oct 24, 2024
91f0395
Merge pull request #2 from ankana2113/kadane_algo
ankana2113 Oct 24, 2024
a6c40a9
Merge pull request #5 from ankana2113/master
ankana2113 Oct 24, 2024
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
42 changes: 42 additions & 0 deletions data_structures/arrays/kadanes_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Kadane's algorithm


def kadanes_algorithm(arr: list[int]) -> int:
"""
Function to find the maximum sum of a contiguous subarray using Kadane's algorithm

>>> kadanes_algorithm([-2, 1, -3, 4, -1, 2, 1, -5, 4])
6

>>> kadanes_algorithm([-1, -2, -3, -4])
-1

>>> kadanes_algorithm([5, 4, -1, 7, 8])
23

>>> kadanes_algorithm([1])
1

>>> kadanes_algorithm([-1, 2, 3, -5, 4])
5
"""
# initializing variables
max_current = arr[0] # store the current max sum
max_global = arr[0] # store the global max sum

# looping through the array starting at the second element
for i in range(1, len(arr)):
# update current max sum by choosing the maximum between
# current element alone or current element plus previous max
max_current = max(arr[i], max_current + arr[i])

# update global max sum if current max is larger
max_global = max(max_current, max_global)

return max_global


if __name__ == "__main__":
import doctest

doctest.testmod()
39 changes: 39 additions & 0 deletions data_structures/stacks/largest_rectangle_histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def largest_rectangle_area(heights: list[int]) -> int:
"""
Inputs an array of integers representing the heights of bars,
and returns the area of the largest rectangle that can be formed

>>> largest_rectangle_area([2, 1, 5, 6, 2, 3])
10

>>> largest_rectangle_area([2, 4])
4

>>> largest_rectangle_area([6, 2, 5, 4, 5, 1, 6])
12

>>> largest_rectangle_area([1])
1
"""
stack: list[int] = []
max_area = 0
heights = [*heights, 0] # make a new list by appending the sentinel 0
n = len(heights)

for i in range(n):
# make sure the stack remains in increasing order
while stack and heights[i] < heights[stack[-1]]:
h = heights[stack.pop()] # height of the bar
# if stack is empty, it means entire width can be taken from index 0 to i-1
w = i if not stack else i - stack[-1] - 1 # calculate width
max_area = max(max_area, h * w)

stack.append(i)

return max_area


if __name__ == "__main__":
import doctest

doctest.testmod()
2 changes: 1 addition & 1 deletion machine_learning/frequent_pattern_growth.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def ascend_tree(leaf_node: TreeNode, prefix_path: list[str]) -> None:
ascend_tree(leaf_node.parent, prefix_path)


def find_prefix_path(base_pat: frozenset, tree_node: TreeNode | None) -> dict: # noqa: ARG001
def find_prefix_path(_: frozenset, tree_node: TreeNode | None) -> dict:
"""
Find the conditional pattern base for a given base pattern.

Expand Down
17 changes: 13 additions & 4 deletions machine_learning/loss_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,13 +629,15 @@ def smooth_l1_loss(y_true: np.ndarray, y_pred: np.ndarray, beta: float = 1.0) ->
return np.mean(loss)


def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float:
def kullback_leibler_divergence(
y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-10
) -> float:
"""
Calculate the Kullback-Leibler divergence (KL divergence) loss between true labels
and predicted probabilities.

KL divergence loss quantifies dissimilarity between true labels and predicted
probabilities. It's often used in training generative models.
KL divergence loss quantifies the dissimilarity between true labels and predicted
probabilities. It is often used in training generative models.

KL = Σ(y_true * ln(y_true / y_pred))

Expand All @@ -649,6 +651,7 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float
>>> predicted_probs = np.array([0.3, 0.3, 0.4])
>>> float(kullback_leibler_divergence(true_labels, predicted_probs))
0.030478754035472025

>>> true_labels = np.array([0.2, 0.3, 0.5])
>>> predicted_probs = np.array([0.3, 0.3, 0.4, 0.5])
>>> kullback_leibler_divergence(true_labels, predicted_probs)
Expand All @@ -659,7 +662,13 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float
if len(y_true) != len(y_pred):
raise ValueError("Input arrays must have the same length.")

kl_loss = y_true * np.log(y_true / y_pred)
# negligible epsilon to avoid issues with log(0) or division by zero
epsilon = 1e-10
y_pred = np.clip(y_pred, epsilon, None)

# calculate KL divergence only where y_true is not zero
kl_loss = np.where(y_true != 0, y_true * np.log(y_true / y_pred), 0.0)

return np.sum(kl_loss)


Expand Down
Loading