This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#https://docs.python.org/2/tutorial/datastructures.html | |
from collections import deque | |
def bfs(g, root_node): | |
""" Implementation of a breadth first search (BFS) | |
BFS is useful e.g. when we need to find a route between | |
It is NOT recursive | |
Algo: | |
1. Enqueue root node; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dfs(g, curr_node, visited=None): | |
""" | |
Parameters | |
---------- | |
graph: dictionary | |
dictionary of nodes and its neighbors | |
node: list | |
Returns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GraphNode: | |
def __init__(self, value, neighbors): | |
"""Initialize graph node | |
Parameters | |
---------- | |
name: str | |
node identifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def linear_regression(x, y, lr=1.0e-4, n_steps=10000, lr_decay=0.5): | |
"""Solves 1 dimensional regression problem: provides w=(w_1, ..., w_n) such that: | |
w* = min_{w} sum_{i=1}{m}[(<w, x_i> - y_i)**2] (mean squared error) | |
L(w) = sum_{i=1}{m}[(<w, x_i> - y_i)**2] | |
<w, x_i> = w_1*x_{i1} + w_2*x_{i2} + ... w_n*x_{in} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def linear_regression_1d(x, y): | |
"""Solves 1 dimensional regression problem: provides w=(w_1, ..., w_n) such that: | |
w* = min_{w} sum_{i=1}{m}[(<w, x_i> - y_i)**2] | |
L(w) = sum_{i=1}{m}[(<w, x_i> - y_i)**2] | |
<w, x_i> = w_1*x_{i1} + w_2*x_{i2} + ... w_n*x_{in} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BinaryTreeNode: | |
def __init__(self, value): | |
self.value = value | |
self.left = None | |
self.right = None | |
def insert(self, value): | |
"insert elements in the way to maintain binary search tree structure" | |
if self.value >= value: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def set_bit(x, i): | |
bi = 0b1 << i | |
return x | bi | |
def xor_bit(x, i): | |
bi = 0b1 << i | |
return x ^ bi | |
def clear_bit(x, i): | |
bi = ~(0b1 << i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_permuted_palindrome(s): | |
"""Checks if the string is a permuted version of a palindrome | |
Upper case do not matter, spaces can be ignored. | |
Example: | |
"cat cat" -> Yes (palindrome: "cat tac") | |
"cat do" -> No | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# In[32]: | |
import numpy as np | |
def hist_pdf(x, data, n_bins=10, minv=None, maxv=None): | |
"""Histogram density estimator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def hist_pdf(x, data, n_bins=10, minv=None, maxv=None): | |
"""Histogram density estimator | |
[minv, minv+delta, , minv+delta*2, ..., 1] | |
for any x in B_l=[minv+delta*j, minv+delta*(j+1)] density is estimated in the following way | |
p(x) = p(x | x \in B_l) * p(x \in B_l) = (1/delta) * (\sum_{x_j}{I(x_j \in B_l)} / N) |