【发布时间】:2018-06-22 22:55:45
【问题描述】:
嘿,我正在尝试在此函数上运行优化器。它应该返回 X = 1.5。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as spo
def f(x):
# Given a scalar X, return some value (a real number)
Y = (X - 1.5)**2 + 0.5
print "X = {}, Y = {}".format(X, Y)
return Y
def test_run():
Xguess = 2.0
min_result = spo.minimize(f, Xguess, method = 'SLSQP', options = {'disp': True})
print "Minima found at:"
print "X = {}, Y = {}".format(min_result.x, min_result.fun)
if __name__ == "__main__":
test_run()
【问题讨论】:
-
X实际上是一个标量吗?如果不是,那么它很可能会迫使Y也成为一个(一维)numpy 数组。 -
CVers 注意:问题的原因是小写的
x传递给f,但被大写的X引用。 -
我应该删除它吗?
标签: python pandas numpy matplotlib scipy