Skip to content

Instantly share code, notes, and snippets.

@edenau
edenau / python-generator.py
Last active December 27, 2019 16:37
Python Generator
def gen(n): # an infinite sequence generator that generates integers >= n
while True:
yield n
n += 1
G = gen(3) # starts at 3
print(next(G)) # 3
print(next(G)) # 4
print(next(G)) # 5
print(next(G)) # 6
@edenau
edenau / python-enumerate.py
Last active December 27, 2019 16:38
Python Enumerate
upperCase = ['A', 'B', 'C', 'D', 'E', 'F']
lowerCase = ['a', 'b', 'c', 'd', 'e', 'f']
for i, (upper, lower) in enumerate(zip(upperCase, lowerCase), 1):
print(f'{i}: {upper} and {lower}.')
# 1: A and a.
# 2: B and b.
# 3: C and c.
# 4: D and d.
# 5: E and e.
# 6: F and f.
@edenau
edenau / python-zip-2.py
Last active December 27, 2019 16:38
Python Zip 2
Eng = list(zip(engList, espList, numList))
Eng.sort() # sort by engList
a, b, c = zip(*Eng)
print(a)
print(b)
print(c)
# ('one', 'two', 'zero')
# ('uno', 'dos', 'cero')
# (1, 2, 0)
@edenau
edenau / python-zip-1.py
Last active December 27, 2019 16:38
Python Zip 1
numList = [0, 1, 2]
engList = ['zero', 'one', 'two']
espList = ['cero', 'uno', 'dos']
print(list(zip(numList, engList, espList)))
# [(0, 'zero', 'cero'), (1, 'one', 'uno'), (2, 'two', 'dos')]
for num, eng, esp in zip(numList, engList, espList):
print(f'{num} is {eng} in English and {esp} in Spanish.')
# 0 is zero in English and cero in Spanish.
# 1 is one in English and uno in Spanish.
@edenau
edenau / python-unpacking.py
Last active December 27, 2019 16:38
Python Unpacking
a, b, c, d = aList[0:4]
print(f'a = {a}, b = {b}, c = {c}, d = {d}')
# a = 0, b = 1, c = 2, d = 3
a, *b, c, d = aList
print(f'a = {a}, b = {b}, c = {c}, d = {d}')
# a = 0, b = [1, 2, 3, 4, 5, 6, 7], c = 8, d = 9
@edenau
edenau / python-list-comprehension.py
Last active December 27, 2019 16:39
Python List Comprehension
print(list(map(add_func, aList)))
print([x ** 2 for x in aList])
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(list(filter(is_odd, aList)))
print([x for x in aList if x%2 == 1])
# [1, 3, 5, 7, 9]
# [1, 3, 5, 7, 9]
@edenau
edenau / python-lambda.py
Last active December 27, 2019 16:39
Python Lambda
add_func = lambda z: z ** 2
is_odd = lambda z: z%2 == 1
multiply = lambda x,y: x*y
aList = list(range(10))
print(aList)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def gradient_descent(X, y, w, b, learning_rate):
dw = -2 * np.sum(X * (y - w * X - b)) # ∂e/∂w
db = -2 * np.sum(y - w * X - b) # ∂e/∂b
w_new = w - learning_rate * dw # minus sign since we are minizing e
b_new = b - learning_rate * db
return w_new, b_new
def get_loss(X,y,w,b):
return (y - w * X - b).T @ (y - w * X - b) # square loss,
# .T and @ denote transpose and matrix multiplication resp.
# Make sure X and y are linearly correlated with normally distributed noise
n = 10000 # 10000 datum points
X = np.random.uniform(-10,10, n)
noise = np.random.normal(0, 3, n) # Gaussian distribution
true_w, true_b = 7.6, -3.3
y = true_w * X + true_b + noise # y = w * x + b + ε
import numpy as np
np.random.seed(42) # for reproducibility