import numpy as np
import matplotlib.pyplot as plt
data = np.array([
[0.20, 0.30], [0.32, 0.23], [0.39, 0.35], [0.46, 0.42],
[0.51, 0.39], [0.60, 0.47], [0.64, 0.51], [0.75, 0.46]
])
plt.figure()
plt.scatter(data[:,0], data[:,1], marker='o', c='C0')
plt.plot([0.1, 0.9], [0.174251 + 0.1*0.448577, 0.174251 + 0.9*0.448577], c='C1', linestyle=':', linewidth=1)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
data = np.array([
[0.20, 0.30], [0.32, 0.23], [0.39, 0.35], [0.46, 0.42],
[0.51, 0.39], [0.60, 0.47], [0.64, 0.51], [0.75, 0.46]
])
x = data[:,0]
y = data[:,1]
random_w = np.random.rand(2)
mse = np.mean((y - (random_w[0] + random_w[1] * x)) ** 2)
print('MSE =', mse)
plt.figure()
plt.scatter(data[:,0], data[:,1], marker='o', c='C0')
plt.plot([0.1, 0.9], [random_w[0] + 0.1*random_w[1], random_w[0] + 0.9*random_w[1]], c='C1')
plt.show()
Source: https://rasbt.github.io/mlxtend/user_guide/general_concepts/gradient-optimization/
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
def fun1(w):
return (w ** 2) + 4
def grad_fun1(w):
return 2 * w
w0 = (np.random.rand() * 20) - 10
print('Initial Guess =', w0)
res = minimize(fun=fun1, jac=grad_fun1, x0=w0, method='CG')
print(res)
print('---------------------')
print('Solution =', res.x)
print('Objective Function Value =', res.fun)
plt.figure(dpi=90)
x = np.linspace(-10, 10, 100)
plt.plot(x, fun1(x), c='gray')
plt.scatter(w0, fun1(w0), c='y', marker='o', s=60)
plt.scatter(res.x, res.fun, c='r', marker='x', s=80)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
data = np.array([
[0.20, 0.30], [0.32, 0.23], [0.39, 0.35], [0.46, 0.42],
[0.51, 0.39], [0.60, 0.47], [0.64, 0.51], [0.75, 0.46]
])
X = np.hstack((np.ones((data.shape[0], 1)), np.reshape(data[:,0], (data.shape[0], 1))))
y = data[:,1]
def mse(w):
# Implement MSE
return np.mean((y - (X @ w)) ** 2)
def grad_mse(w):
# Implement the gradient of MSE
return 2 * np.mean(np.reshape(y - (X @ w), (X.shape[0], 1)) * (-X), axis=0)
w0 = np.random.rand(X.shape[1])
print('Initial Guess w0 =', w0)
print('MSE at w0 =', mse(w0))
# Gradient Descent to get w1 and w0
res = minimize(fun=mse, jac=grad_mse, x0=w0, method='CG')
w = res.x
print('Final w =', w)
print('Final MSE =', mse(w))
plt.figure()
plt.scatter(data[:,0], data[:,1], marker='o', c='gray')
plt.plot(
np.array([data[:,0].min()-0.2,data[:,0].max()+0.2]),
w[1] * np.array([data[:,0].min()-0.2,data[:,0].max()+0.2]) + w[0], c='r'
)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
data = np.array([ [0.20, 0.30], [0.32, 0.23], [0.39, 0.35], [0.46, 0.42],
[0.51, 0.39], [0.60, 0.47], [0.64, 0.51], [0.75, 0.46] ])
x = np.reshape(data[:,0], (data.shape[0], 1))
X = np.hstack((x, x ** 2))
y = data[:,1]
from sklearn import linear_model
reg = linear_model.LinearRegression().fit(X, y)
w = np.hstack(([reg.intercept_], reg.coef_))
X = np.hstack((np.ones((data.shape[0], 1)), X))
print('Final w =', w)
print('Final MSE =', mse(w))
plt.figure()
plt.scatter(X[:,1], y, marker='o', c='gray')
plt.plot(X[:,1], np.dot(X, w), c='r')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
data = np.array([ [0.20, 0.30], [0.32, 0.23], [0.39, 0.35], [0.46, 0.42],
[0.51, 0.39], [0.60, 0.47], [0.64, 0.51], [0.75, 0.46] ])
x = np.reshape(data[:,0], (data.shape[0], 1))
X = np.hstack((x, x ** 2, x ** 3, x ** 4))
y = data[:,1]
from sklearn import linear_model
reg = linear_model.LinearRegression().fit(X, y)
w = np.hstack(([reg.intercept_], reg.coef_))
X = np.hstack((np.ones((data.shape[0], 1)), X))
print('Final w =', w)
print('Final MSE =', mse(w))
plt.figure()
plt.scatter(X[:,1], y, marker='o', c='gray')
plt.plot(X[:,1], np.dot(X, w), c='r')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
data = np.array([ [0.20, 0.30], [0.32, 0.23], [0.39, 0.35], [0.46, 0.42],
[0.51, 0.39], [0.60, 0.47], [0.64, 0.51], [0.75, 0.46] ])
x = np.reshape(data[:,0], (data.shape[0], 1))
X = np.hstack((x, x ** 2, x ** 3, x ** 4, x ** 5, x ** 6, x ** 7))
y = data[:,1]
from sklearn import linear_model
reg = linear_model.LinearRegression().fit(X, y)
w = np.hstack(([reg.intercept_], reg.coef_))
X = np.hstack((np.ones((data.shape[0], 1)), X))
print('Final w =', w)
print('Final MSE =', mse(w))
plt.figure()
plt.scatter(X[:,1], y, marker='o', c='gray')
plt.plot(X[:,1], np.dot(X, w), c='r')
plt.show()
Source: https://rpubs.com/charlydethibault/348566
Let there be two classes: $Y \in \{0,+1\}$
Source: https://guide.freecodecamp.org/machine-learning/logistic-regression/
a1 = np.random.uniform(low=0, high=0.5, size=(100,2))
a2 = np.random.uniform(low=0.5, high=1, size=(100,2))
X = np.hstack((np.ones((200,1)), np.vstack((a1, a2))))
y = np.hstack(( np.zeros((100)), np.ones((100)) ))
def obj_fun(w):
gx = 1 / (1 + np.exp(-np.dot(X, w)))
# Implement Cross-Entropy
return - np.mean((y * np.log(np.fmax(gx, 1e-9))) + ((1 - y) * np.log(np.fmax(1 - gx, 1e-9))))
def obj_grad(w):
gx = 1 / (1 + np.exp(-np.dot(X, w)))
# Implement the gradient of Cross-Entropy
return - np.mean(np.reshape((y - gx), (X.shape[0],1)) * X, axis=0)
w0 = np.random.rand(X.shape[1]) * 2 - 1
res = minimize(fun=obj_fun, jac=obj_grad, x0=w0, method='CG')
print('Initial Guess =', w0)
print('Initial Obj Fun val =', obj_fun(w0))
plt.figure()
for i in range(2):
plt.scatter(X[y==i,1], X[y==i,2], marker='x')
extreme_xvals = np.array([np.min(X[:,1]), np.max(X[:,1])])
plt.plot(extreme_xvals, (-w0[0] - w0[1] * extreme_xvals) / w0[2], c='r')
plt.show()
plt.figure()
for i in range(2):
plt.scatter(X[y==i,1], X[y==i,2], marker='x')
final_w = res.x
print('Final w =', final_w)
print('Obj Fun val =', obj_fun(final_w))
plt.plot(extreme_xvals, (-final_w[0] - final_w[1] * extreme_xvals) / final_w[2], c='r')
plt.ylim(extreme_xvals+np.array([-0.05, 0.05]))
plt.show()
from sklearn.datasets import load_digits
X, y = load_digits().data, load_digits().target
X = X[y<2,:]
y = y[y<2]
X = np.hstack((np.ones((X.shape[0],1)), X))
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)
def obj_fun(w, X, y):
gx = 1 / (1 + np.exp(-np.dot(X, w)))
return -np.mean(y * np.log(np.fmax(gx, 1e-6)) + (1 - y) * np.log(np.fmax(1 - gx, 1e-6)))
def obj_grad(w, X, y):
gx = 1 / (1 + np.exp(-np.dot(X, w)))
return -np.mean(np.reshape(y - gx, (y.shape[0],1)) * X, axis=0)
min_cost = +np.inf
for i in range(10):
w0 = (np.random.rand(X.shape[1]) * 2) - 1
res = minimize(fun=obj_fun, jac=obj_grad, args=(X_train, y_train), x0=w0, method='CG')
if res.fun < min_cost:
min_w = res.x
min_cost = res.fun
from sklearn.metrics import accuracy_score
print('Accuracy =',
accuracy_score(y_test, np.round(1 / (1 + np.exp(-np.dot(X_test, min_w)))))
)
for i in range(5):
plt.figure(dpi=50)
plt.imshow(np.reshape(X_test[i,1:], (8,8)), cmap='gray')
plt.xlabel(
'Predicted Digit: '+str(int(np.round(1 / (1 + np.exp(-np.dot(X_test[i,:], min_w)))))),
fontsize=24)
plt.xticks([], [])
plt.yticks([], [])
plt.show()