Python fmin too公司

2024-06-01 09:39:24 发布

您现在位置:Python中文网/ 问答频道 /正文

在x\u数据中有一个3x2000个numpy数组,在y_数据中有一个1x2000个numpy数组,我把它传递给这个函数reagress,给我一条回归线。它工作得很好。问题是,我正在尝试做一些回溯测试,并测试1000种情况,我必须回归1000次,它将需要我大约5分钟来运行这个。在

我试着把变量标准化,但似乎没有加快速度。在

我也试过了一段时间,似乎是打破了它。在

有什么想法吗?谢谢!在

def regress(x_data, y_data, fg_spread, fg_line):

    theta = np.matrix(np.ones((1,x_data.shape[0]))*.11)
    hyp = lambda theta, x: 1 / (1 + np.exp(-(theta*x)))
    cost_hyp = lambda theta, x, y: ((np.multiply(-y,np.log10(hyp(theta,x)))) - \
                            (np.multiply((1-y),(np.log10(1-hyp(theta, x)))))).sum()

    theta = scipy.optimize.fmin(cost_hyp, theta, args=(x_data,y_data), xtol=.00001, disp=0)

    return hyp(np.matrix(theta),np.matrix([1,fg_spread, fg_line]).reshape(3,1))

Tags: 数据lambdanumpydatanpline数组matrix
1条回答
网友
1楼 · 发布于 2024-06-01 09:39:24

使用numexpr使你的hyp和cost_hyp计算更快。fmin函数族为不同的条目多次计算这些函数。所以这些函数的任何增益都直接在最小化中报告。在

例如,您可以替换:

hyp = lambda theta, x: 1 / (1 + np.exp(-(theta*x)))

签署人:

^{pr2}$

Numexpr用于numpy数组。在

相关问题 更多 >