小编Esk*_*app的帖子

使用连续隐马尔可夫模型的时间序列预测步骤

我正在尝试使用高斯 HMM 预测股市。我不知道模型训练后预测步骤是如何完成的。我不明白准确预测最可能的状态序列如何有助于预测未来价值。

One of the question asked suggest this method: "Use the Viterbi algorithm with the (partial) sequence to obtain the most likely hidden-state-sequence. Take the emission distribution of the last hidden state in this sequence and predict e.g. the mean of that distribution (which often is Gaussian)."

I did not get what he says after predicting most likely state sequence.

I have trained my model using functions available with hmmlearn in python. I have also applied Viterbi algorithm …

python time-series hidden-markov-models hmmlearn

9
推荐指数
1
解决办法
3365
查看次数

创建一个numpy数组子类,其中所有形状更改操作都返回一个普通数组

我有一个数组子类,其中一些额外的属性仅对对象的原始形状有效.有没有办法确保所有数组形状更改操作返回一个普通的numpy数组而不是我的类的实例?

我已经写array_wrap,但是这似乎并不具有像操作的任何影响np.mean,np.sumnp.rollaxis.这些只是返回我班级的一个实例.

import numpy as np    

class NewArrayClass(np.ndarray):
    __array_priority__ = 3.0
    def __array_wrap__(self, out_arr, context=None):
        if out_arr.shape == self.shape:
            out = out_arr.view(new_array)
            # Do a bunch of class dependant initialization and attribute copying.
            # ...
            return out
        else:
            return np.asarray(out_arr)

A = np.arange(10)
A.shape = (5, 2)
A = arr.view(NewArrayClass)
# Would like this to be np.ndarray, but get new_array_class.
print type(np.sum(A, 0))
Run Code Online (Sandbox Code Playgroud)

我想我必须做的东西__new__还是__array_finalize__,但我还没有什么线索.

更新: 仔细阅读子类化的numpy文档(http://docs.scipy.org/doc/numpy/user/basics.subclassing.html)后,所有数组形状更改操作都在执行"从模板新建"操作.所以问题就变成了,如何使'new from template'操作返回ndarray实例而不是我的类的实例.据我所知, …

python numpy scipy

7
推荐指数
1
解决办法
955
查看次数

为什么 numba 无法编译这个 for 循环?

我尝试计算一维离散余弦变换(类型 2),我试图用 numba 提高我的性能。我有以下代码:

import numpy as np
import math
import numba

@numba.jit()
def epsilon(N:int, i: int) -> float:
    if i == 0 or i == N:
        return math.sqrt(2)/2
    return 1.0

@numba.jit()
def dct2(a):
    n = len(a)
    y = np.empty([2*n])
    y[:len(a)] = a
    y[n:] = np.flip(a)
    fft = np.fft.fft(y)
    erg = np.empty([n])
    factor = 1/math.sqrt(2*n)
    for i in range(0,n):
        erg[i] = factor*epsilon(n,i)*(math.cos(-i*2*math.pi/(4*n))*fft[i].real - math.sin(-i*2*math.pi/(4*n))*fft[i].imag)
    return erg
Run Code Online (Sandbox Code Playgroud)

我认为它无法编译for循环,但我不知道为什么。根据我对 numba 文档的理解,应该能够解除循环。

我收到以下警告:

In definition 0:
    All templates rejected with literals.
In definition …
Run Code Online (Sandbox Code Playgroud)

python numba python-3.6

5
推荐指数
1
解决办法
2689
查看次数

如何在 Pycharm (Python 3.8 / 3.9) 中安装 YAML 包?

如何在 Pycharm 中安装 YAML 包?我想将它用于 python 3.8(或 3.9)项目。当尝试通过项目设置安装包时,出现以下错误:

ERROR: Could not find a version that satisfies the requirement yaml

或者,我尝试通过键入 来搜索通过命令窗口可用的任何 YAML 包pip search yaml。然后我收到以下错误:

ERROR: XMLRPC request failed [code: -32500]

有什么建议吗?

谢谢

yaml pip pycharm

5
推荐指数
1
解决办法
2万
查看次数

工厂调用备用构造函数(classmethod)

我正在努力找到一种方法来factory_boy使用定义为a的备用构造函数来创建类Factory(我使用版本2.11.1和Python 3)@classmethod.

因此,假设我们有一个用于构建具有默认构造函数的2D点对象的类,另外还有2个:

class Point:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    @classmethod
    def fromlist(cls, coords):  # alternate constructor from list
        return cls(coords[0], coords[1])

    @classmethod
    def duplicate(cls, obj):  # alternate constructor from another Point
        return cls(obj.x, obj.y)
Run Code Online (Sandbox Code Playgroud)

我创建了一个基本的Point工厂:

import factory

class PointFactory(factory.Factory):
    class Meta:
        model = Point
        inline_args = ('x', 'y')

    x = 1.
    y = 2.
Run Code Online (Sandbox Code Playgroud)

默认情况下,它似乎调用__init__类的构造函数,这似乎非常合乎逻辑.我不能找到一种方法,通过inline_argscoords使用可选的构造fromlist.有办法吗?

这是我第一次在工作和建造工厂的经历,所以我也可能在网上查找错误的关键字......

python testing class-method python-3.x factory-boy

3
推荐指数
1
解决办法
281
查看次数

有没有办法在 tf.keras 中保存指定时期的模型?

使用 tf.keras.callbacks,我只能通过选择一个要监控的属性(通常是验证准确性)来自动保存最佳模型,但有时,我需要根据验证和训练准确性的比较来保存它。我怎样才能做到这一点?

tf.keras.history 文件是否记录模型在每个时期的权重?如果是这样,如何通过指定我想要的纪元从历史文件中保存我的模型?这是另一种可能的解决方案。

这就是我遇到的情况:有时,我的验证准确性在早期时期非常高(我想这纯粹是偶然),而我的训练准确性仍然远远低于它。该纪元最终成为自动保存的模型。这是一个蹩脚的模型,因为它的训练精度很差,但由于它的验证精度很高,所以它被拯救了。如果它保存在训练和验证准确性满足的地方,那将是一个非常好的模型。因此,在每个时期,我更愿意比较训练准确性和验证准确性,选择两者中最低的一个,并据此决定我的最佳模型。关于如何做到这一点有什么建议吗?

python keras tensorflow tf.keras

2
推荐指数
1
解决办法
2559
查看次数