python - numpy 的参数方程

标签 python numpy parametric-equations

我目前正在为时间序列分类 (TSC) 在 python 中实现一个称为回声状态网络 (ESN) 的递归神经网络 (RNN)。

我想使用参数方程生成轨迹,然后训练我的神经网络对这些轨迹进行分类,就像 Mickael Hüsken & Peter Stagge, Recurrent Neural Networks for Time Series Classification 的这篇文章一样.最后,我想比较一下我的ESN和他们的RNN的性能。
好吧,我在生成这些轨迹之一时遇到了麻烦。

根据这篇文章,这里是三个类:

enter image description here
哪个应该产生这样的东西: enter image description here

我为每个类别生成 50 个轨迹,alpha 是一个固定为 0.7 的 float ,beta 和 t0 在 0 到 2*pi 之间随机选择。一条轨迹包含 30 个点,因此时间步长为 (2*pi)/30。

这是我的代码,我知道这不是最 Pythonic 的方式,但它完成了第一类和第三类的工作。但是,第二类仍然存在问题:(

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()  

当我绘制第二类轨迹(使用 matplotlib)时,我得到了一个奇怪的结果......例如:

enter image description here

最佳答案

第二个等式对我来说似乎很奇怪,而且确实是does not seem to produce the picture shown .

查看第 1 类和第 3 类的方程式,很容易猜出一个参数方程式可以产生具有三个“花瓣”的图形:

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

然后做:

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()

给出:

3-petals figure

关于python - numpy 的参数方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23455226/

相关文章:

python - 当右括号位于同一行时自动插入引号失败

python /PIL : Create and save image from data uri

python - 如何使用标志仅返回一组使用 findall- Python

python - 在 NumPy 中,如何在不使用 np.s_ 的情况下从一维数组中提取范围?

java - 方程驱动的平滑阴影同心形状

ios - 如何使用这个复杂的参数方程在 iOS 中绘制曲线

python - Tensorflow Keras 嵌入层错误 : Layer weight shape not compatible

python - numpy 数组映射并取平均值

python - 替换np数组的元素

python - 使用 matplotlib Python 绘制参数图