使用scipy.optimize进行非代数函数

时间:2013-12-20 21:23:07

标签: python optimization numpy scipy

我想尝试使用Scipy.optimze为数据挖掘工具构建解算器。

在使用最小化函数之前我必须定义的函数是这样的,它不是代数函数 - 只是对给出输出估计的另一个程序的可调用查询:

def query(x):
    import numpy as np
    file_direc_in="path_to_input_file.csv"
    file_direc_out="path_to_output_file.csv"

    x=np.array([[1,2,4,6]])
    with open(file_direc_in, 'w') as f:
        np.savetxt(f, x, delimiter=';', fmt='%.3f',newline='\r\n')
    f.close()
    os.system(Dataset_query.bat)
    #batch file takes the array i wrote to from input_file and estimates a result
    #afterwards the output will be taken from the output file:
    f = open(file_direc_out,'r')
    out = np.array([[float(f.readlines()[0])]])
    f.close()
    return out


from scipy.optimize import minimize
x0=np.array([[1,1,1,1]])#first guess
res=minimize(query,x0,method='nelder-mead',callback=True)

在我调用res部分后,我看到我通常在控制台中看到的内容:数据挖掘工具回答我的查询,但res成为循环,x0数组作为输入在每个周期 - 我想在每个cicle上另一个数组都由minimize函数测试。

我做错了什么? 如何更改我的query函数以实现函数scipy将最小化?

1 个答案:

答案 0 :(得分:2)

def query(x):
    import numpy as np
    file_direc_in="path_to_input_file.csv"
    file_direc_out="path_to_output_file.csv"

    x=np.array([[1,2,4,6]])

重要部分:

    x=np.array([[1,2,4,6]])

你扔掉你的输入!不要这样做,看看会发生什么。

相关问题