def train(data, layers=2, alpha=0.1, lsize=100, iterations=5000): x, y = split_xy(data) n, d = x.shape y = (1 + y) / 2 ns = [d] + [lsize] * layers + [1] weights = [] for i in range(layers + 1): weights.append(np.random.random((ns[i + 1], ns[i])) - 0.5) def forward(x): g = lambda x: 1 / (1 + np.exp(-x)) a, z = [x], [x] for w in weights: a.append((w.dot(z[-1]))) z.append(g(a[-1])) return a, z def backward(a, z, y): dl = z[-1] - y for i in range(layers, -1, -1): dj = z[i] * (1 - z[i]) * np.dot(weights[i].transpose(), dl) weights[i] -= alpha * np.outer(dl, a[i]) dl = dj for _ in range(iterations): i = randint(0, n - 1) a, z = forward(x[i]) backward(a, z, y[i]) return lambda x: forward(x)[1][-1][0]
def train(data, c=0): x, y = split_xy(data) n, d = x.shape def f(theta): theta0, theta1 = theta[-1], theta[:-1] pred = y * (np.dot(x, theta1) + theta0) return 0.5 * np.sum(theta1 * theta1) + c * np.sum(1 - pred[pred < 1]) return minimize(f, np.zeros(d + 1)).x
def train(data, l=0): x, y = split_xy(data) n, d = x.shape def f(theta): theta0, theta1 = theta[-1], theta[:-1] loglike = np.log(1 + np.exp(-y * (np.dot(x, theta1) + theta0))) return l / 2 * np.sum(theta1 * theta1) + np.sum(loglike) return minimize(f, np.zeros(d + 1)).x
def train(data, iterations=1000): x, y = split_xy(data) n, d = x.shape theta = np.zeros(d) for it in range(iterations): for i in range(n): if classify(theta, x[i]) != y[i]: theta += y[i] * x[i] return theta
def train(data, c, kernel=scalar, tol=1e-9): def crop(p, lo, hi): return lo if p < lo else (hi if p > hi else p) x, y = split_xy(data) n, d = x.shape alpha, b = np.zeros(n), 0 # f = lambda v: classify(alpha, b, x, y, v, kernel) k = np.empty((n, n)) for i in range(n): for j in range(n): k[i][j] = kernel(x[i], x[j]) ff = lambda i: b + np.sum(np.dot(k[i], alpha * y)) changed = True while changed: changed = False for i in range(n): Ei = ff(i) - y[i] if (y[i] * Ei < -tol and alpha[i] < c) or (y[i] * Ei > tol and alpha[i] > 0): j = randint(0, n - 2) if j == i: j += 1 Ej = ff(j) - y[j] ai, aj = alpha[i], alpha[j] if y[i] != y[j]: L, H = max(0, aj - ai), min(c, c + aj - ai) else: L, H = max(0, ai + aj - c), min(c, ai + aj) if L >= H: continue eta = 2 * k[i][j] - k[i][i] - k[j][j] if eta > 0: continue alpha[j] = crop(alpha[j] - y[j] * (Ei - Ej) / eta, L, H) if abs(alpha[j] - aj) < 1e-5: # lol why not tol? TODO: think about it continue alpha[i] = alpha[i] + y[i] * y[j] * (aj - alpha[j]) b1 = b - Ei - y[i] * (alpha[i] - ai) * kernel( x[i], x[i]) - y[j] * (alpha[j] - aj) * kernel(x[i], x[j]) b2 = b - Ej - y[i] * (alpha[i] - ai) * kernel( x[i], x[j]) - y[j] * (alpha[j] - aj) * kernel(x[j], x[j]) if 0 < alpha[i] < c: b = b1 elif 0 < alpha[j] < c: b = b2 else: b = (b1 + b2) / 2 changed = True return alpha, b
def test(data_train, data, p, kernel=scalar): x, y = split_xy(data_train) x_test, y_test = split_xy(data) n, n_test = len(y), len(y_test) alpha, b = p stats = Stats() f = lambda v: classify(alpha, b, x, y, v, kernel) for i in range(n_test): yc = f(x_test[i]) if yc > 0 and y_test[i] == 1: stats.tp += 1 elif yc > 0 and y_test[i] == -1: stats.fp += 1 elif yc < 0 and y_test[i] == 1: stats.fn += 1 else: stats.tn += 1 return stats
def average_error(data, theta): x, y = split_xy(data) theta0, theta1 = theta[-1], theta[:-1] predictions = 1 / (1 + np.exp(-y * (np.dot(x, theta1) + theta0))) answer = np.empty(predictions.shape) for i, p in enumerate(predictions): if p > 0.5: answer[i] = -1 else: answer[i] = 1 print(answer - y) return np.average(1 - predictions)
def train(data, c, kernel=scalar, tol=1e-9): def crop(p, lo, hi): return lo if p < lo else (hi if p > hi else p) x, y = split_xy(data) n, d = x.shape alpha, b = np.zeros(n), 0 # f = lambda v: classify(alpha, b, x, y, v, kernel) k = np.empty((n, n)) for i in range(n): for j in range(n): k[i][j] = kernel(x[i], x[j]) ff = lambda i: b + np.sum(np.dot(k[i], alpha * y)) changed = True while changed: changed = False for i in range(n): Ei = ff(i) - y[i] if (y[i] * Ei < -tol and alpha[i] < c) or (y[i] * Ei > tol and alpha[i] > 0): j = randint(0, n - 2) if j == i: j += 1 Ej = ff(j) - y[j] ai, aj = alpha[i], alpha[j] if y[i] != y[j]: L, H = max(0, aj - ai), min(c, c + aj - ai) else: L, H = max(0, ai + aj - c), min(c, ai + aj) if L >= H: continue eta = 2 * k[i][j] - k[i][i] - k[j][j] if eta > 0: continue alpha[j] = crop(alpha[j] - y[j] * (Ei - Ej) / eta, L, H) if abs(alpha[j] - aj) < 1e-5: # lol why not tol? TODO: think about it continue alpha[i] = alpha[i] + y[i] * y[j] * (aj - alpha[j]) b1 = b - Ei - y[i] * (alpha[i] - ai) * kernel(x[i], x[i]) - y[j] * (alpha[j] - aj) * kernel(x[i], x[j]) b2 = b - Ej - y[i] * (alpha[i] - ai) * kernel(x[i], x[j]) - y[j] * (alpha[j] - aj) * kernel(x[j], x[j]) if 0 < alpha[i] < c: b = b1 elif 0 < alpha[j] < c: b = b2 else: b = (b1 + b2) / 2 changed = True return alpha, b
def test(data, theta): x, y = split_xy(data) stats = Stats() for i in range(len(y)): yc = classify(theta, x[i]) if yc == 1 and y[i] == 1: stats.tp += 1 elif yc == 1 and y[i] == -1: stats.fp += 1 elif yc == -1 and y[i] == 1: stats.fn += 1 else: stats.tn += 1 return stats
def test(data, network): x, y = split_xy(data) stats = Stats() for i in range(len(y)): yc = 1 if network(x[i]) > 0.5 else -1 if yc == 1 and y[i] == 1: stats.tp += 1 elif yc == 1 and y[i] == -1: stats.fp += 1 elif yc == -1 and y[i] == 1: stats.fn += 1 else: stats.tn += 1 return stats
def test(data, theta): x, y = split_xy(data) stats = Stats() theta0, theta1 = theta[-1], theta[:-1] pred = np.dot(x, theta1) + theta0 pred[pred < 0], pred[pred >= 0] = -1, 1 for i in range(len(y)): if pred[i] == 1 and y[i] == 1: stats.tp += 1 elif pred[i] == 1 and y[i] == -1: stats.fp += 1 elif pred[i] == -1 and y[i] == 1: stats.fn += 1 else: stats.tn += 1 return stats
def average_error(data, theta): x, y = split_xy(data) theta0, theta1 = theta[-1], theta[:-1] predictions = 1 / (1 + np.exp(-y * (np.dot(x, theta1) + theta0))) return np.average(1 - predictions)