Artificial Intelligence for the Brave and True

This is an on-point book on Artificial Intelligence (AI) and is meant for technical audience. You will not see lengthy introductions or digressions and instead, we will focus on the crux of AI algorithms, models, and related machinery. We will be assuming good knowledge of Python and linear algebra.

Linear Regression

This is likely the simplest form of statistical AI. The goal is to approximate date with a line.

Implementation (Linear Regression)
import numpy as np

from dataclasses import dataclass

class LinearRegression:
    """Linear Regression."""

    epochs: int
    learning_rate: float
    logging: bool

    def fit(self, features: np.ndarray, labels: np.ndarray) -> None:
        """Fits the Linear Regression model."""

        num_samples, num_features = features.shape
        self.weights, self.bias = np.zeros(num_features), 0

        for epoch in range(self.epochs):
            residuals = labels - self.predict(features)

            d_weights = -2 / num_samples *
            d_bias = -2 / num_samples * residuals.sum()

            self.weights -= self.learning_rate * d_weights
            self.bias -= self.learning_rate * d_bias

            if self.logging:
                print(f"MSE Loss [{epoch}]: {(residuals ** 2).mean():.3f}")

    def predict(self, features: np.ndarray) -> np.ndarray:
        """Performs inference using the given features."""

        return + self.bias

Logistic Regression


K-Nearest Neighbors (k-NN)


K-Means Clustering (KMeans)