我需要用随机初始化的猜测数据"x"最小化函数func(x)
np.random.seed(1234)
m = 500 #500
n = 100 #100
x = np.asmatrix(np.random.randint(500,1000,size=(n,1)))
def func(x):
A = np.asmatrix(np.random.randint(-10,-1, size=(n, m)))
b = np.asmatrix(np.random.randint(500,10000,size=(m,1)))
c = np.asmatrix(np.random.randint(1,10,size=(n,1)))
fx = c.transpose()*x - sum(np.log10((b - A.transpose()* x)))
return fx
sc.optimize.fmin_cg(func,y)
我面临这个错误"ValueError:形状(1100)和(1100)未对齐:100(dim 1)!=1(dim 0)"不确定scipy对数据的预期。我对scipy是个新手。如果有人能指出正确的方向那就太好了。
应该这样做:
import scipy.optimize
import numpy as np
np.random.seed(1234)
m = 500 #500
n = 100 #100
x0 = np.random.rand(n)
A = np.asmatrix(np.random.randint(1,10, size=(n, m)))
b = np.asmatrix(np.random.randint(500,10000,size=(m,1)))
c = np.asmatrix(np.random.randint(1,10,size=(n,1)))
def func(x, A, b, c):
fx = np.dot(c.T, x) - np.sum(np.log10((b - np.dot(A.T, x))))
return fx
res = scipy.optimize.fmin_cg(func, x0, args=(A,b,c), maxiter=5)