Skip to content

optimization (TheAlgorithms#1303) #5

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 8 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
818 changes: 433 additions & 385 deletions DIRECTORY.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data_structures/binary_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BinarySearchTree:
def __init__(self):
self.root = None

# Insert a new node in Binary Search Tree with value label
def insert(self, label):
# Create a new Node
new_node = Node(label, None)
Expand Down
6 changes: 4 additions & 2 deletions linear_algebra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ This module contains some useful classes and functions for dealing with linear a

## Overview

- class Vector
### class Vector
-
- This class represents a vector of arbitray size and operations on it.

**Overview about the methods:**
Expand All @@ -32,7 +33,8 @@ This module contains some useful classes and functions for dealing with linear a
- function randomVector(N,a,b)
- returns a random vector of size N, with random integer components between 'a' and 'b'.

- class Matrix
### class Matrix
-
- This class represents a matrix of arbitrary size and operations on it.

**Overview about the methods:**
Expand Down
60 changes: 57 additions & 3 deletions machine_learning/scoring_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

# Mean Absolute Error
def mae(predict, actual):
"""
Examples(rounded for precision):
>>> actual = [1,2,3];predict = [1,4,3]
>>> np.around(mae(predict,actual),decimals = 2)
0.67

>>> actual = [1,1,1];predict = [1,1,1]
>>> mae(predict,actual)
0.0
"""
predict = np.array(predict)
actual = np.array(actual)

Expand All @@ -27,6 +37,16 @@ def mae(predict, actual):

# Mean Squared Error
def mse(predict, actual):
"""
Examples(rounded for precision):
>>> actual = [1,2,3];predict = [1,4,3]
>>> np.around(mse(predict,actual),decimals = 2)
1.33

>>> actual = [1,1,1];predict = [1,1,1]
>>> mse(predict,actual)
0.0
"""
predict = np.array(predict)
actual = np.array(actual)

Expand All @@ -39,6 +59,16 @@ def mse(predict, actual):

# Root Mean Squared Error
def rmse(predict, actual):
"""
Examples(rounded for precision):
>>> actual = [1,2,3];predict = [1,4,3]
>>> np.around(rmse(predict,actual),decimals = 2)
1.15

>>> actual = [1,1,1];predict = [1,1,1]
>>> rmse(predict,actual)
0.0
"""
predict = np.array(predict)
actual = np.array(actual)

Expand All @@ -51,6 +81,16 @@ def rmse(predict, actual):

# Root Mean Square Logarithmic Error
def rmsle(predict, actual):
"""
Examples(rounded for precision):
>>> actual = [10,10,30];predict = [10,2,30]
>>> np.around(rmsle(predict,actual),decimals = 2)
0.75

>>> actual = [1,1,1];predict = [1,1,1]
>>> rmsle(predict,actual)
0.0
"""
predict = np.array(predict)
actual = np.array(actual)

Expand All @@ -68,15 +108,29 @@ def rmsle(predict, actual):

# Mean Bias Deviation
def mbd(predict, actual):
"""
This value is Negative, if the model underpredicts,
positive, if it overpredicts.

Example(rounded for precision):

Here the model overpredicts
>>> actual = [1,2,3];predict = [2,3,4]
>>> np.around(mbd(predict,actual),decimals = 2)
50.0

Here the model underpredicts
>>> actual = [1,2,3];predict = [0,1,1]
>>> np.around(mbd(predict,actual),decimals = 2)
-66.67
"""
predict = np.array(predict)
actual = np.array(actual)

difference = predict - actual
numerator = np.sum(difference) / len(predict)
denumerator = np.sum(actual) / len(predict)
print(numerator)
print(denumerator)

# print(numerator, denumerator)
score = float(numerator) / denumerator * 100

return score
6 changes: 1 addition & 5 deletions maths/abs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ def abs_val(num):
>>abs_val(0)
0
"""
if num < 0:
return -num

# Returns if number is not < 0
return num
return -num if num < 0 else num


def main():
Expand Down
8 changes: 2 additions & 6 deletions maths/average_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@

def average(nums):
"""Find mean of a list of numbers."""
sum = 0
for x in nums:
sum += x
avg = sum / len(nums)
print(avg)
avg = sum(nums) / len(nums)
return avg


def main():
"""Call average module to find mean of a specific list of numbers."""
average([2, 4, 6, 8, 20, 50, 70])
print(average([2, 4, 6, 8, 20, 50, 70]))


if __name__ == "__main__":
Expand Down
7 changes: 3 additions & 4 deletions searches/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ def binary_search(sorted_collection, item):
current_item = sorted_collection[midpoint]
if current_item == item:
return midpoint
elif item < current_item:
right = midpoint - 1
else:
if item < current_item:
right = midpoint - 1
else:
left = midpoint + 1
left = midpoint + 1
return None


Expand Down
3 changes: 2 additions & 1 deletion sorts/selection_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def selection_sort(collection):
for k in range(i + 1, length):
if collection[k] < collection[least]:
least = k
collection[least], collection[i] = (collection[i], collection[least])
if least != i:
collection[least], collection[i] = (collection[i], collection[least])
return collection


Expand Down