【问题标题】:Minimize function with arrays as arguments [closed]以数组作为参数最小化函数[关闭]
【发布时间】:2014-02-07 04:23:55
【问题描述】:

我有一个功能,我想最小化。它是普通最小二乘法的矢量化版本。

import numpy as np
from scipy import optimize

def lr_cost_function(theta, x, y, derivative = False, hypotesis=linear_hypotesis, polynom = 1):
    hyp = hypotesis(theta, x, polynom)
    print("Hyp: ", hyp.shape)
    dif = hyp - y
    print("Dif:", dif.shape)
    reuslt = dot(dif.T,dif)
    print("RES", reuslt.shape)
    return 1/len(y)*(dot(dif.T,dif)[0,0])

def linear_hypotesis(theta, x, polynom = 1):
    print(x.shape, theta.shape, type(theta))
    return np.dot(x, theta)

所以我这样调用最小化:

optimize.minimize(fun=lr_cost_function, x0=theta_copy, args=(x, y))

并且我的代码无法完成,因为在 optimize.py 参数中 x0 变平并且我的矢量化被完全破坏(0.13.2 scipy 版本中的第 822 行)。我什至无法完成代码并查看结果,因为我没有足够的内存,并且在计算差异时一切都出错了。

【问题讨论】:

    标签: python numpy scipy


    【解决方案1】:

    我对数组的维度有点困惑,所以出现了这个错误。我的 theta(它是二维数组)应该被展平,并且代码中的所有向量(也是二维数组)应该被展平,因此代码可以以最小的变化工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 2021-12-31
      • 2016-06-18
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多