# Continuous linear change (CostCLinear)#

Bases: BaseCost

Piecewise linear approximation with a continuity constraint.

Source code in ruptures/costs/costclinear.py
  8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 class CostCLinear(BaseCost): r"""Piecewise linear approximation with a continuity constraint.""" model = "clinear" def __init__(self): """Initialize the object.""" self.signal = None self.min_size = 3 def fit(self, signal) -> "CostCLinear": """Set parameters of the instance. Args: signal (array): signal of shape (n_samples, n_dims) or (n_samples,) Returns: self """ if signal.ndim == 1: self.signal = signal.reshape(-1, 1) else: self.signal = signal return self def error(self, start, end) -> float: """Return the approximation cost on the segment [start:end]. Args: start (int): start of the segment end (int): end of the segment Returns: segment cost (float) Raises: NotEnoughPoints: when the segment is too short (less than min_size samples). """ if end - start < self.min_size: raise NotEnoughPoints if start == 0: start = 1 sub = self.signal[start:end] slope = (self.signal[end - 1] - self.signal[start - 1]) / (end - start) intercept = self.signal[start - 1] approx = slope.reshape(-1, 1) * np.arange( 1, end - start + 1 ) + intercept.reshape(-1, 1) return np.sum((sub - approx.transpose()) ** 2) 

## __init__()#

Initialize the object.

Source code in ruptures/costs/costclinear.py
 13 14 15 16 def __init__(self): """Initialize the object.""" self.signal = None self.min_size = 3 

## error(start, end)#

Return the approximation cost on the segment [start:end].

Parameters:

Name Type Description Default
start int

start of the segment

required
end int

end of the segment

required

Returns:

Type Description
float

segment cost (float)

Raises:

Type Description
NotEnoughPoints

when the segment is too short (less than min_size samples).

Source code in ruptures/costs/costclinear.py
 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 def error(self, start, end) -> float: """Return the approximation cost on the segment [start:end]. Args: start (int): start of the segment end (int): end of the segment Returns: segment cost (float) Raises: NotEnoughPoints: when the segment is too short (less than min_size samples). """ if end - start < self.min_size: raise NotEnoughPoints if start == 0: start = 1 sub = self.signal[start:end] slope = (self.signal[end - 1] - self.signal[start - 1]) / (end - start) intercept = self.signal[start - 1] approx = slope.reshape(-1, 1) * np.arange( 1, end - start + 1 ) + intercept.reshape(-1, 1) return np.sum((sub - approx.transpose()) ** 2) 

## fit(signal)#

Set parameters of the instance.

Parameters:

Name Type Description Default
signal array

signal of shape (n_samples, n_dims) or (n_samples,)

required

Returns:

Type Description
CostCLinear

self

Source code in ruptures/costs/costclinear.py
 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 def fit(self, signal) -> "CostCLinear": """Set parameters of the instance. Args: signal (array): signal of shape (n_samples, n_dims) or (n_samples,) Returns: self """ if signal.ndim == 1: self.signal = signal.reshape(-1, 1) else: self.signal = signal return self