You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 KiB
45 KiB
None
<html lang="en">
<head>
</head>
</html>
In [3]:
#!pip3 install scikit-learn
from sklearn import datasets
iris = datasets.load_iris()
print(iris.DESCR)
In [37]:
import numpy as np
import matplotlib.pyplot as plt
x = iris.data[:,3:4]
y = (iris.target == 0).astype(int).reshape(-1,1)
y
Out[37]:
In [11]:
def sigmoid(z):
z = np.clip(z, -500, 500)
sig = 1.0 / (1.0 + np.exp(-z))
return sig
In [13]:
def log_loss(y, p, eps=1e-12):
p = np.clip(p, eps, 1 - eps)
return -np.mean(y*np.log(p) + (1-y)*np.log(1-p))
In [27]:
lr=0.1
epochs=2000
l2=0.0,
X = np.column_stack([X, np.ones_like(X)])
m = X.shape[0]
theta = np.zeros((2,1))
In [28]:
X
Out[28]:
In [30]:
for i in range(epochs):
z = X @ theta # (m,1)
h = sigmoid(z) # (m,1)
grad = (X.T @ (h - y)) / m # (2,1) <-- from your formula
theta -= lr * grad
#if (i % 0 == 0 or t == epochs-1):
# print(f"{i:4d} loss={log_loss(y, h):.6f} w={theta[0,0]:.6f} b={theta[1,0]:.6f}")
w, b = theta[0,0], theta[1,0]
In [33]:
w
Out[33]:
In [34]:
def predict_proba(x, w, b):
x = np.asarray(x, float).reshape(-1)
return sigmoid(w*x + b)
def predict(x, w, b, thresh=0.5):
return (predict_proba(x, w, b) >= thresh).astype(int)
In [ ]:
rng = np.random.default_rng(0)
m = 120
xNew = np.linspace(-1, 3, m)
p = predict_proba(xNew, w, b)
print(f"\nLearned: w={w:.3f}, b={b:.3f}, loss={log_loss(p.reshape(-1,1), p.reshape(-1,1)):.4f}")
In [64]:
yJitter = y +np.random.uniform(-0.1, 0.1, size=y.shape)
plt.plot(x, yJitter, 'ok', alpha=0.1)
plt.plot(xNew,p)
Out[64]:
In [ ]: