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 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 |
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
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. |
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
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) |
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
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. |
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
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 |
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
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] |
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
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] |
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 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. |
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
# 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 + ε |
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 | |
np.random.seed(42) # for reproducibility |
OlderNewer