【发布时间】:2013-12-21 05:29:23
【问题描述】:
我想尝试使用 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)
调用respart 后,我看到了我通常在控制台中看到的内容:数据挖掘工具回答了我的查询,但res变成了一个循环,x0array 是每个循环的输入 - 我想在每个 cicle 上,minimize 函数会测试另一个数组。
我做错了什么?
如何更改我的queryfunction 以实现 scipy 将最小化的功能?
【问题讨论】:
标签: python optimization numpy scipy