You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importpandasaspdimportnumpyasnpfromtimesegmentimportPartition_treeimportmatplotlib.pyplotasplt# Read datadata=pd.read_csv('data_sample.csv')
# Invert time axis (specific for this data)data=data.iloc[::-1]
# Segment the 256 most recent pointsmy_obj=Partition_tree(np.array(data['value'])[data.shape[0] -256:], # data as numpy array-1, # Max depth of the partitionning tree1, # Early Stop30, # The number of segment desired after pruning0.0, # A Complexity parameter1) # Tau : Minimum number of observations within a segment# Build the partition treeres=my_obj.split()
# Tree pruningmy_obj.weakest_link_pruning()
# Get predictionspreds=my_obj.get_predictions()
# Get segments durationsdurations=my_obj.get_durations()
print(durations)
# Plot raw data alongside with predictionplt.plot(np.arange(np.array(data['date'])[data.shape[0] -256:].shape[0]),
np.array(data['value'])[data.shape[0] -256:], 'k',
np.arange(np.array(data['date'])[data.shape[0] -256:].shape[0]),
preds, 'ro')
plt.show()
Object Partition_tree parameters
signal : The timeseries to be segmented. It should be a numpy array of shape = [n_samples]
max_depth : The maximum depth of the partitioning tree. If -1, then no depth constraint exists on the tree
early_stop : Early Stop. If 1, then stop splitting a node if no MSE improvment is found. Otherwise, the best split is performed, even if it induces a MSE increase.
nb_segments : The number of segments desired when performing the Weakest Link Pruning
delta_complexity : A complexity parameter. Only perform the best split if np.abs(MSE_CurrentNode - min(MSE_LeftChild + MSE_RightChild)) / MSE_CurrentNode <= delta_complexity Which in human language reads as : "only perform the best split if it decreases the MSE by more than delta_complexity percentage"
tau : The minimum number of observations within a segment. If the current segment has less than 2*tau observation, we do not split. Otherwise, we split in two segments, both of which of duration greater than tau