All problems
PELT Segmentation Cost
Problem
Compute the PELT objective for a segmentation: the sum of within-segment squared deviations from each segment mean plus penalty * (number of segments - 1). Changepoint indices mark segment starts; indices that are not strictly inside the series are ignored.
The cost of an empty segment is skipped.
Hint
The penalty term charges for each additional segment introduced by a changepoint.
Test cases
case 1
in: [[1,2,3,10,11,12],[3],0.5]
expected: 4.5
case 2
in: [[1,2,3,4],[],1]
expected: 5
case 3
in: [[5,5,5,5],[1,2,3],0]
expected: 0
case 4
in: [[1,9,1,9],[2],2]
expected: 66
case 5
in: [[1,2],[0,5],1]
expected: 0.5
Starter codePython
def pelt_segmentation_cost(series, changepoints, penalty):
# Your code here
pass5 test cases · run your code against them in the editor