March 2020
Beginner to intermediate
342 pages
8h 38m
English
Here’s our final binary classification code, in all its glory. It loads Roberto’s data file, learns from it, and then comes up with a bunch of classifications:
| | import numpy as np |
| | |
| | |
| | def sigmoid(z): |
| | return 1 / (1 + np.exp(-z)) |
| | |
| | |
| | def forward(X, w): |
| | weighted_sum = np.matmul(X, w) |
| | return sigmoid(weighted_sum) |
| | |
| | |
| | def classify(X, w): |
| | return np.round(forward(X, w)) |
| | |
| | |
| | def loss(X, Y, w): |
| | y_hat = forward(X, w) |
| | first_term = Y * np.log(y_hat) |
| | second_term = (1 - Y) * np.log(1 - y_hat) |
| | return -np.average(first_term + second_term) |
| | |
| | |
| | def gradient(X, Y, w): |
| | return np.matmul(X.T, (forward(X, w) - Y)) / X.shape[0] |
| | |
| | |
| | def train(X, Y, iterations, ... |
Read now
Unlock full access