Python 以数组作为参数最小化函数

Python 以数组作为参数最小化函数,python,numpy,scipy,Python,Numpy,Scipy,我有一个函数,我想最小化它。它是普通最小二乘法的矢量化版本 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

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

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行)。我甚至无法完成代码并查看结果,因为我没有足够的内存,并且在计算dif时出现了所有错误。

我对数组的尺寸有点困惑,因此出现了此错误。我的θ(它是二维数组)应该是平坦的,代码中的所有向量(也是二维数组)都应该是平坦的,这样代码可以在最小的变化下工作