{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic usage\n", "\n", "\n", "\n", "Let us start with a simple example to illustrate the use of `ruptures`: generate a 3-dimensional piecewise constant signal with noise and estimate the change points.\n", "\n", "## Setup\n", "First, we make the necessary imports." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt # for display purposes\n", "\n", "import ruptures as rpt # our package" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate and display the signal\n", "\n", "Let us generate a 3-dimensional piecewise constant signal with Gaussian noise." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n_samples, n_dims, sigma = 1000, 3, 2\n", "n_bkps = 4 # number of breakpoints\n", "signal, bkps = rpt.pw_constant(n_samples, n_dims, n_bkps, noise_std=sigma)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The true change points of this synthetic signal are available in the `bkps` variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(bkps)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the first four element are change point indexes while the last is simply the number of samples.\n", "(This is a technical convention so that functions in `ruptures` always know the length of the signal at hand.)\n", "\n", "It is also possible to plot our $\\mathbb{R}^3$-valued signal along with the true change points with the `rpt.display` function.\n", "In the following image, the color changes whenever the mean of the signal shifts." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax_array = rpt.display(signal, bkps)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change point detection\n", "We can now perform change point detection, meaning that we find the indexes where the signal mean changes.\n", "To that end, we minimize the sum of squared errors when approximating the signal by a piecewise constant signal.\n", "Formally, for a signal $y_0,y_1,\\dots,y_{T-1}$ ($T$ samples), we solve the following optimization problem, over all possible change positions $t_1