How to model segmentation of a sequence to similar parts?
The problem can be reframed as a binary classification by ignoring the order, and then a decision tree algorithm can learn to separate the groups with perfect accuracy.

import numpy as np
from sklearn.tree import DecisionTreeClassifier

# Define training data
training_data    = np.array([1, 2, 1, 1, 2, 1, 2, 5, 6, 5, 4, 5, 1, 1, 2, 1, 1, 2, 1]).reshape(-1, 1)
training_targets = np.array([1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1]).reshape(-1, 1)

# Train a descision tree with scikit-learn
clf = DecisionTreeClassifier(max_depth=1)
clf.fit(training_data, training_targets)

# Let's if the model can predict unseen data
test_data_low = np.array([0]).reshape(1, -1)
prediction = clf.predict(test_data_low)
assert prediction == 1

test_data_high = np.array([7]).reshape(1, -1)
prediction = clf.predict(test_data_high)
assert prediction == 2


The ability of a model to learn to generalize (i.e., predict total new values) can be increased by increasing regularization. In the above example, one method to increase regularization is limiting the depth of the decision tree. I have set the depth to be a single split, aka decision stump. A decision stump is the most regularized version of a decision tree.