import pandas as pd
from scipy.optimize import fmin
data = pd.DataFrame({'DIV': [1,2,3]*3,
'MONTH': ['May','May','May','June','June','Jun','Jul','Jul','Jul'],
'C':[8]*9,
'U':[3,2,1]*3,
'S':[9]*9})
data.to_csv(r'C:\Users\mbabski\Documents\Unit Plan Summer 2016\data_test.csv')
def return_array(x):
return x.values
def mape(c,u,s,r): #returns an array of line level Mean Absolute Percentage Errors
p = c + u * r
m = abs(1.0-(p/s))
return m
def e(c,u,s,r): #calculates average of the MAPEs
return np.mean(mape(c,u,s,r))
for d in range(1,4):
div_data = data[data.DIV==d]
c = return_array(div_data.C)
u = return_array(div_data.U)
s = return_array(div_data.S)
r0 = [[1.0]]
t = fmin(e,r0,args=(c,u,s))
print 'r:',t
优化成功终止。
当前功能值:0.000000
迭代次数:29
功能评估:58
r:[ - 69。]
优化成功终止 当前功能值:0.000000
迭代次数:29
功能评估:58
r:[ - 70。]
优化成功终止 当前功能值:0.000000
迭代次数:29
功能评估:58
r:[ - 71。]
为什么我得到r = -69,-70和-71? 我应该用这个数据得到r = 0.333,0.555和0.999。
答案 0 :(得分:3)
scipy.optimize.fmin
会将尝试最小化的值作为函数的第一个参数传递。如果您将函数重写为
def e(r,c,u,s): #calculates average of the MAPEs
return np.mean(mape(c,u,s,r))
您获得了正确的结果
for d in range(1,4):
div_data = data[data.DIV==d]
c = return_array(div_data.C)
u = return_array(div_data.U)
s = return_array(div_data.S)
r0 = [[1.0]]
t = fmin(e,r0,args=(c,u,s))
print 'r:',t
Optimization terminated successfully. Current function value: 0.000011 Iterations: 16 Function evaluations: 32 r: [ 0.33330078] Optimization terminated successfully. Current function value: 0.000000 Iterations: 15 Function evaluations: 30 r: [ 0.5] Optimization terminated successfully. Current function value: 0.000000 Iterations: 10 Function evaluations: 20 r: [ 1.]