import matplotlib.pyplot as plt
%matplotlib inline
y = np.array([ np.random.random()*10 + 50*int(np.random.random() > 0.99)*np.sign(np.random.random()-0.5) for _ in range(1000)])
y[:len(y)//2] += 200
y += 100
plt.figure(figsize=(20,5))
plt.plot(y)

import rrcf
import numpy as np
import math
num_trees = 20
shingle_size = 1
tree_size = 100
threshold = tree_size * 0.2
X = []
forest = []
scores = []
points = rrcf.shingle(y, size=shingle_size)
for i in range(num_trees):
tree = rrcf.RCTree(random_state=i)
forest.append(tree)
for index, point in enumerate(points):
scorei = []
for tree in forest:
if len(tree.leaves) >= tree_size:
tree.forget_point(index - tree_size)
tree.insert_point(point, index=index)
scorei.append(tree.codisp(index))
scores.append(np.median(scorei))
plt.figure(figsize=(20,5))
plt.subplot(2,1,1)
plt.plot(scores)
plt.subplot(2,1,2)
plt.plot(y)
scores = np.array(scores)
locs = np.where( scores >= threshold)
locs = locs[0]
if len(locs) > 0:
plt.plot(locs, y[locs], 'ro')
