1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Unsupervised Time Series Segmentation Without Predefined Number of Segments

Discussão em 'Python' iniciado por Stack, Novembro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have univariate time series data (example below) and I want to automatically find distinct low-scoring segments (in my example there is one big low segment). I don’t know how many latent states there are in advance.

    Things I tried:

    hmmlearn Gaussian HMM — but the fitted HMM often quickly leaves low states, so contiguous low segments aren’t stable. (see 2nd picture)

    A band-aid solution that I found works: pick large # of n_components (10), fit HMM, then merge all states with mean < 0.3. This works but feels unsophisticated.

    I read about sticky HMMs, but I’m not sure what approach is best (whether non-HMM route is better). I want a principled, reproducible method — preferably simple to try first. Some of these more complex methods don't have well-maintained tutorials/code snippets I can learn from. Any code snippets for this small example would be greatly appreciated!

    Below I include the code to simulate the example signal:

    Here's code to replicate a similar plot:

    import matplotlib.pyplot as plt
    import numpy as np
    x = np.arange(5000)

    y = 0.12 + 0.02*np.random.randn(5000) # baseline noise
    y += 0.6*np.exp(-((x-400)**2)/(2*180**2)) # big left peak
    y += 0.20*np.exp(-((x-1150)**2)/(2*90**2)) # mid bump
    y += 0.03*np.sin(x/35) # small waves
    y += 0.06*np.sin(x/7) * ( (x>3400) & (x<4200) ) # oscillations region
    y += 0.75*np.exp(-((x-4700)**2)/(2*120**2)) # large right peak

    # ---- PLOT ----
    plt.figure(figsize=(14,4))
    plt.plot(x, y, linewidth=1.3)

    plt.xlim(0, 5000)
    plt.ylim(0, 0.85)

    plt.tight_layout()
    plt.show()


    [​IMG] [​IMG]

    Continue reading...

Compartilhe esta Página