Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Machine Learning by (19k points)

By processing a time series graph, I Would like to detect patterns that look similar to this:

enter image description here

Using a sample time series as an example, I would like to be able to detect the patterns as marked here:

enter image description here

What kind of AI algorithm (I am assuming machine learning techniques) do I need to use to achieve this? Is there any library (in C/C++) out there that I can use?

1 Answer

0 votes
by (33.1k points)

The following python code is the general approach to your problem. I have used ECG dataset from here.


import numpy as np

import numpy.random as rnd

import matplotlib.pyplot as plt 

import scipy.linalg as lin

import re

data=np.array(map(lambda l: map(float,filter(lambda x: len(x)>0,re.split('\\s+',l))),open('chfdb_chf01_275.txt'))).T




def create_mats(dat):





    A=np.zeros( (K,K) )


    pA=np.zeros( (K,K) )


    for i in xrange(1,K-1):









    w=np.ones( (K,2) , dtype=np.float)




    return A,pA,w,K


eta=10. #precision parameter for the autoregressive portion of the model 

lam=.1 #precision parameter for the weights prior 




x=np.ones( (T+1,M) ) # sequence data (just one sequence)



e=np.zeros( (T,K) )


v=np.zeros( (T,K) )

#store the forward and backward recurrences

f=np.zeros( (T+1,K) )

fls=np.zeros( (T+1) )


b=np.zeros( (T+1,K) )

bls=np.zeros( (T+1) )


#hidden states

z=np.zeros( (T+1), )

#expected hidden states

ex_k=np.zeros( (T,K) )

# expected pairs of hidden states

ex_kk=np.zeros( (K,K) )

nkk=np.zeros( (K,K) )

def fwd(xn):

    global f,e

    for t in xrange(T):





        assert f[t+1,0]==0

def bck(xn):

    global b,e

    for t in xrange(T-1,-1,-1):





def em_step(xn):

    global A,w,eta

    global f,b,e,v

    global ex_k,ex_kk,nkk

    x=xn[:-1] #current data vectors

    y=xn[1:,:1] #next data vectors predicted from current

    #compute residuals,w.T) # (N,K) <- (N,1) (N,K)





    # compute expected hidden states

    for t in xrange(len(e)):



    # compute expected pairs of hidden states    

    for t in xrange(len(f)-1):






    # solve the weighted regression problem for emissions weights

    #  x and y are from above 

    for k in xrange(K):



        w[k,:]=lin.solve(dx+lam*np.eye(x.shape[1]), dy)

    #return the probability of the sequence (computed by the forward algorithm)

    return fls[-1]

if __name__=='__main__':

    #run the em algorithm

    for i in xrange(20):

        print em_step(x)





    for i in r:


Hope this answer helps.

by (100 points)
This is great, but somewhat cryptic. A little bit of explanation of each function would be very much appreciated. Thanks a lot.
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories