Skip to content

Instantly share code, notes, and snippets.

@uberwach
Created October 20, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uberwach/f5498b0cd4f42c0bbbb1 to your computer and use it in GitHub Desktop.
Save uberwach/f5498b0cd4f42c0bbbb1 to your computer and use it in GitHub Desktop.
IDA 2: Introduction to Python (selected solutions)
# Solution Exercise 2.2
def gradient_descent(gradient, eta, x0=0, max_iter=10000, tol=1e-7):
# Your code
x_k = x0
i = 0
diff = tol + 1
while i < max_iter and diff >= tol:
x_k1 = x_k - eta * gradient(x_k)
diff = abs(x_k - x_k1)
x_k = x_k1
i += 1
return x_k
f_1 = lambda x: x**2 - x - 3
gradient_1 = lambda x: 2*x - 1
print 'Function 1: x² - x - 3'
x_prime = gradient_descent(gradient_1, 0.01)
print 'Local min {} at {}.'.format(f_1(x_prime), x_prime)
ys = [f_1(0.01 * x) for x in range(-1000, 1001)]
print 'Global min (?): {}'.format(min(ys))
from math import sin, cos
from random import uniform
f_2 = lambda x: sin(x)**2 / (x**2 + 1)
gradient_2 = lambda x: (2 * sin(x) * ( (x**2 + 1) * cos(x) - x * sin(x))) / (x**2 + 1)**2
print 'Function 2: sin²(x) / (x² + 1)'
x_primes = []
for i in range(10):
x0 = uniform(-6.0, 6.0)
x_prime = gradient_descent(gradient_2, -0.001, x0)
print 'Local max {} at {} with start value {}.'.format(f_2(x_prime), x_prime, x0)
x_primes.append(x_prime)
ys = [f_2(0.01 * x) for x in range(-1000, 1001)]
print 'Global max (?): {}'.format(max(ys))
# Solution Exercise 4.1
idx = (svc.predict(X_test) != y_test)
X_miss = X_test[idx, :]
y_miss = y_test[idx]
len(X_miss) # number of missclassifications
for i in range(min(10, len(X_miss))):
plt.matshow(X_miss[i].reshape(8,8), cmap=plt.cm.Greys)
plt.xlabel('Classified as ' + str(svc.predict(X_miss[i])[0]) + ' but is ' + str(y_miss[i]))
@uberwach
Copy link
Author

So here a compromise, I post some solutions (which I deem most helpful for you) and delete them after the semester.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment