Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parametric equation with numpy

I'm currently implementing a Recurrent Neural Network (RNN) called Echo State Network (ESN) in python for Time Series Classification (TSC).

I want to generate trajectories using parametric equations, then train my neural network to classify these trajectories, like in this article by Mickael Hüsken & Peter Stagge, Recurrent Neural Networks for Time Series Classification. Finally, I want to compare the performance between my ESN and their RNN.
Well, I'm in trouble with the generation of one of these trajectories.

Here are the three classes according to this article:

enter image description here
Which should generate something like this: enter image description here

I generate 50 trajectories of each class, alpha is a float fixed to 0.7, beta and t0 are chosen randomly between 0 and 2*pi. A trajectory contains 30 points, so the timestep is (2*pi)/30.

Here is my code, I know it's not the most pythonic way, but it does the job for the first and third class. However, the second class is still bugged :(

import numpy as np
import sys, getopt, random

timestep = 2.0*np.pi / 30.0
alpha = 0.7

def class1(t, beta):
    return alpha*np.sin(t+beta)*np.abs(np.sin(t)), alpha*np.cos(t+beta)*np.abs(np.sin(t))

def class2(t, beta):
    return alpha*np.sin(t/2.0+beta)*np.sin(3.0/2.0*t), alpha*np.cos(t+beta)*np.sin(2.0*t)

def class3(t, beta):
    return alpha*np.sin(t+beta)*np.sin(2.0*t), alpha*np.cos(t+beta)*np.sin(2.0*t)

def generate():
    clazz = {
            '1' : class1,
            '2' : class2,
            '3' : class3
            }

    for classID in clazz :
        for i in xrange(50):
            fd = open("dataset/%s_%s"%(classID, i+1), 'w')
            beta = 2*np.pi*np.random.random()
            t = 2*np.pi*np.random.random()
            for _ in xrange(30):
               fd.write("%s %s\n"%clazz[classID](t, beta))
               t += timestep
            fd.close()  

When I plot trajectories of the second class (using matplotlib), I get a strange result... for example:

enter image description here

like image 248
NiziL Avatar asked Oct 20 '22 09:10

NiziL


1 Answers

The second equation seems strange to me, and indeed does not seem to produce the picture shown.

Looking at the equations for classes 1 and 3, it is easy to guess a parametric equation that would produce a figure with three "petals":

def class2(t, beta):
    return alpha*np.sin(t+beta)*np.sin(3*t), alpha*np.cos(t+beta)*np.sin(3*t)

Then doing:

for beta in [0, np.pi/3, np.pi/2]:
    pylab.plot(*class2(np.linspace(0, np.pi, 100), beta),
               label='$\\beta={:.3f}$'.format(beta))
pylab.legend()

gives:

3-petals figure

like image 150
Lev Levitsky Avatar answered Oct 23 '22 10:10

Lev Levitsky